Skip to content

Commit

Permalink
Fix return value from Batcher.Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 31, 2023
1 parent e141e1d commit cb488fd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions syncx/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ func (b *Batcher[T]) Start() {
}()
}

// Queue queues the given value, potentially blocking. Returns the new free capacity.
// Queue queues the given value, potentially blocking. Returns the new free capacity (batch + buffer).
func (b *Batcher[T]) Queue(value T) int {
b.buffer <- value

return cap(b.buffer) - len(b.buffer)
return (cap(b.batch) + cap(b.buffer)) - (len(b.batch) + len(b.buffer))
}

// Stop stops this batcher.
Expand All @@ -91,6 +91,7 @@ func (b *Batcher[T]) flush() {
}
}

// processes everything in the batch and buffer until they're both empty
func (b *Batcher[T]) drain() {
for len(b.buffer) > 0 || len(b.batch) > 0 {
buffSize := len(b.buffer)
Expand Down
2 changes: 1 addition & 1 deletion syncx/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestBatcher(t *testing.T) {

b.Start()

b.Queue(1) // won't trigger a batch
assert.Equal(t, 4, b.Queue(1)) // won't trigger a batch

time.Sleep(time.Millisecond * 100)
assert.Equal(t, [][]int{}, batches)
Expand Down

0 comments on commit cb488fd

Please sign in to comment.