Fix race condition in TestClient_WatchList and TestClient_WatchGet

This commit is contained in:
Kyle Havlovitz 2016-10-25 15:18:38 -07:00
parent e1d850c081
commit f6c971f46c
1 changed files with 9 additions and 0 deletions

View File

@ -244,6 +244,7 @@ func TestClient_WatchGet(t *testing.T) {
// Put the key // Put the key
value := []byte("test") value := []byte("test")
doneCh := make(chan struct{})
go func() { go func() {
kv := c.KV() kv := c.KV()
@ -252,6 +253,7 @@ func TestClient_WatchGet(t *testing.T) {
if _, err := kv.Put(p, nil); err != nil { if _, err := kv.Put(p, nil); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
doneCh <- struct{}{}
}() }()
// Get should work // Get should work
@ -272,6 +274,9 @@ func TestClient_WatchGet(t *testing.T) {
if meta2.LastIndex <= meta.LastIndex { if meta2.LastIndex <= meta.LastIndex {
t.Fatalf("unexpected value: %#v", meta2) t.Fatalf("unexpected value: %#v", meta2)
} }
// Block until put finishes to avoid a race between it and deferred s.Stop()
<-doneCh
} }
func TestClient_WatchList(t *testing.T) { func TestClient_WatchList(t *testing.T) {
@ -297,6 +302,7 @@ func TestClient_WatchList(t *testing.T) {
// Put the key // Put the key
value := []byte("test") value := []byte("test")
doneCh := make(chan struct{})
go func() { go func() {
kv := c.KV() kv := c.KV()
@ -305,6 +311,7 @@ func TestClient_WatchList(t *testing.T) {
if _, err := kv.Put(p, nil); err != nil { if _, err := kv.Put(p, nil); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
doneCh <- struct{}{}
}() }()
// Get should work // Get should work
@ -326,6 +333,8 @@ func TestClient_WatchList(t *testing.T) {
t.Fatalf("unexpected value: %#v", meta2) t.Fatalf("unexpected value: %#v", meta2)
} }
// Block until put finishes to avoid a race between it and deferred s.Stop()
<-doneCh
} }
func TestClient_Keys_DeleteRecurse(t *testing.T) { func TestClient_Keys_DeleteRecurse(t *testing.T) {