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 083d82c
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testutils
import (
"fmt"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -148,18 +147,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", "0", "1", "2", "3", "4", "0", "1", "2", "3", "4"}
// 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 +172,22 @@ 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")
if string(event.Value) == string(initialValue) {
continue
}
results = append(results, string(event.Value))
if len(results) == len(inputs) {
assert.Equal(t, results, inputs)
return
}
case <-time.After(10 * getShortTimeout()):
t.Fatalf("Timeout reached (results=%v)", results)
return
}
}
Expand Down

0 comments on commit 083d82c

Please sign in to comment.