Skip to content

Commit

Permalink
fixup! Rewrite testWatch
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Dec 20, 2021
1 parent 6837d14 commit 0032462
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,23 @@ func testPutGetDeleteExists(t *testing.T, kv store.Store) {
func testWatch(t *testing.T, kv store.Store) {
key := "testWatch"

err := kv.Put(key, []byte("0"), nil)
initialValue := []byte("-1")
err := kv.Put(key, initialValue, nil)
assert.NoError(t, err)
v, err := kv.Get(key)
assert.NoError(t, err)
assert.Equal(t, v.Value, initialValue)

stopCh := make(<-chan struct{})
events, err := kv.Watch(key, stopCh)
assert.NoError(t, err)
assert.NotNil(t, events)

inputs := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
// Update loop
go func() {
for i := 1; i < 5; i++ {
err := kv.Put(key, []byte(strconv.Itoa(i)), nil)
for _, i := range inputs {
err := kv.Put(key, []byte(i), nil)
if !assert.NoError(t, err) {
return
}
Expand All @@ -168,14 +173,28 @@ func testWatch(t *testing.T, kv store.Store) {
}()

// Check for updates
for i := 0; i < 5; i++ {
results := []string{}
for {
select {
case event := <-events:
assert.NotNil(t, event)
assert.Equal(t, event.Key, key)
assert.Equal(t, event.Value, []byte(strconv.Itoa(i)))
case <-time.After(getShortTimeout()):
t.Fatal("Timeout reached")
value := string(event.Value)
if value == string(initialValue) {
// skip it initial value as only zookeper catches it
continue
}
if v, _ := strconv.Atoi(value); v < 4 {
// on etcd initial events gets missed, test only tail events
continue
}
results = append(results, value)
if len(results) == len(inputs[4:]) {
assert.Equal(t, results, inputs[4:])
return
}
case <-time.After(10 * getShortTimeout()):
t.Fatalf("Timeout reached (results=%v)", results)
return
}
}
Expand Down

0 comments on commit 0032462

Please sign in to comment.