Skip to content

Commit

Permalink
update 4b90b35 for these changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sharnoff committed Jan 29, 2025
1 parent cad0b27 commit 5fdd4ad
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions pkg/reporting/batcher_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
package reporting

import (
"bytes"
"testing"

"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)

type csvBatchBuilder struct {
buf bytes.Buffer
started bool
}

func (b *csvBatchBuilder) Add(event string) {
if b.started {
b.buf.Write([]byte{','})
}
b.buf.Write([]byte(event))
b.started = true
}

func (b *csvBatchBuilder) Finish() []byte {
return b.buf.Bytes()
}

func TestEventBatching(t *testing.T) {
targetBatchSize := 3

Expand All @@ -16,7 +34,14 @@ func TestEventBatching(t *testing.T) {
}
gauge := prometheus.NewGauge(prometheus.GaugeOpts{})

batcher := newEventBatcher[string](targetBatchSize, notify, gauge)
newBatch := func() BatchBuilder[string] {
return &csvBatchBuilder{
buf: bytes.Buffer{},
started: false,
}
}

batcher := newEventBatcher(targetBatchSize, newBatch, notify, gauge)

// First batch:
// Add a small number of items to the batch, and then explicitly request early completion.
Expand All @@ -29,7 +54,9 @@ func TestEventBatching(t *testing.T) {
batcher.finishOngoing()
// check that this batch was completed:
assert.Equal(t, true, notified)
assert.Equal(t, []batch[string]{{events: []string{"b1-1", "b1-2"}}}, batcher.peekCompleted())
assert.Equal(t, []batch[string]{
{count: 2, serialized: []byte("b1-1,b1-2")},
}, batcher.peekCompleted())
// clear the current batch:
notified = false
batcher.dropCompleted(1)
Expand All @@ -51,8 +78,8 @@ func TestEventBatching(t *testing.T) {
notified = false // reset the notification
// check that the batches so far match what we expect:
assert.Equal(t, []batch[string]{
{events: []string{"b2-1", "b2-2", "b2-3"}},
{events: []string{"b3-1", "b3-2", "b3-3"}},
{count: 3, serialized: []byte("b2-1,b2-2,b2-3")},
{count: 3, serialized: []byte("b3-1,b3-2,b3-3")},
}, batcher.peekCompleted())
// add the last batch:
batcher.enqueue("b4-1")
Expand All @@ -62,16 +89,16 @@ func TestEventBatching(t *testing.T) {
assert.Equal(t, true, notified)
// Check that the final batches are what we expect
assert.Equal(t, []batch[string]{
{events: []string{"b2-1", "b2-2", "b2-3"}},
{events: []string{"b3-1", "b3-2", "b3-3"}},
{events: []string{"b4-1", "b4-2", "b4-3"}},
{count: 3, serialized: []byte("b2-1,b2-2,b2-3")},
{count: 3, serialized: []byte("b3-1,b3-2,b3-3")},
{count: 3, serialized: []byte("b4-1,b4-2,b4-3")},
}, batcher.peekCompleted())
// Consume one batch:
batcher.dropCompleted(1)
// and now, it should just be b3 and b4:
assert.Equal(t, []batch[string]{
{events: []string{"b3-1", "b3-2", "b3-3"}},
{events: []string{"b4-1", "b4-2", "b4-3"}},
{count: 3, serialized: []byte("b3-1,b3-2,b3-3")},
{count: 3, serialized: []byte("b4-1,b4-2,b4-3")},
}, batcher.peekCompleted())
// consume the final two:
batcher.dropCompleted(2)
Expand Down

0 comments on commit 5fdd4ad

Please sign in to comment.