File: commit_test.go

package info (click to toggle)
syncthing 0.14.18%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,388 kB
  • ctags: 4,608
  • sloc: xml: 781; sh: 271; makefile: 45
file content (100 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

package config

import (
	"errors"
	"testing"
)

type requiresRestart struct {
	committed chan struct{}
}

func (requiresRestart) VerifyConfiguration(_, _ Configuration) error {
	return nil
}
func (c requiresRestart) CommitConfiguration(_, _ Configuration) bool {
	select {
	case c.committed <- struct{}{}:
	default:
	}
	return false
}
func (requiresRestart) String() string {
	return "requiresRestart"
}

type validationError struct{}

func (validationError) VerifyConfiguration(_, _ Configuration) error {
	return errors.New("some error")
}
func (c validationError) CommitConfiguration(_, _ Configuration) bool {
	return true
}
func (validationError) String() string {
	return "validationError"
}

func TestReplaceCommit(t *testing.T) {
	t.Skip("broken, fails randomly, #3834")

	w := Wrap("/dev/null", Configuration{Version: 0})
	if w.RawCopy().Version != 0 {
		t.Fatal("Config incorrect")
	}

	// Replace config. We should get back a clean response and the config
	// should change.

	err := w.Replace(Configuration{Version: 1})
	if err != nil {
		t.Fatal("Should not have a validation error:", err)
	}
	if w.RequiresRestart() {
		t.Fatal("Should not require restart")
	}
	if w.RawCopy().Version != CurrentVersion {
		t.Fatal("Config should have changed")
	}

	// Now with a subscriber requiring restart. We should get a clean response
	// but with the restart flag set, and the config should change.

	sub0 := requiresRestart{committed: make(chan struct{}, 1)}
	w.Subscribe(sub0)

	err = w.Replace(Configuration{Version: 2})
	if err != nil {
		t.Fatal("Should not have a validation error:", err)
	}

	<-sub0.committed
	if !w.RequiresRestart() {
		t.Fatal("Should require restart")
	}
	if w.RawCopy().Version != CurrentVersion {
		t.Fatal("Config should have changed")
	}

	// Now with a subscriber that throws a validation error. The config should
	// not change.

	w.Subscribe(validationError{})

	err = w.Replace(Configuration{Version: 3})
	if err == nil {
		t.Fatal("Should have a validation error")
	}
	if !w.RequiresRestart() {
		t.Fatal("Should still require restart")
	}
	if w.RawCopy().Version != CurrentVersion {
		t.Fatal("Config should not have changed")
	}
}