forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
195 lines (172 loc) · 5.33 KB
/
watch.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package test
import (
"testing"
"time"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/vt/topo"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// waitForInitialValue waits for the initial value of
// keyspaces/test_keyspace/SrvKeyspace to appear, and match the
// provided srvKeyspace.
func waitForInitialValue(t *testing.T, ts topo.Impl, cell string, srvKeyspace *topodatapb.SrvKeyspace) (changes <-chan *topo.WatchData, cancel func()) {
var current *topo.WatchData
ctx := context.Background()
start := time.Now()
for {
current, changes, cancel = ts.Watch(ctx, cell, "keyspaces/test_keyspace/SrvKeyspace")
if current.Err == topo.ErrNoNode {
// hasn't appeared yet
if time.Now().Sub(start) > 10*time.Second {
t.Fatalf("time out waiting for file to appear")
}
time.Sleep(10 * time.Millisecond)
continue
}
if current.Err != nil {
t.Fatalf("watch failed: %v", current.Err)
}
// we got a valid result
break
}
got := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(current.Contents, got); err != nil {
t.Fatalf("cannot proto-unmarshal data: %v", err)
}
if !proto.Equal(got, srvKeyspace) {
t.Fatalf("got bad data: %v expected: %v", got, srvKeyspace)
}
return changes, cancel
}
// checkWatch runs the tests on the Watch part of the Backend API.
// We can't just use the full API yet, so use SrvKeyspace for now.
func checkWatch(t *testing.T, ts topo.Impl) {
ctx := context.Background()
cell := getLocalCell(ctx, t, ts)
// start watching something that doesn't exist -> error
current, changes, cancel := ts.Watch(ctx, cell, "keyspaces/test_keyspace/SrvKeyspace")
if current.Err != topo.ErrNoNode {
t.Errorf("watch on missing node didn't return ErrNoNode: %v %v", current, changes)
}
// create some data
srvKeyspace := &topodatapb.SrvKeyspace{
ShardingColumnName: "user_id",
}
if err := ts.UpdateSrvKeyspace(ctx, cell, "test_keyspace", srvKeyspace); err != nil {
t.Fatalf("UpdateSrvKeyspace(1): %v", err)
}
// start watching again, it should work
changes, cancel = waitForInitialValue(t, ts, cell, srvKeyspace)
defer cancel()
// change the data
srvKeyspace.ShardingColumnName = "new_user_id"
if err := ts.UpdateSrvKeyspace(ctx, cell, "test_keyspace", srvKeyspace); err != nil {
t.Fatalf("UpdateSrvKeyspace(2): %v", err)
}
// Make sure we get the watch data, maybe not as first notice,
// but eventually. The API specifies it is possible to get duplicate
// notifications.
for {
wd, ok := <-changes
if !ok {
t.Fatalf("watch channel unexpectedly closed")
}
if wd.Err != nil {
t.Fatalf("watch interrupted: %v", wd.Err)
}
got := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(wd.Contents, got); err != nil {
t.Fatalf("cannot proto-unmarshal data: %v", err)
}
if got.ShardingColumnName == "user_id" {
// extra first value, still good
continue
}
if got.ShardingColumnName == "new_user_id" {
// watch worked, good
break
}
t.Fatalf("got unknown SrvKeyspace: %v", got)
}
// remove the SrvKeyspace
if err := ts.DeleteSrvKeyspace(ctx, cell, "test_keyspace"); err != nil {
t.Fatalf("DeleteSrvKeyspace: %v", err)
}
// Make sure we get the ErrNoNode notification eventually.
// The API specifies it is possible to get duplicate
// notifications.
for {
wd, ok := <-changes
if !ok {
t.Fatalf("watch channel unexpectedly closed")
}
if wd.Err == topo.ErrNoNode {
// good
break
}
if wd.Err != nil {
t.Fatalf("bad error returned for deletion: %v", wd.Err)
}
// we got something, better be the right value
got := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(wd.Contents, got); err != nil {
t.Fatalf("cannot proto-unmarshal data: %v", err)
}
if got.ShardingColumnName == "new_user_id" {
// good value
continue
}
t.Fatalf("got unknown SrvKeyspace waiting for deletion: %v", got)
}
// now the channel should be closed
if wd, ok := <-changes; ok {
t.Fatalf("got unexpected event after error: %v", wd)
}
}
// checkWatchInterrupt tests we can interrupt a watch.
func checkWatchInterrupt(t *testing.T, ts topo.Impl) {
ctx := context.Background()
cell := getLocalCell(ctx, t, ts)
// create some data
srvKeyspace := &topodatapb.SrvKeyspace{
ShardingColumnName: "user_id",
}
if err := ts.UpdateSrvKeyspace(ctx, cell, "test_keyspace", srvKeyspace); err != nil {
t.Fatalf("UpdateSrvKeyspace(1): %v", err)
}
// Start watching, it should work.
changes, cancel := waitForInitialValue(t, ts, cell, srvKeyspace)
// Now cancel the watch.
cancel()
// Make sure we get the topo.ErrInterrupted notification eventually.
for {
wd, ok := <-changes
if !ok {
t.Fatalf("watch channel unexpectedly closed")
}
if wd.Err == topo.ErrInterrupted {
// good
break
}
if wd.Err != nil {
t.Fatalf("bad error returned for deletion: %v", wd.Err)
}
// we got something, better be the right value
got := &topodatapb.SrvKeyspace{}
if err := proto.Unmarshal(wd.Contents, got); err != nil {
t.Fatalf("cannot proto-unmarshal data: %v", err)
}
if got.ShardingColumnName == "user_id" {
// good value
continue
}
t.Fatalf("got unknown SrvKeyspace waiting for deletion: %v", got)
}
// Now the channel should be closed.
if wd, ok := <-changes; ok {
t.Fatalf("got unexpected event after error: %v", wd)
}
// And calling cancel() again should just work.
cancel()
}