Skip to content

Commit ff2a0ec

Browse files
author
Steven.Osborne
committed
Check batch channel length when draining channel
1 parent d7124bd commit ff2a0ec

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

batcher/batcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (b *basicBatcher) Dispose() {
167167
b.items = nil
168168

169169
// Drain the batch channel and all routines waiting to put on the channel
170-
for atomic.LoadInt32(&b.waiting) > 0 {
170+
for len(b.batchChan) > 0 || atomic.LoadInt32(&b.waiting) > 0 {
171171
<-b.batchChan
172172
}
173173
close(b.batchChan)

batcher/batcher_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func TestMultiConsumer(t *testing.T) {
137137

138138
func TestDispose(t *testing.T) {
139139
assert := assert.New(t)
140-
b, err := New(1, 2, 100000, 10, func(str interface{}) uint {
140+
b, err := New(1, 2, 100000, 2, func(str interface{}) uint {
141141
return uint(len(str.(string)))
142142
})
143143
assert.Nil(err)
@@ -157,12 +157,18 @@ func TestDispose(t *testing.T) {
157157
assert.Nil(err)
158158

159159
b.Put("d")
160+
b.Put("e")
161+
b.Put("f")
162+
b.Put("g")
163+
b.Put("h")
164+
b.Put("i")
165+
160166
b.Dispose()
161167

162168
_, err = b.Get()
163169
assert.Equal(ErrDisposed, err)
164170

165-
assert.Equal(ErrDisposed, b.Put("e"))
171+
assert.Equal(ErrDisposed, b.Put("j"))
166172
assert.Equal(ErrDisposed, b.Flush())
167173

168174
}

0 commit comments

Comments
 (0)