Skip to content

Commit 4d27a42

Browse files
authored
Merge pull request #206 from lzhfromustc/4_Fatal_misusages
Fix the misusage of t.Fatal() in 4 test functions
2 parents a74531a + 59e65f9 commit 4d27a42

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

db_test.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,31 @@ func TestOpen_MultipleGoroutines(t *testing.T) {
7373
path := tempfile()
7474
defer os.RemoveAll(path)
7575
var wg sync.WaitGroup
76+
errCh := make(chan error, iterations*instances)
7677
for iteration := 0; iteration < iterations; iteration++ {
7778
for instance := 0; instance < instances; instance++ {
7879
wg.Add(1)
7980
go func() {
8081
defer wg.Done()
8182
db, err := bolt.Open(path, 0600, nil)
8283
if err != nil {
83-
t.Fatal(err)
84+
errCh <- err
85+
return
8486
}
8587
if err := db.Close(); err != nil {
86-
t.Fatal(err)
88+
errCh <- err
89+
return
8790
}
8891
}()
8992
}
9093
wg.Wait()
9194
}
95+
close(errCh)
96+
for err := range errCh {
97+
if err != nil {
98+
t.Fatalf("error from inside goroutine: %v", err)
99+
}
100+
}
92101
}
93102

94103
// Ensure that opening a database with a blank path returns an error.
@@ -428,19 +437,20 @@ func TestDB_Open_InitialMmapSize(t *testing.T) {
428437
t.Fatal(err)
429438
}
430439

431-
done := make(chan struct{})
440+
done := make(chan error, 1)
432441

433442
go func() {
434-
if err := wtx.Commit(); err != nil {
435-
t.Fatal(err)
436-
}
437-
done <- struct{}{}
443+
err := wtx.Commit()
444+
done <- err
438445
}()
439446

440447
select {
441448
case <-time.After(5 * time.Second):
442449
t.Errorf("unexpected that the reader blocks writer")
443-
case <-done:
450+
case err := <-done:
451+
if err != nil {
452+
t.Fatal(err)
453+
}
444454
}
445455

446456
if err := rtx.Rollback(); err != nil {
@@ -698,18 +708,19 @@ func testDB_Close_PendingTx(t *testing.T, writable bool) {
698708
}
699709

700710
// Open update in separate goroutine.
701-
done := make(chan struct{})
711+
done := make(chan error, 1)
702712
go func() {
703-
if err := db.Close(); err != nil {
704-
t.Fatal(err)
705-
}
706-
close(done)
713+
err := db.Close()
714+
done <- err
707715
}()
708716

709717
// Ensure database hasn't closed.
710718
time.Sleep(100 * time.Millisecond)
711719
select {
712-
case <-done:
720+
case err := <-done:
721+
if err != nil {
722+
t.Errorf("error from inside goroutine: %v", err)
723+
}
713724
t.Fatal("database closed too early")
714725
default:
715726
}
@@ -727,7 +738,10 @@ func testDB_Close_PendingTx(t *testing.T, writable bool) {
727738
// Ensure database closed now.
728739
time.Sleep(100 * time.Millisecond)
729740
select {
730-
case <-done:
741+
case err := <-done:
742+
if err != nil {
743+
t.Fatalf("error from inside goroutine: %v", err)
744+
}
731745
default:
732746
t.Fatal("database did not close")
733747
}
@@ -1520,6 +1534,7 @@ func BenchmarkDBBatchManual10x100(b *testing.B) {
15201534
for i := 0; i < b.N; i++ {
15211535
start := make(chan struct{})
15221536
var wg sync.WaitGroup
1537+
errCh := make(chan error, 10)
15231538

15241539
for major := 0; major < 10; major++ {
15251540
wg.Add(1)
@@ -1542,13 +1557,18 @@ func BenchmarkDBBatchManual10x100(b *testing.B) {
15421557
}
15431558
return nil
15441559
}
1545-
if err := db.Update(insert100); err != nil {
1546-
b.Fatal(err)
1547-
}
1560+
err := db.Update(insert100)
1561+
errCh <- err
15481562
}(uint32(major))
15491563
}
15501564
close(start)
15511565
wg.Wait()
1566+
close(errCh)
1567+
for err := range errCh {
1568+
if err != nil {
1569+
b.Fatal(err)
1570+
}
1571+
}
15521572
}
15531573

15541574
b.StopTimer()

0 commit comments

Comments
 (0)