Skip to content
This repository was archived by the owner on Mar 9, 2019. It is now read-only.

Commit ee8e42e

Browse files
committed
Handle Batch full
1 parent 6931794 commit ee8e42e

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

batch.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ func (db *DB) Batch(fn func(*Tx) error) error {
2222
errCh := make(chan error, 1)
2323

2424
db.batchMu.Lock()
25+
2526
if db.batch == nil {
2627
db.batch = &batch{
2728
db: db,
2829
}
2930
db.batch.timer = time.AfterFunc(db.MaxBatchDelay, db.batch.trigger)
3031
}
31-
if len(db.batch.calls) < db.MaxBatchSize {
32-
db.batch.calls = append(db.batch.calls, call{fn: fn, err: errCh})
33-
}
32+
db.batch.calls = append(db.batch.calls, call{fn: fn, err: errCh})
3433
if len(db.batch.calls) >= db.MaxBatchSize {
3534
// wake up batch, it's ready to run
3635
go db.batch.trigger()

batch_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bolt_test
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/boltdb/bolt"
78
)
@@ -72,3 +73,41 @@ func TestDB_Batch_Panic(t *testing.T) {
7273
t.Fatalf("wrong error: %v != %v", g, e)
7374
}
7475
}
76+
77+
func TestDB_BatchFull(t *testing.T) {
78+
db := NewTestDB()
79+
defer db.Close()
80+
db.MustCreateBucket([]byte("widgets"))
81+
82+
n := 3
83+
// high enough to never trigger here
84+
db.MaxBatchDelay = 1 * time.Hour
85+
// make sure everything will not fit in one batch
86+
db.MaxBatchSize = n - 1
87+
ch := make(chan error)
88+
for i := 0; i < n; i++ {
89+
go func(i int) {
90+
ch <- db.Batch(func(tx *bolt.Tx) error {
91+
return tx.Bucket([]byte("widgets")).Put(ui64tob(uint64(i)), []byte{})
92+
})
93+
}(i)
94+
}
95+
96+
// Check all responses to make sure there's no error.
97+
for i := 0; i < n; i++ {
98+
if err := <-ch; err != nil {
99+
t.Fatal(err)
100+
}
101+
}
102+
103+
// Ensure data is correct.
104+
db.MustView(func(tx *bolt.Tx) error {
105+
b := tx.Bucket([]byte("widgets"))
106+
for i := 0; i < n; i++ {
107+
if v := b.Get(ui64tob(uint64(i))); v == nil {
108+
t.Errorf("key not found: %d", i)
109+
}
110+
}
111+
return nil
112+
})
113+
}

0 commit comments

Comments
 (0)