Skip to content

Commit

Permalink
Rewrite testWatch
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Dec 17, 2021
1 parent 09f8a0a commit 7978b10
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutils
import (
"fmt"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -146,11 +147,8 @@ func testPutGetDeleteExists(t *testing.T, kv store.Store) {

func testWatch(t *testing.T, kv store.Store) {
key := "testWatch"
value := []byte("world")
newValue := []byte("world!")

// Put the key
err := kv.Put(key, value, nil)
err := kv.Put(key, []byte("0"), nil)
assert.NoError(t, err)

stopCh := make(<-chan struct{})
Expand All @@ -160,40 +158,22 @@ func testWatch(t *testing.T, kv store.Store) {

// Update loop
go func() {
timeout := time.After(1 * time.Second)
tick := time.Tick(250 * time.Millisecond)
for {
select {
case <-timeout:
return
case <-tick:
err := kv.Put(key, newValue, nil)
if assert.NoError(t, err) {
continue
}
for i := 1; i < 5; i++ {
err := kv.Put(key, []byte(strconv.Itoa(i)), nil)
if !assert.NoError(t, err) {
return
}
time.Sleep(500 * time.Microsecond)
}
}()

// Check for updates
eventCount := 1
for {
for i := 0; i < 5; i++ {
select {
case event := <-events:
assert.NotNil(t, event)
if eventCount == 1 {
assert.Equal(t, event.Key, key)
assert.Equal(t, event.Value, value)
} else {
assert.Equal(t, event.Key, key)
assert.Equal(t, event.Value, newValue)
}
eventCount++
// We received all the events we wanted to check
if eventCount >= 4 {
return
}
assert.Equal(t, event.Key, key)
assert.Equal(t, event.Value, []byte(strconv.Itoa(i)))
case <-time.After(getShortTimeout()):
t.Fatal("Timeout reached")
return
Expand Down

0 comments on commit 7978b10

Please sign in to comment.