Skip to content

Commit ffb3450

Browse files
author
Ibrahim Jarif
authored
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.
1 parent eef7c12 commit ffb3450

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

db_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,10 +1581,7 @@ func TestGoroutineLeak(t *testing.T) {
15811581
ctx, cancel := context.WithCancel(context.Background())
15821582
var wg sync.WaitGroup
15831583
wg.Add(1)
1584-
var subWg sync.WaitGroup
1585-
subWg.Add(1)
15861584
go func() {
1587-
subWg.Done()
15881585
err := db.Subscribe(ctx, func(kvs *pb.KVList) {
15891586
require.Equal(t, []byte("value"), kvs.Kv[0].GetValue())
15901587
updated = true
@@ -1594,7 +1591,8 @@ func TestGoroutineLeak(t *testing.T) {
15941591
require.Equal(t, err.Error(), context.Canceled.Error())
15951592
}
15961593
}()
1597-
subWg.Wait()
1594+
// Wait for the go routine to be scheduled.
1595+
time.Sleep(time.Second)
15981596
err := db.Update(func(txn *Txn) error {
15991597
return txn.SetEntry(NewEntry([]byte("key"), []byte("value")))
16001598
})
@@ -1947,6 +1945,8 @@ func ExampleDB_Subscribe() {
19471945
log.Printf("subscription closed")
19481946
}()
19491947

1948+
// Wait for the above go routine to be scheduled.
1949+
time.Sleep(time.Second)
19501950
// Write both keys, but only one should be printed in the Output.
19511951
err = db.Update(func(txn *Txn) error { return txn.Set(aKey, aValue) })
19521952
if err != nil {

0 commit comments

Comments
 (0)