Files
pages/internal/dregarnuhr/runner_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

186 lines
4.8 KiB
Go

package dregarnuhr
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
)
type finishedJob struct {
id string
err error
at time.Time
}
type fakeQueue struct {
mu sync.Mutex
settings Settings
jobs []Job
claimed int
cancelled bool
cancelErr error
finished chan finishedJob
claimAt time.Time
}
func newFakeQueue(concurrency int) *fakeQueue {
q := &fakeQueue{settings: Settings{Concurrency: concurrency}, finished: make(chan finishedJob, 100)}
for n := 0; n < 64; n++ {
q.jobs = append(q.jobs, Job{UUID: fmt.Sprint(n), Type: "synthetic"})
}
return q
}
func (q *fakeQueue) Settings(context.Context) (Settings, error) {
q.mu.Lock()
defer q.mu.Unlock()
return q.settings, nil
}
func (q *fakeQueue) Claim(_ context.Context, at time.Time) (*Job, error) {
q.mu.Lock()
defer q.mu.Unlock()
if len(q.jobs) == 0 {
return nil, ErrNoJob
}
j := q.jobs[0]
q.jobs = q.jobs[1:]
q.claimed++
q.claimAt = at
return &j, nil
}
func (q *fakeQueue) Cancellation(context.Context, string) (bool, error) {
q.mu.Lock()
defer q.mu.Unlock()
return q.cancelled, q.cancelErr
}
func (q *fakeQueue) Started(context.Context, *Job) error { return nil }
func (q *fakeQueue) Finish(ctx context.Context, j *Job, err error, at time.Time) error {
if ctx.Err() != nil {
return ctx.Err()
}
q.finished <- finishedJob{j.UUID, err, at}
return nil
}
func TestRunnerConcurrentAdmission(t *testing.T) {
for _, foreground := range []bool{false, true} {
t.Run(fmt.Sprint(foreground), func(t *testing.T) {
q := newFakeQueue(4)
reg := &Registry{}
block := make(chan struct{})
started := make(chan struct{}, 4)
wakes := make(chan struct{}, 4)
clock := newFakeClock()
defer close(block)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := reg.Register("synthetic", func(ctx context.Context, _ *Job) error {
started <- struct{}{}
select {
case <-ctx.Done():
return ctx.Err()
case <-block:
return nil
}
}); err != nil {
t.Fatal(err)
}
r := NewRunner(q, reg, RunnerOptions{Clock: clock, Foreground: func() bool { return foreground }, Wake: func() { wakes <- struct{}{} }})
var wg sync.WaitGroup
for n := 0; n < 16; n++ {
wg.Add(1)
go func() { defer wg.Done(); r.Dispatch(ctx) }()
}
wg.Wait()
want := 4
if foreground {
want = 1
}
for n := 0; n < want; n++ {
receive(t, started)
}
if r.Running() != int64(want) || q.claimed != want || !q.claimAt.Equal(clock.Now()) {
t.Fatalf("active=%d claimed=%d want=%d", r.Running(), q.claimed, want)
}
cancel()
for n := 0; n < want; n++ {
receive(t, q.finished)
receive(t, wakes)
}
if r.Running() != 0 {
t.Fatal("worker slots leaked")
}
})
}
}
func TestRunnerOutcomesAndCancellation(t *testing.T) {
for _, mode := range []string{"success", "cancel-before", "cancel-after", "cancellation-read-error", "panic", "unknown", "failure"} {
t.Run(mode, func(t *testing.T) {
q := newFakeQueue(1)
q.jobs = q.jobs[:1]
q.cancelled = mode == "cancel-before"
if mode == "cancellation-read-error" {
q.cancelErr = errors.New("database unavailable")
}
reg := &Registry{}
called := false
if mode != "unknown" {
if err := reg.Register("synthetic", func(context.Context, *Job) error {
called = true
if mode == "panic" {
panic("sensitive panic payload")
}
if mode == "failure" {
return errors.New("handler failure")
}
if mode == "cancel-after" {
q.mu.Lock()
q.cancelled = true
q.mu.Unlock()
}
return nil
}); err != nil {
t.Fatal(err)
}
}
wakes := make(chan struct{}, 1)
clock := newFakeClock()
r := NewRunner(q, reg, RunnerOptions{Clock: clock, Wake: func() { wakes <- struct{}{} }})
r.Dispatch(context.Background())
out := receive(t, q.finished)
receive(t, wakes)
if (out.err == nil) != (mode == "success") {
t.Fatal(mode, out.err)
}
if (mode == "cancel-before" || mode == "cancellation-read-error") && called {
t.Fatal("handler ran without cancellation check")
}
if mode == "panic" && out.err.Error() != "event handler panicked" {
t.Fatal("panic payload leaked")
}
if r.Running() != 0 || !out.at.Equal(clock.Now()) {
t.Fatal("completion/clock failure")
}
})
}
}
func TestRegistryAndPausedRunner(t *testing.T) {
r := &Registry{}
h := func(context.Context, *Job) error { return nil }
if r.Register("", h) == nil || r.Register("nil", nil) == nil {
t.Fatal("invalid registration")
}
if r.Register("synthetic", h) != nil || r.Register("synthetic", h) == nil {
t.Fatal("duplicate replaced")
}
q := newFakeQueue(3)
q.settings.Paused = true
NewRunner(q, r, RunnerOptions{}).Dispatch(context.Background())
if q.claimed != 0 {
t.Fatal("paused claim")
}
}