-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatcher_test.go
162 lines (124 loc) · 3.03 KB
/
watcher_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package eureka
import (
"sync"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Watcher", func() {
var (
interval = 10 * time.Millisecond
existingApp *App
registry *mockRegistry
watcher *Watcher
)
BeforeEach(func() {
existingApp = &App{
Name: "existing",
Instances: []*Instance{
&Instance{
ID: "one",
HostName: "one.example.com",
},
&Instance{
ID: "two",
HostName: "two.example.com",
},
},
}
registry = newMockRegistry()
registry.Register(existingApp)
watcher = newWatcher(registry, interval)
// should receive event for the above register
Eventually(watcher.Events()).Should(Receive())
})
AfterEach(func() {
watcher.Stop()
})
It("reports instances for newly registered apps", func() {
instance := &Instance{
ID: "one",
HostName: "new.example.com",
}
app := &App{
Name: "new",
Instances: []*Instance{instance},
}
expectedEvent := Event{
EventInstanceRegistered,
instance,
}
registry.Register(app)
Eventually(watcher.Events()).Should(Receive(Equal(expectedEvent)))
})
It("reports new instances for previously registered apps", func() {
instance := &Instance{
ID: "two",
HostName: "two.example.com",
}
existingApp.Instances = append(existingApp.Instances, instance)
expectedEvent := Event{
EventInstanceRegistered,
instance,
}
registry.Register(existingApp)
Eventually(watcher.Events()).Should(Receive(Equal(expectedEvent)))
})
It("reports instances that have been changed", func() {
existingApp.Instances[0] = &Instance{
ID: "one",
HostName: "updated.example.com",
}
expectedEvent := Event{
EventInstanceUpdated,
existingApp.Instances[0],
}
registry.Register(existingApp)
Eventually(watcher.Events()).Should(Receive(Equal(expectedEvent)))
})
It("reports instances that have been deregistered", func() {
expectedEvent := Event{
EventInstanceDeregistered,
existingApp.Instances[0],
}
existingApp.Instances = existingApp.Instances[1:]
registry.Register(existingApp)
Eventually(watcher.Events()).Should(Receive(Equal(expectedEvent)))
})
It("reports all instances when the app has been deregistered as a whole", func() {
expectedEvent := Event{
EventInstanceDeregistered,
existingApp.Instances[0],
}
registry.Deregister(existingApp.Name)
Eventually(watcher.Events()).Should(Receive(Equal(expectedEvent)))
})
})
type mockRegistry struct {
mtx sync.RWMutex
apps map[string]*App
}
func newMockRegistry() *mockRegistry {
return &mockRegistry{
apps: map[string]*App{},
}
}
func (m *mockRegistry) Register(app *App) {
m.mtx.Lock()
defer m.mtx.Unlock()
m.apps[app.Name] = app
}
func (m *mockRegistry) Deregister(name string) {
m.mtx.Lock()
defer m.mtx.Unlock()
delete(m.apps, name)
}
func (m *mockRegistry) Apps() ([]*App, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
apps := make([]*App, 0, len(m.apps))
for _, a := range m.apps {
apps = append(apps, a)
}
return apps, nil
}