From ffb3450ea41c9678f4b80f9c58f5504e98ee3e8d Mon Sep 17 00:00:00 2001 From: Ibrahim Jarif Date: Wed, 20 Nov 2019 18:29:17 +0530 Subject: [PATCH] Fix TestGoroutineLeak and Example_subscribe test (#1123) Both the test use subscription API and there was a race condition that would cause the builds to fail at times. This commit fixes it. --- db_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/db_test.go b/db_test.go index 7cdae8c52..dcd5f9747 100644 --- a/db_test.go +++ b/db_test.go @@ -1581,10 +1581,7 @@ func TestGoroutineLeak(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) var wg sync.WaitGroup wg.Add(1) - var subWg sync.WaitGroup - subWg.Add(1) go func() { - subWg.Done() err := db.Subscribe(ctx, func(kvs *pb.KVList) { require.Equal(t, []byte("value"), kvs.Kv[0].GetValue()) updated = true @@ -1594,7 +1591,8 @@ func TestGoroutineLeak(t *testing.T) { require.Equal(t, err.Error(), context.Canceled.Error()) } }() - subWg.Wait() + // Wait for the go routine to be scheduled. + time.Sleep(time.Second) err := db.Update(func(txn *Txn) error { return txn.SetEntry(NewEntry([]byte("key"), []byte("value"))) }) @@ -1947,6 +1945,8 @@ func ExampleDB_Subscribe() { log.Printf("subscription closed") }() + // Wait for the above go routine to be scheduled. + time.Sleep(time.Second) // Write both keys, but only one should be printed in the Output. err = db.Update(func(txn *Txn) error { return txn.Set(aKey, aValue) }) if err != nil {