commit_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "errors"
  9. "testing"
  10. )
  11. type requiresRestart struct {
  12. committed chan struct{}
  13. }
  14. func (requiresRestart) VerifyConfiguration(_, _ Configuration) error {
  15. return nil
  16. }
  17. func (c requiresRestart) CommitConfiguration(_, _ Configuration) bool {
  18. select {
  19. case c.committed <- struct{}{}:
  20. default:
  21. }
  22. return false
  23. }
  24. func (requiresRestart) String() string {
  25. return "requiresRestart"
  26. }
  27. type validationError struct{}
  28. func (validationError) VerifyConfiguration(_, _ Configuration) error {
  29. return errors.New("some error")
  30. }
  31. func (c validationError) CommitConfiguration(_, _ Configuration) bool {
  32. return true
  33. }
  34. func (validationError) String() string {
  35. return "validationError"
  36. }
  37. func TestReplaceCommit(t *testing.T) {
  38. t.Skip("broken, fails randomly, #3834")
  39. w := wrap("/dev/null", Configuration{Version: 0})
  40. if w.RawCopy().Version != 0 {
  41. t.Fatal("Config incorrect")
  42. }
  43. // Replace config. We should get back a clean response and the config
  44. // should change.
  45. _, err := w.Replace(Configuration{Version: 1})
  46. if err != nil {
  47. t.Fatal("Should not have a validation error:", err)
  48. }
  49. if w.RequiresRestart() {
  50. t.Fatal("Should not require restart")
  51. }
  52. if w.RawCopy().Version != CurrentVersion {
  53. t.Fatal("Config should have changed")
  54. }
  55. // Now with a subscriber requiring restart. We should get a clean response
  56. // but with the restart flag set, and the config should change.
  57. sub0 := requiresRestart{committed: make(chan struct{}, 1)}
  58. w.Subscribe(sub0)
  59. _, err = w.Replace(Configuration{Version: 2})
  60. if err != nil {
  61. t.Fatal("Should not have a validation error:", err)
  62. }
  63. <-sub0.committed
  64. if !w.RequiresRestart() {
  65. t.Fatal("Should require restart")
  66. }
  67. if w.RawCopy().Version != CurrentVersion {
  68. t.Fatal("Config should have changed")
  69. }
  70. // Now with a subscriber that throws a validation error. The config should
  71. // not change.
  72. w.Subscribe(validationError{})
  73. _, err = w.Replace(Configuration{Version: 3})
  74. if err == nil {
  75. t.Fatal("Should have a validation error")
  76. }
  77. if !w.RequiresRestart() {
  78. t.Fatal("Should still require restart")
  79. }
  80. if w.RawCopy().Version != CurrentVersion {
  81. t.Fatal("Config should not have changed")
  82. }
  83. }