This repository was archived by the owner on Mar 9, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-3
lines changed Expand file tree Collapse file tree 2 files changed +41
-3
lines changed Original file line number Diff line number Diff line change @@ -22,15 +22,14 @@ func (db *DB) Batch(fn func(*Tx) error) error {
22
22
errCh := make (chan error , 1 )
23
23
24
24
db .batchMu .Lock ()
25
+
25
26
if db .batch == nil {
26
27
db .batch = & batch {
27
28
db : db ,
28
29
}
29
30
db .batch .timer = time .AfterFunc (db .MaxBatchDelay , db .batch .trigger )
30
31
}
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 })
34
33
if len (db .batch .calls ) >= db .MaxBatchSize {
35
34
// wake up batch, it's ready to run
36
35
go db .batch .trigger ()
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package bolt_test
2
2
3
3
import (
4
4
"testing"
5
+ "time"
5
6
6
7
"github.com/boltdb/bolt"
7
8
)
@@ -72,3 +73,41 @@ func TestDB_Batch_Panic(t *testing.T) {
72
73
t .Fatalf ("wrong error: %v != %v" , g , e )
73
74
}
74
75
}
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
+ }
You can’t perform that action at this time.
0 commit comments