-
Notifications
You must be signed in to change notification settings - Fork 3
/
sync_test.go
138 lines (114 loc) · 2.72 KB
/
sync_test.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package targetsync
import (
"context"
"fmt"
"testing"
"time"
"github.com/sirupsen/logrus"
)
func equalTargets(a, b []*Target) error {
aMap := make(map[string]*Target)
for _, target := range a {
aMap[target.Key()] = target
}
bMap := make(map[string]*Target)
for _, target := range b {
bMap[target.Key()] = target
}
if len(aMap) != len(bMap) {
return fmt.Errorf("Mismatch in len a=%d b=%d", len(aMap), len(bMap))
}
for k := range aMap {
if _, ok := bMap[k]; !ok {
return fmt.Errorf("b is missing %s", k)
}
}
return nil
}
func TestSyncer(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
cfg := &SyncConfig{
LockOptions: LockOptions{
Key: "a",
TTL: time.Second,
},
RemoveDelay: time.Second,
}
src := newmockSource()
dst := newmockDestination()
syncer := &Syncer{
Config: cfg,
Locker: &mockLocker{},
Src: src,
Dst: dst,
}
go syncer.Run(context.TODO())
targets := []*Target{
{IP: "1"},
{IP: "2"},
}
target := []*Target{targets[0]}
empty := []*Target{}
// set targets
src.ch <- targets
time.Sleep(time.Second)
// check that they match
tgts, _ := dst.GetTargets(nil)
if err := equalTargets(targets, tgts); err != nil {
t.Fatalf("Mismatch in targets err=%v expected=%+v actual=%+v", err, targets, tgts)
}
time.Sleep(time.Second * 2)
src.ch <- target
time.Sleep(time.Second * 2)
// check that they match
tgts, _ = dst.GetTargets(nil)
if err := equalTargets(target, tgts); err != nil {
t.Fatalf("Mismatch in targets err=%v expected=%+v actual=%+v", err, target, tgts)
}
src.ch <- empty
time.Sleep(time.Second * 2)
// check that they match
tgts, _ = dst.GetTargets(nil)
if err := equalTargets(empty, tgts); err != nil {
t.Fatalf("Mismatch in targets err=%v expected=%+v actual=%+v", err, empty, tgts)
}
}
// TestSyncer_Races is specifically checking for issues where removals and adds are racing with eachother
func TestSyncer_Races(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
cfg := &SyncConfig{
LockOptions: LockOptions{
Key: "a",
TTL: time.Second,
},
RemoveDelay: time.Second,
}
src := newmockSource()
dst := newmockDestination()
syncer := &Syncer{
Config: cfg,
Locker: &mockLocker{},
Src: src,
Dst: dst,
}
go syncer.Run(context.TODO())
target := []*Target{{IP: "1"}}
empty := []*Target{}
// Add 1
src.ch <- target
time.Sleep(time.Second * 2)
// remove it twice
src.ch <- empty
time.Sleep(time.Millisecond * 300)
src.ch <- empty
time.Sleep(time.Millisecond * 300)
// add 1
// sleep 1s
src.ch <- target
time.Sleep(time.Second * 2)
// check
tgts, _ := dst.GetTargets(nil)
if err := equalTargets(target, tgts); err != nil {
t.Fatalf("Mismatch in targets err=%v expected=%+v actual=%+v", err, target, tgts)
}
}