commit_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 http://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. w := Wrap("/dev/null", Configuration{Version: 0})
  39. if w.Raw().Version != 0 {
  40. t.Fatal("Config incorrect")
  41. }
  42. // Replace config. We should get back a clean response and the config
  43. // should change.
  44. err := w.Replace(Configuration{Version: 1})
  45. if err != nil {
  46. t.Fatal("Should not have a validation error:", err)
  47. }
  48. if w.RequiresRestart() {
  49. t.Fatal("Should not require restart")
  50. }
  51. if w.Raw().Version != 1 {
  52. t.Fatal("Config should have changed")
  53. }
  54. // Now with a subscriber requiring restart. We should get a clean response
  55. // but with the restart flag set, and the config should change.
  56. sub0 := requiresRestart{committed: make(chan struct{}, 1)}
  57. w.Subscribe(sub0)
  58. err = w.Replace(Configuration{Version: 2})
  59. if err != nil {
  60. t.Fatal("Should not have a validation error:", err)
  61. }
  62. <-sub0.committed
  63. if !w.RequiresRestart() {
  64. t.Fatal("Should require restart")
  65. }
  66. if w.Raw().Version != 2 {
  67. t.Fatal("Config should have changed")
  68. }
  69. // Now with a subscriber that throws a validation error. The config should
  70. // not change.
  71. w.Subscribe(validationError{})
  72. err = w.Replace(Configuration{Version: 3})
  73. if err == nil {
  74. t.Fatal("Should have a validation error")
  75. }
  76. if !w.RequiresRestart() {
  77. t.Fatal("Should still require restart")
  78. }
  79. if w.Raw().Version != 2 {
  80. t.Fatal("Config should not have changed")
  81. }
  82. }