Skip to content

Commit 3e560db

Browse files
authored
Merge pull request #445 from ahrtr/fix_testDB_Close_PendingTx_20230330
test: improve testDB_Close_PendingTx to reduce flaky
2 parents 1bc0c6b + 7b07e70 commit 3e560db

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

db_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,15 @@ func testDB_Close_PendingTx(t *testing.T, writable bool) {
747747
}
748748

749749
// Open update in separate goroutine.
750+
startCh := make(chan struct{}, 1)
750751
done := make(chan error, 1)
751752
go func() {
753+
startCh <- struct{}{}
752754
err := db.Close()
753755
done <- err
754756
}()
757+
// wait for the above goroutine to get scheduled.
758+
<-startCh
755759

756760
// Ensure database hasn't closed.
757761
time.Sleep(100 * time.Millisecond)
@@ -775,14 +779,13 @@ func testDB_Close_PendingTx(t *testing.T, writable bool) {
775779
}
776780

777781
// Ensure database closed now.
778-
time.Sleep(100 * time.Millisecond)
779782
select {
780783
case err := <-done:
781784
if err != nil {
782785
t.Fatalf("error from inside goroutine: %v", err)
783786
}
784-
default:
785-
t.Fatal("database did not close")
787+
case <-time.After(5 * time.Second):
788+
t.Fatalf("database did not close")
786789
}
787790
}
788791

0 commit comments

Comments
 (0)