forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// TestOutboxPreservesTraceContext verifies the durable handoff retains the incoming trace parent.
|
||||
func TestOutboxPreservesTraceContext(t *testing.T) {
|
||||
s := testStore(t)
|
||||
a := fixtureAttachment(t, s)
|
||||
parent := "00-11111111111111111111111111111111-2222222222222222-01"
|
||||
ctx := propagation.TraceContext{}.Extract(t.Context(), propagation.MapCarrier{"traceparent": parent})
|
||||
if !trace.SpanContextFromContext(ctx).IsValid() {
|
||||
t.Fatal("invalid fixture context")
|
||||
}
|
||||
if _, err := s.QueueAttachment(ctx, a.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jobs, err := s.Due(ctx, 1)
|
||||
if err != nil || len(jobs) != 1 || jobs[0].Traceparent != parent {
|
||||
t.Fatal("durable trace carrier missing")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSnapshotObservesCurrentBacklog checks unfinished accepted work rather than delivery count.
|
||||
func TestSnapshotObservesCurrentBacklog(t *testing.T) {
|
||||
s := testStore(t)
|
||||
_, job := queuedJob(t, s)
|
||||
_, err := s.pool.Exec(t.Context(), `UPDATE attachment_jobs SET created_at=clock_timestamp()-interval '10 seconds' WHERE id=@id`, pgx.StrictNamedArgs{"id": job.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot, err := s.Snapshot(t.Context())
|
||||
if err != nil || snapshot.QueueDepth != 1 || snapshot.OldestSeconds < 10 {
|
||||
t.Fatal("incorrect accepted backlog")
|
||||
}
|
||||
lease, err := s.Claim(t.Context(), job, time.Minute, 5)
|
||||
if err != nil || lease == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, err := s.Complete(t.Context(), *lease, "original", nil); err != nil || !ok {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot, err = s.Snapshot(t.Context())
|
||||
if err != nil || snapshot.QueueDepth != 0 || snapshot.OldestSeconds != 0 {
|
||||
t.Fatal("completed work remains in backlog")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSnapshotCancellationBoundsObservation prevents metrics collection from blocking on unavailable SQL.
|
||||
func TestSnapshotCancellationBoundsObservation(t *testing.T) {
|
||||
s := testStore(t)
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
if _, err := s.Snapshot(ctx); err == nil {
|
||||
t.Fatal("canceled snapshot succeeded")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user