1
0

commit_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 (validationError) CommitConfiguration(_, _ Configuration) bool {
  32. return true
  33. }
  34. func (validationError) String() string {
  35. return "validationError"
  36. }
  37. func replace(t testing.TB, w Wrapper, to Configuration) {
  38. t.Helper()
  39. waiter, err := w.Modify(func(cfg *Configuration) {
  40. *cfg = to
  41. })
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. waiter.Wait()
  46. }
  47. func TestReplaceCommit(t *testing.T) {
  48. w := wrap("/dev/null", Configuration{Version: 0}, device1)
  49. defer w.stop()
  50. if w.RawCopy().Version != 0 {
  51. t.Fatal("Config incorrect")
  52. }
  53. // Replace config. We should get back a clean response and the config
  54. // should change.
  55. replace(t, w, Configuration{Version: 1})
  56. if w.RequiresRestart() {
  57. t.Fatal("Should not require restart")
  58. }
  59. if w.RawCopy().Version != CurrentVersion {
  60. t.Fatal("Config should have changed")
  61. }
  62. // Now with a subscriber requiring restart. We should get a clean response
  63. // but with the restart flag set, and the config should change.
  64. sub0 := requiresRestart{committed: make(chan struct{}, 1)}
  65. w.Subscribe(sub0)
  66. replace(t, w, Configuration{Version: 1})
  67. <-sub0.committed
  68. if !w.RequiresRestart() {
  69. t.Fatal("Should require restart")
  70. }
  71. if w.RawCopy().Version != CurrentVersion {
  72. t.Fatal("Config should have changed")
  73. }
  74. // Now with a subscriber that throws a validation error. The config should
  75. // not change.
  76. w.Subscribe(validationError{})
  77. _, err := w.Modify(func(cfg *Configuration) {
  78. *cfg = Configuration{Version: 3}
  79. })
  80. if err == nil {
  81. t.Fatal("Should have a validation error")
  82. }
  83. if !w.RequiresRestart() {
  84. t.Fatal("Should still require restart")
  85. }
  86. if w.RawCopy().Version != CurrentVersion {
  87. t.Fatal("Config should not have changed")
  88. }
  89. }