test: prove queued snapshot stays within byte budget
This commit is contained in:
@@ -358,6 +358,41 @@ func TestQueueSubmitOwnsEntrySnapshot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestQueueByteBudgetTracksOwnedEntrySnapshot(t *testing.T) {
|
||||||
|
batch := &fakeBatch{}
|
||||||
|
backend := newQueueBackend(func(context.Context, string) (logger.Batch, error) {
|
||||||
|
return batch, nil
|
||||||
|
})
|
||||||
|
entry := &logger.LogEntry{RequestID: "original", RequestBody: []byte("body")}
|
||||||
|
budget := logger.EstimatedBytes(entry)
|
||||||
|
q := logger.NewQueueWithBackend(backend, 2, 1, 1, time.Hour, budget)
|
||||||
|
|
||||||
|
q.Submit(entry)
|
||||||
|
entry.RequestID = "mutated"
|
||||||
|
entry.RequestBody = []byte("body expanded after submission")
|
||||||
|
q.Submit(&logger.LogEntry{RequestID: "x"})
|
||||||
|
|
||||||
|
if stats := q.Stats(); stats.Enqueued != 1 || stats.Dropped != 1 || stats.Bytes != budget {
|
||||||
|
t.Fatalf("before flush: %+v budget=%d", stats, budget)
|
||||||
|
}
|
||||||
|
|
||||||
|
q.Start(context.Background())
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := q.Stop(ctx); err != nil {
|
||||||
|
t.Fatalf("Stop: %v", err)
|
||||||
|
}
|
||||||
|
if len(batch.rows) != 1 {
|
||||||
|
t.Fatalf("written rows=%d want 1", len(batch.rows))
|
||||||
|
}
|
||||||
|
if row := batch.rows[0]; row[0] != "original" || row[6] != "body" {
|
||||||
|
t.Fatalf("written row does not match budgeted snapshot: %#v", row)
|
||||||
|
}
|
||||||
|
if stats := q.Stats(); stats.Bytes != 0 {
|
||||||
|
t.Fatalf("after flush: %+v", stats)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestQueueCanceledStopEventuallyReleasesAllBudget(t *testing.T) {
|
func TestQueueCanceledStopEventuallyReleasesAllBudget(t *testing.T) {
|
||||||
entry := &logger.LogEntry{RequestID: "queued"}
|
entry := &logger.LogEntry{RequestID: "queued"}
|
||||||
q := logger.NewQueue(nil, 32, 32, 1, time.Hour, 32*logger.EstimatedBytes(entry))
|
q := logger.NewQueue(nil, 32, 32, 1, time.Hour, 32*logger.EstimatedBytes(entry))
|
||||||
|
|||||||
Reference in New Issue
Block a user