Files
pages/internal/ordoschnellic/dispatcher_test.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

151 lines
5.1 KiB
Go

package ordoschnellic
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
type queueFixture struct {
items []*PushDelivery
calls []string
errAt string
err error
finished []Outcome
finishContext func(context.Context)
at time.Time
t *testing.T
}
func (q *queueFixture) call(op string, at time.Time) error {
q.t.Helper()
if !at.Equal(q.at) {
q.t.Fatalf("clock for %s: %v", op, at)
}
q.calls = append(q.calls, op)
if op == q.errAt {
return q.err
}
return nil
}
func (q *queueFixture) Cleanup(_ context.Context, at time.Time) error { return q.call("cleanup", at) }
func (q *queueFixture) Claim(_ context.Context, at time.Time) (*PushDelivery, error) {
if err := q.call("claim", at); err != nil {
return nil, err
}
if len(q.items) == 0 {
return nil, nil
}
d := q.items[0]
q.items = q.items[1:]
return d, nil
}
func (q *queueFixture) DeferFocused(_ context.Context, _ *PushDelivery, at time.Time) error {
return q.call("defer", at)
}
func (q *queueFixture) Finish(ctx context.Context, _ *PushDelivery, o Outcome, at time.Time) error {
if q.finishContext != nil {
q.finishContext(ctx)
}
q.finished = append(q.finished, o)
return q.call("finish", at)
}
type senderFixture func(context.Context, *PushDelivery) Outcome
func (f senderFixture) Send(ctx context.Context, d *PushDelivery) Outcome { return f(ctx, d) }
func newQueueFixture(t *testing.T) *queueFixture {
return &queueFixture{t: t, at: time.Unix(1800000000, 0), err: errors.New("queue failure")}
}
func TestDispatcherBoundedBatchAndCounters(t *testing.T) {
q := newQueueFixture(t)
q.items = append(q.items, &PushDelivery{Focused: true}, &PushDelivery{})
for i := 0; i < 25; i++ {
q.items = append(q.items, &PushDelivery{Subscription: "valid"})
}
sends := 0
sender := senderFixture(func(context.Context, *PushDelivery) Outcome {
sends++
return Outcome{Attempted: true, Success: sends%2 == 0, Retry: sends%2 != 0}
})
stats, err := (Dispatcher{Queue: q, Sender: sender, Now: func() time.Time { return q.at }}).Run(context.Background())
if err != nil || stats != (Stats{Sent: 9, Failed: 9}) || sends != 18 || len(q.items) != 7 || len(q.finished) != 19 {
t.Fatalf("stats=%+v sends=%d pending=%d finished=%d err=%v", stats, sends, len(q.items), len(q.finished), err)
}
if q.finished[0] != (Outcome{}) {
t.Fatal("ineligible delivery was sent/retried")
}
if fmt.Sprint(q.calls[:5]) != "[cleanup claim defer claim finish]" {
t.Fatal(q.calls)
}
}
func TestDispatcherStopsOnQueueErrors(t *testing.T) {
for _, op := range []string{"cleanup", "claim", "defer", "finish"} {
t.Run(op, func(t *testing.T) {
q := newQueueFixture(t)
q.errAt = op
q.items = []*PushDelivery{{Subscription: "valid", Focused: op == "defer"}, {Subscription: "valid"}}
sends := 0
_, err := (Dispatcher{Queue: q, Sender: senderFixture(func(context.Context, *PushDelivery) Outcome { sends++; return Outcome{Attempted: true, Success: true} }), Now: func() time.Time { return q.at }}).Run(context.Background())
if !errors.Is(err, q.err) || q.calls[len(q.calls)-1] != op || sends > 1 {
t.Fatalf("calls=%v sends=%d err=%v", q.calls, sends, err)
}
})
}
}
func TestDispatcherCancellationFinalizesClaim(t *testing.T) {
q := newQueueFixture(t)
q.items = []*PushDelivery{{Subscription: "valid"}, {Subscription: "valid"}}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
q.finishContext = func(ctx context.Context) {
if ctx.Err() != nil {
t.Fatal("completion inherited cancellation")
}
deadline, ok := ctx.Deadline()
if !ok || time.Until(deadline) > PushFinishTimeout {
t.Fatal("completion is not bounded")
}
}
stats, err := (Dispatcher{Queue: q, Now: func() time.Time { return q.at }, Sender: senderFixture(func(ctx context.Context, _ *PushDelivery) Outcome {
if _, ok := ctx.Deadline(); !ok {
t.Fatal("send has no deadline")
}
cancel()
return Outcome{Attempted: true, Retry: true}
})}).Run(ctx)
if !errors.Is(err, context.Canceled) || stats.Failed != 1 || len(q.finished) != 1 || len(q.items) != 1 || !q.finished[0].Retry {
t.Fatalf("%+v %v %+v", stats, err, q.finished)
}
q.calls = nil
_, err = (Dispatcher{Queue: q, Sender: senderFixture(func(context.Context, *PushDelivery) Outcome { t.Fatal("cancelled batch sent"); return Outcome{} })}).Run(ctx)
if !errors.Is(err, context.Canceled) || len(q.calls) != 0 {
t.Fatal("cancelled batch accessed queue", err, q.calls)
}
}
func TestDeliveryPolicy(t *testing.T) {
at := time.Unix(1800000000, 0)
for _, attempt := range []int{-1, 0, 1, 2, 3, 4, 5, 1000000} {
next, ok := RetryAt(attempt, at)
if ok != (attempt >= 1 && attempt <= 4) {
t.Fatalf("attempt %d", attempt)
}
if ok && next.Sub(at) != time.Duration(1<<attempt)*time.Minute {
t.Fatal("backoff", attempt, next)
}
}
for _, code := range []int{200, 201, 204, 301, 400, 401, 403, 404, 410, 429, 500, 503} {
o := ClassifyPushStatus(code)
if !o.Attempted || o.Success != (code >= 200 && code < 300) || o.Retry != (code == 429 || code >= 500) || o.Expired != (code == 404 || code == 410) {
t.Fatal(code, o)
}
}
}