fix: harden high-fanout delivery under load
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCallbackQueueMetricsTrackWaitingAndProcessing(t *testing.T) {
|
||||
metrics := &callbackQueueMetrics{}
|
||||
metrics.enqueue()
|
||||
metrics.enqueue()
|
||||
metrics.startCallback(time.Now().Add(-10 * time.Millisecond))
|
||||
|
||||
snapshot := metrics.snapshot()
|
||||
if snapshot.Current != 1 || snapshot.Processing != 1 || snapshot.Maximum != 2 || snapshot.Enqueued != 2 {
|
||||
t.Fatalf("unexpected active queue snapshot: %+v", snapshot)
|
||||
}
|
||||
if snapshot.WaitSecondsTotal <= 0 || snapshot.WaitSecondsMax <= 0 || metrics.isDrained() {
|
||||
t.Fatalf("wait or drain state not tracked: %+v", snapshot)
|
||||
}
|
||||
|
||||
metrics.finishCallback()
|
||||
metrics.startCallback(time.Now())
|
||||
metrics.finishCallback()
|
||||
if snapshot = metrics.snapshot(); snapshot.Current != 0 || snapshot.Processing != 0 || snapshot.Completed != 2 || !metrics.isDrained() {
|
||||
t.Fatalf("unexpected drained queue snapshot: %+v", snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWaitForCallbackQueueWaitsForProcessingCallback(t *testing.T) {
|
||||
metrics := &callbackQueueMetrics{}
|
||||
metrics.enqueue()
|
||||
metrics.startCallback(time.Now())
|
||||
go func() {
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
metrics.finishCallback()
|
||||
}()
|
||||
|
||||
started := time.Now()
|
||||
if err := waitForCallbackQueue(context.Background(), metrics, time.Second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if time.Since(started) < 15*time.Millisecond {
|
||||
t.Fatal("wait returned before the callback finished")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user