65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
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")
|
|
}
|
|
}
|
|
|
|
func TestWaitForDeliveryQuiescenceResetsForLateDelivery(t *testing.T) {
|
|
recorder := newDeliveryRecorder("run", "a", 1)
|
|
queue := &callbackQueueMetrics{}
|
|
go func() {
|
|
time.Sleep(20 * time.Millisecond)
|
|
recorder.received.Add(1)
|
|
}()
|
|
|
|
started := time.Now()
|
|
if err := waitForDeliveryQuiescence(context.Background(), recorder, queue, 150*time.Millisecond, time.Second); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if elapsed := time.Since(started); elapsed < 250*time.Millisecond {
|
|
t.Fatalf("quiet period was not reset after late delivery: elapsed=%s", elapsed)
|
|
}
|
|
}
|