From 083d82c45deb51651d50d28da31140ae1dce5a3c Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Mon, 20 Dec 2021 10:58:41 +0000 Subject: [PATCH] fixup! Rewrite `testWatch` --- testutils/utils.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/testutils/utils.go b/testutils/utils.go index 15f09af2..ca6e9205 100644 --- a/testutils/utils.go +++ b/testutils/utils.go @@ -3,7 +3,6 @@ package testutils import ( "fmt" "os" - "strconv" "testing" "time" @@ -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 } @@ -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 } }