Skip to content

Commit

Permalink
Fix TestGoroutineLeak and Example_subscribe test (#1123)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ibrahim Jarif authored Nov 20, 2019
1 parent eef7c12 commit ffb3450
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")))
})
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ffb3450

Please sign in to comment.