Skip to content

Commit

Permalink
Merge pull request #1 from FriendlyCaptcha/no-flush-empty-in-shutdown
Browse files Browse the repository at this point in the history
Never flush without items
  • Loading branch information
gzuidhof authored Nov 21, 2024
2 parents 7567839 + b9c6205 commit bc0c89b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (b *Batcher[T]) start(ctx context.Context,

// If the batcher is cancelled and the buffer is not empty, we want to flush the
// remaining items with the maximum batch size, so we skip until we reach max size or the buffer is empty.
skipFlush := isCancelled && len(b.buffer) > 0 && !isMaxSize
skipFlush := (isCancelled && len(b.buffer) > 0 && !isMaxSize) || len(items) == 0

if !skipFlush {
// We need to copy the slice to make sure that the slice that is passed is valid even if asynchronously
Expand Down
13 changes: 13 additions & 0 deletions batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,17 @@ func TestBatcherCancellation(t *testing.T) {
assert.ErrorIs(t, batcher.Push(0), ErrBatcherStopped)
assert.Equal(t, 0, batcher.CurrentBufferSize())
})

t.Run("no empty flush after cancellation", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())

_, flusher := startWithMockFlusher(ctx, t, New[int]())

cancel()

time.Sleep(time.Millisecond * 5)

assert.Equal(t, 0, flusher.StartCount())
})
}

0 comments on commit bc0c89b

Please sign in to comment.