// TestBatch_MultipleBatches ensures that a batcher correctly processes multiple batches. func TestBatch_MultipleBatches(t *testing.T) { batchSize := 2 batcher := tsdb.NewPointBatcher(batchSize, 100*time.Millisecond) if batcher == nil { t.Fatal("failed to create batcher for size test") } batcher.Start() var p tsdb.Point var b []tsdb.Point batcher.In() <- p batcher.In() <- p b = <-batcher.Out() // Batch threshold reached. if len(b) != batchSize { t.Errorf("received batch (size) has incorrect length exp %d, got %d", batchSize, len(b)) } batcher.In() <- p b = <-batcher.Out() // Timeout triggered. if len(b) != 1 { t.Errorf("received batch (timeout) has incorrect length exp %d, got %d", 1, len(b)) } checkPointBatcherStats(t, batcher, -1, 3, 1, 1) }
// TestBatch_Flush ensures that a batcher generates a batch when flushed func TestBatch_Flush(t *testing.T) { batchSize := 2 batcher := tsdb.NewPointBatcher(batchSize, time.Hour) if batcher == nil { t.Fatal("failed to create batcher for flush test") } batcher.Start() var p tsdb.Point go func() { batcher.In() <- p batcher.Flush() }() batch := <-batcher.Out() if len(batch) != 1 { t.Errorf("received batch has incorrect length exp %d, got %d", 1, len(batch)) } checkPointBatcherStats(t, batcher, -1, 1, 0, 0) }
// TestBatch_Size ensures that a batcher generates a batch when the timeout triggers. func TestBatch_Timeout(t *testing.T) { batchSize := 5 batcher := tsdb.NewPointBatcher(batchSize+1, 100*time.Millisecond) if batcher == nil { t.Fatal("failed to create batcher for timeout test") } batcher.Start() var p tsdb.Point go func() { for i := 0; i < batchSize; i++ { batcher.In() <- p } }() batch := <-batcher.Out() if len(batch) != batchSize { t.Errorf("received batch has incorrect length exp %d, got %d", batchSize, len(batch)) } checkPointBatcherStats(t, batcher, -1, batchSize, 0, 1) }
// TestBatch_Size ensures that a buffered batcher generates a batch when the size threshold is reached. func TestBatch_SizeBuffered(t *testing.T) { batchSize := 5 batcher := tsdb.NewPointBatcher(batchSize, 5, time.Hour) if batcher == nil { t.Fatal("failed to create batcher for size test") } batcher.Start() var p models.Point go func() { for i := 0; i < batchSize; i++ { batcher.In() <- p } }() batch := <-batcher.Out() if len(batch) != batchSize { t.Errorf("received batch has incorrect length exp %d, got %d", batchSize, len(batch)) } checkPointBatcherStats(t, batcher, -1, batchSize, 1, 0) }