A card from an ntfy subscription now carries its notification, "<topic>/<sequence ID>", in its Meta, and the queue stores it with the job. ntfy's sequence IDs are per topic and one subscription reads several topics, so the topic is part of the key; a message without a sequence_id is its own sequence. Digests and jobs from the routes carry none. Nothing reads it yet: it lets an ntfy message_delete find the waiting job of its notification, also after a restart, once cancelling comes. Part of stage 1c of docs/plans/job-store.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
620 lines
20 KiB
Go
620 lines
20 KiB
Go
package printqueue_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"uberbringer/internal/pkg/printqueue"
|
|
"uberbringer/internal/pkg/receipt"
|
|
)
|
|
|
|
// commit is one write a testStore committed.
|
|
type commit struct {
|
|
op string // add, sending or finish
|
|
seq uint64 // the job's for add, the delivery's otherwise
|
|
state printqueue.StoredState
|
|
synced bool
|
|
}
|
|
|
|
// testStore wraps a Store. It records each commit and whether it was
|
|
// synced, fails the next call of a method on demand, holds the calls of a
|
|
// method while blocked, and can rewrite the content Job returns.
|
|
type testStore struct {
|
|
printqueue.Store
|
|
|
|
mu sync.Mutex
|
|
commits []commit
|
|
fail map[string]error
|
|
block map[string]*gate
|
|
content func([]byte) []byte
|
|
}
|
|
|
|
// gate holds the calls of a blocked method: entered gets a value as each
|
|
// call arrives, and open closes to let them through.
|
|
type gate struct {
|
|
entered chan struct{}
|
|
open chan struct{}
|
|
}
|
|
|
|
func newTestStore(s printqueue.Store) *testStore {
|
|
return &testStore{Store: s, fail: make(map[string]error), block: make(map[string]*gate)}
|
|
}
|
|
|
|
// failNext makes the next call of op (add, sending, finish, job, idle or
|
|
// load) fail with err.
|
|
func (s *testStore) failNext(op string, err error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.fail[op] = err
|
|
}
|
|
|
|
// blockOn holds every call of op until release is called. entered gets a
|
|
// value as each call arrives.
|
|
func (s *testStore) blockOn(op string) (entered <-chan struct{}, release func()) {
|
|
g := &gate{entered: make(chan struct{}, 10), open: make(chan struct{})}
|
|
s.mu.Lock()
|
|
s.block[op] = g
|
|
s.mu.Unlock()
|
|
return g.entered, sync.OnceFunc(func() {
|
|
s.mu.Lock()
|
|
delete(s.block, op)
|
|
s.mu.Unlock()
|
|
close(g.open)
|
|
})
|
|
}
|
|
|
|
// before waits while op is blocked, then returns the error its call is to
|
|
// fail with, if any.
|
|
func (s *testStore) before(op string) error {
|
|
s.mu.Lock()
|
|
err := s.fail[op]
|
|
delete(s.fail, op)
|
|
g := s.block[op]
|
|
s.mu.Unlock()
|
|
if g != nil {
|
|
g.entered <- struct{}{}
|
|
<-g.open
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s *testStore) record(c commit) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.commits = append(s.commits, c)
|
|
}
|
|
|
|
// Commits returns the commits so far, in order.
|
|
func (s *testStore) Commits() []commit {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return slices.Clone(s.commits)
|
|
}
|
|
|
|
func (s *testStore) Load(ctx context.Context) (printqueue.Loaded, error) {
|
|
if err := s.before("load"); err != nil {
|
|
return printqueue.Loaded{}, err
|
|
}
|
|
return s.Store.Load(ctx)
|
|
}
|
|
|
|
func (s *testStore) Add(ctx context.Context, r printqueue.Record, job []byte, sync bool) error {
|
|
if err := s.before("add"); err != nil {
|
|
return err
|
|
}
|
|
if err := s.Store.Add(ctx, r, job, sync); err != nil {
|
|
return err
|
|
}
|
|
s.record(commit{op: "add", seq: r.Seq, state: printqueue.StoredQueued, synced: sync})
|
|
return nil
|
|
}
|
|
|
|
func (s *testStore) Sending(ctx context.Context, d printqueue.Delivery) error {
|
|
if err := s.before("sending"); err != nil {
|
|
return err
|
|
}
|
|
if err := s.Store.Sending(ctx, d); err != nil {
|
|
return err
|
|
}
|
|
s.record(commit{op: "sending", seq: d.Seq, state: printqueue.StoredSending, synced: true})
|
|
return nil
|
|
}
|
|
|
|
func (s *testStore) Finish(ctx context.Context, d printqueue.Delivery, p printqueue.Prune, sync bool) error {
|
|
if err := s.before("finish"); err != nil {
|
|
return err
|
|
}
|
|
if err := s.Store.Finish(ctx, d, p, sync); err != nil {
|
|
return err
|
|
}
|
|
s.record(commit{op: "finish", seq: d.Seq, state: d.State, synced: sync})
|
|
return nil
|
|
}
|
|
|
|
func (s *testStore) Job(ctx context.Context, seq uint64) ([]byte, error) {
|
|
if err := s.before("job"); err != nil {
|
|
return nil, err
|
|
}
|
|
data, err := s.Store.Job(ctx, seq)
|
|
s.mu.Lock()
|
|
rewrite := s.content
|
|
s.mu.Unlock()
|
|
if err == nil && rewrite != nil {
|
|
data = rewrite(data)
|
|
}
|
|
return data, err
|
|
}
|
|
|
|
func (s *testStore) Idle(ctx context.Context) error {
|
|
if err := s.before("idle"); err != nil {
|
|
return err
|
|
}
|
|
return s.Store.Idle(ctx)
|
|
}
|
|
|
|
// runQueue runs q in the background. stop cancels Run's context and
|
|
// returns what Run returned; done is closed once Run has returned.
|
|
func runQueue(t *testing.T, q *printqueue.Queue) (stop func() error, done <-chan struct{}) {
|
|
t.Helper()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
finished := make(chan struct{})
|
|
var err error
|
|
go func() {
|
|
defer close(finished)
|
|
err = q.Run(ctx)
|
|
}()
|
|
stop = func() error {
|
|
cancel()
|
|
<-finished
|
|
return err
|
|
}
|
|
t.Cleanup(func() { _ = stop() })
|
|
return stop, finished
|
|
}
|
|
|
|
// waitClosed waits for done to close or to give a value.
|
|
func waitClosed(t *testing.T, done <-chan struct{}, what string) {
|
|
t.Helper()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatalf("%s did not happen", what)
|
|
}
|
|
}
|
|
|
|
var errDisk = errors.New("disk I/O error")
|
|
|
|
// The sending mark and a failure are synced; the insert of a job from the
|
|
// routes and its done are not. A failed dial writes no sending mark.
|
|
func TestCommitsAndSyncs(t *testing.T) {
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
sender := &fakeSender{failures: 1}
|
|
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{Retries: 1})
|
|
startQueue(t, q)
|
|
|
|
first, _ := q.Submit(job("first"), printqueue.Meta{})
|
|
waitForState(t, q, first.ID, printqueue.StateDone)
|
|
sender.mu.Lock()
|
|
sender.failures = 2
|
|
sender.mu.Unlock()
|
|
second, _ := q.Submit(job("second"), printqueue.Meta{})
|
|
if st := waitForState(t, q, second.ID, printqueue.StateFailed); st.Attempts != 2 {
|
|
t.Errorf("second job %+v, want 2 attempts", st)
|
|
}
|
|
|
|
want := []commit{
|
|
{op: "add", seq: 1, state: printqueue.StoredQueued},
|
|
{op: "sending", seq: 1, state: printqueue.StoredSending, synced: true},
|
|
{op: "finish", seq: 1, state: printqueue.StoredDone},
|
|
{op: "add", seq: 2, state: printqueue.StoredQueued},
|
|
{op: "finish", seq: 2, state: printqueue.StoredFailed, synced: true},
|
|
}
|
|
if got := store.Commits(); !slices.Equal(got, want) {
|
|
t.Errorf("commits\n%+v\nwant\n%+v", got, want)
|
|
}
|
|
}
|
|
|
|
// sent_at is set by the first attempt that was about to write, and kept
|
|
// through later attempts; a failed dial sets nothing.
|
|
func TestSentAt(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
script []string
|
|
state printqueue.State
|
|
marks int
|
|
}{
|
|
{"printed", nil, printqueue.StateDone, 1},
|
|
{"after a failed dial", []string{"dial"}, printqueue.StateDone, 1},
|
|
{"never dialled", []string{"dial", "dial"}, printqueue.StateFailed, 0},
|
|
{"a write failed, then a dial", []string{"write", "dial"}, printqueue.StateFailed, 1},
|
|
{"a write failed, then printed", []string{"write"}, printqueue.StateDone, 2},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
sender := &fakeSender{script: tt.script}
|
|
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{Retries: 1})
|
|
startQueue(t, q)
|
|
before := time.Now().UTC()
|
|
st, _ := q.Submit(job("x"), printqueue.Meta{})
|
|
got := waitForState(t, q, st.ID, tt.state)
|
|
var marks int
|
|
for _, c := range store.Commits() {
|
|
if c.op == "sending" {
|
|
marks++
|
|
}
|
|
}
|
|
if marks != tt.marks {
|
|
t.Errorf("%d sending marks, want %d", marks, tt.marks)
|
|
}
|
|
if tt.marks == 0 {
|
|
if got.SentAt != nil {
|
|
t.Errorf("sent_at %v, want none", got.SentAt)
|
|
}
|
|
return
|
|
}
|
|
if got.SentAt == nil || got.SentAt.Before(before) || got.SentAt.Before(*got.StartedAt) || got.SentAt.After(*got.FinishedAt) {
|
|
t.Errorf("status %+v, want sent_at between started_at and finished_at", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The job the worker renders is the job submitted, read back from the
|
|
// store.
|
|
func TestJobRoundTripsThroughTheStore(t *testing.T) {
|
|
var submitted receipt.Job
|
|
body := `{"elements": [{"type": "text", "text": "Grüße", "align": "center", "bold": true, "size": 2},
|
|
{"type": "kv", "pairs": [{"key": "Total", "value": "£12.50", "bold": true}]},
|
|
{"type": "qr", "data": "https://example.com", "size": 6},
|
|
{"type": "box", "title": "T", "children": [{"type": "text", "text": "in a box"}]}], "copies": 2}`
|
|
if err := json.Unmarshal([]byte(body), &submitted); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rendered := make(chan receipt.Job, 1)
|
|
renderer := rendererFunc(func(job receipt.Job) ([]byte, error) {
|
|
rendered <- job
|
|
return []byte("x"), nil
|
|
})
|
|
q := newQueue(renderer, &fakeSender{}, printqueue.Options{})
|
|
startQueue(t, q)
|
|
if _, err := q.Submit(submitted, printqueue.Meta{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case got := <-rendered:
|
|
if !reflect.DeepEqual(got, submitted) {
|
|
t.Errorf("rendered %+v\nwant %+v", got, submitted)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("the job was not rendered")
|
|
}
|
|
}
|
|
|
|
// rendererFunc is a Renderer.
|
|
type rendererFunc func(receipt.Job) ([]byte, error)
|
|
|
|
func (f rendererFunc) Render(_ context.Context, job receipt.Job) ([]byte, error) {
|
|
return f(job)
|
|
}
|
|
|
|
// When Add fails, Submit returns the error, nothing is queued, the queue
|
|
// stops and Run returns the error; later jobs are refused as closed.
|
|
func TestStoreFailureOnAdd(t *testing.T) {
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
sender := &fakeSender{}
|
|
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
|
stop, done := runQueue(t, q)
|
|
|
|
store.failNext("add", errDisk)
|
|
if _, err := q.Submit(job("x"), printqueue.Meta{}); !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
|
t.Fatalf("Submit = %v, want the store's error marked ErrStoreFailed", err)
|
|
}
|
|
waitClosed(t, done, "Run's return")
|
|
if err := stop(); !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
|
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
|
}
|
|
if q.Pending() != 0 || len(sender.Sent()) != 0 {
|
|
t.Errorf("pending %d, sent %v; want nothing", q.Pending(), sender.Sent())
|
|
}
|
|
if _, err := q.Submit(job("y"), printqueue.Meta{}); !errors.Is(err, printqueue.ErrClosed) {
|
|
t.Errorf("Submit after the failure = %v, want ErrClosed", err)
|
|
}
|
|
}
|
|
|
|
// When the sending mark fails, nothing is written, the job never reads
|
|
// failed and the queue stops.
|
|
func TestStoreFailureOnSending(t *testing.T) {
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
sender := &fakeSender{}
|
|
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
|
store.failNext("sending", errDisk)
|
|
st, err := q.Submit(job("x"), printqueue.Meta{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stop, done := runQueue(t, q)
|
|
waitClosed(t, done, "Run's return")
|
|
if err := stop(); !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
|
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
|
}
|
|
if sent := sender.Sent(); len(sent) != 0 {
|
|
t.Errorf("sent %v after the mark failed", sent)
|
|
}
|
|
if got, _ := q.Status(st.ID); got.State != printqueue.StateQueued || got.Error != "" || got.StartedAt != nil {
|
|
t.Errorf("status %+v, want queued, as the store has it", got)
|
|
}
|
|
for _, c := range store.Commits() {
|
|
if c.op != "add" {
|
|
t.Errorf("commit %+v after the failed mark", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
// When Finish fails, the job keeps the state the store last recorded and
|
|
// the queue stops.
|
|
func TestStoreFailureOnFinish(t *testing.T) {
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
sender := &fakeSender{}
|
|
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
|
store.failNext("finish", errDisk)
|
|
st, _ := q.Submit(job("x"), printqueue.Meta{})
|
|
second, _ := q.Submit(job("y"), printqueue.Meta{})
|
|
stop, done := runQueue(t, q)
|
|
waitClosed(t, done, "Run's return")
|
|
if err := stop(); !errors.Is(err, errDisk) {
|
|
t.Errorf("Run = %v, want the store's error", err)
|
|
}
|
|
if got, _ := q.Status(st.ID); got.State != printqueue.StatePrinting || got.FinishedAt != nil {
|
|
t.Errorf("status %+v, want printing, as the store has it", got)
|
|
}
|
|
if got, _ := q.Status(second.ID); got.State != printqueue.StateQueued {
|
|
t.Errorf("waiting job %+v, want it left queued", got)
|
|
}
|
|
if sent := sender.Sent(); !slices.Equal(sent, []string{"x"}) {
|
|
t.Errorf("sent %v, want only the first job", sent)
|
|
}
|
|
}
|
|
|
|
// A failed read of a job's content or a failed Idle stops the queue too.
|
|
func TestStoreFailureOnReadOrIdle(t *testing.T) {
|
|
for _, op := range []string{"job", "idle"} {
|
|
t.Run(op, func(t *testing.T) {
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
q := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{})
|
|
store.failNext(op, errDisk)
|
|
var st printqueue.Status
|
|
if op == "job" {
|
|
var err error
|
|
if st, err = q.Submit(job("x"), printqueue.Meta{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
stop, done := runQueue(t, q)
|
|
waitClosed(t, done, "Run's return")
|
|
if err := stop(); !errors.Is(err, errDisk) {
|
|
t.Errorf("Run = %v, want the store's error", err)
|
|
}
|
|
if got, _ := q.Status(st.ID); op == "job" && (got.State != printqueue.StateQueued || got.StartedAt != nil) {
|
|
t.Errorf("status %+v, want queued, as the store has it", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Once a store error in Submit has stopped the queue, the job in hand
|
|
// writes nothing more to the store: it stays queued, as the store has it.
|
|
func TestStoreFailureLeavesTheJobInHandQueued(t *testing.T) {
|
|
store := newTestStore(printqueue.NewDurableMemoryStore())
|
|
started, release := make(chan struct{}, 1), make(chan struct{})
|
|
renderer := rendererFunc(func(j receipt.Job) ([]byte, error) {
|
|
started <- struct{}{}
|
|
<-release
|
|
return []byte(j.Elements[0].Text), nil
|
|
})
|
|
sender := &fakeSender{}
|
|
q := newQueueOn(store, renderer, sender, discardLogger(), printqueue.Options{})
|
|
first, _ := q.Submit(job("first"), printqueue.Meta{})
|
|
stop, done := runQueue(t, q)
|
|
waitClosed(t, started, "the rendering")
|
|
|
|
store.failNext("add", errDisk)
|
|
if _, err := q.Submit(job("second"), printqueue.Meta{}); !errors.Is(err, printqueue.ErrStoreFailed) {
|
|
t.Fatalf("Submit = %v, want the store's error marked ErrStoreFailed", err)
|
|
}
|
|
close(release)
|
|
waitClosed(t, done, "Run's return")
|
|
if err := stop(); !errors.Is(err, printqueue.ErrStoreFailed) {
|
|
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
|
}
|
|
for _, c := range store.Commits() {
|
|
if c.op != "add" {
|
|
t.Errorf("commit %+v after the store failed", c)
|
|
}
|
|
}
|
|
if sent := sender.Sent(); len(sent) != 0 {
|
|
t.Errorf("sent %v after the store failed", sent)
|
|
}
|
|
if got, _ := q.Status(first.ID); got.State != printqueue.StateQueued || got.StartedAt != nil {
|
|
t.Errorf("job in hand %+v, want queued, as the store has it", got)
|
|
}
|
|
}
|
|
|
|
// An insert that fails while shutdown waits for it stops the queue, and
|
|
// Run returns its error, with the memory store and with a durable one.
|
|
func TestStoreFailureDuringShutdownIsReturned(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
store printqueue.Store
|
|
}{
|
|
{"memory store", printqueue.NewMemoryStore()},
|
|
{"durable store", printqueue.NewDurableMemoryStore()},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
store := newTestStore(tt.store)
|
|
q := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{})
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
ran := make(chan error, 1)
|
|
go func() { ran <- q.Run(ctx) }()
|
|
|
|
inAdd, releaseAdd := store.blockOn("add")
|
|
defer releaseAdd()
|
|
store.failNext("add", errDisk)
|
|
submitted := make(chan error, 1)
|
|
go func() {
|
|
_, err := q.Submit(job("x"), printqueue.Meta{})
|
|
submitted <- err
|
|
}()
|
|
waitClosed(t, inAdd, "the insert")
|
|
cancel()
|
|
// The worker reaches shutdown, which waits for the insert.
|
|
time.Sleep(20 * time.Millisecond)
|
|
releaseAdd()
|
|
if err := <-submitted; !errors.Is(err, printqueue.ErrStoreFailed) {
|
|
t.Errorf("Submit = %v, want the store's error marked ErrStoreFailed", err)
|
|
}
|
|
if err := <-ran; !errors.Is(err, errDisk) || !errors.Is(err, printqueue.ErrStoreFailed) {
|
|
t.Errorf("Run = %v, want the store's error marked ErrStoreFailed", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// While a commit is held, Status and Pending still answer.
|
|
func TestStatusAnswersDuringACommit(t *testing.T) {
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
sender := &fakeSender{}
|
|
q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{})
|
|
startQueue(t, q)
|
|
|
|
inFinish, releaseFinish := store.blockOn("finish")
|
|
defer releaseFinish()
|
|
first, _ := q.Submit(job("first"), printqueue.Meta{})
|
|
waitClosed(t, inFinish, "the finish commit")
|
|
|
|
inAdd, releaseAdd := store.blockOn("add")
|
|
defer releaseAdd()
|
|
submitted := make(chan struct{})
|
|
go func() {
|
|
defer close(submitted)
|
|
_, _ = q.Submit(job("second"), printqueue.Meta{})
|
|
}()
|
|
waitClosed(t, inAdd, "the second insert")
|
|
|
|
answered := make(chan struct{})
|
|
go func() {
|
|
defer close(answered)
|
|
if st, err := q.Status(first.ID); err != nil || st.State != printqueue.StatePrinting {
|
|
t.Errorf("status %+v, %v during the held finish", st, err)
|
|
}
|
|
_ = q.Pending()
|
|
}()
|
|
waitClosed(t, answered, "Status and Pending answering")
|
|
releaseAdd()
|
|
waitClosed(t, submitted, "the second Submit")
|
|
releaseFinish()
|
|
waitForState(t, q, first.ID, printqueue.StateDone)
|
|
waitFor(t, func() bool { return q.Pending() == 0 && len(sender.Sent()) == 2 })
|
|
}
|
|
|
|
// A stored job that no longer decodes fails, logged by ID and never with
|
|
// its content.
|
|
func TestUndecodableJobFails(t *testing.T) {
|
|
var logs lockedBuffer
|
|
store := newTestStore(printqueue.NewMemoryStore())
|
|
store.content = func([]byte) []byte {
|
|
return []byte(`{"elements": [{"type": "text", "text": "secret words"}], "renamed_field": 1}`)
|
|
}
|
|
sender := &fakeSender{}
|
|
q := newQueueOn(store, fakeRenderer{}, sender, slog.New(slog.NewTextHandler(&logs, nil)), printqueue.Options{})
|
|
startQueue(t, q)
|
|
st, _ := q.Submit(job("secret words"), printqueue.Meta{Source: "print", Ref: "req_1"})
|
|
failed := waitForState(t, q, st.ID, printqueue.StateFailed)
|
|
if !strings.Contains(failed.Error, "no longer decodes") || len(sender.Sent()) != 0 {
|
|
t.Errorf("status %+v, sent %v", failed, sender.Sent())
|
|
}
|
|
out := logs.String()
|
|
if !strings.Contains(out, "job_id="+st.ID.String()) || !strings.Contains(out, "no longer decodes") {
|
|
t.Errorf("log %q, want the failure with the job's ID", out)
|
|
}
|
|
if strings.Contains(out, "secret") || strings.Contains(out, "renamed_field") {
|
|
t.Errorf("log %q quotes the stored job", out)
|
|
}
|
|
}
|
|
|
|
// Statuses beyond History go from the store with their deliveries in the
|
|
// commit that finishes the job pushing them out, and with no content
|
|
// budget a job's content goes in the commit that finishes it.
|
|
func TestHistoryPrunesTheStore(t *testing.T) {
|
|
inner := printqueue.NewMemoryStore()
|
|
q := newQueueOn(inner, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{History: 2})
|
|
startQueue(t, q)
|
|
var ids []printqueue.JobID
|
|
for range 4 {
|
|
st, _ := q.Submit(job("x"), printqueue.Meta{})
|
|
ids = append(ids, st.ID)
|
|
waitForState(t, q, st.ID, printqueue.StateDone)
|
|
}
|
|
loaded, err := inner.Load(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var kept []printqueue.JobID
|
|
for _, r := range loaded.Records {
|
|
kept = append(kept, r.ID)
|
|
}
|
|
if !slices.Equal(kept, ids[2:]) {
|
|
t.Errorf("store keeps %v, want the last two, %v", kept, ids[2:])
|
|
}
|
|
for seq := uint64(1); seq <= 4; seq++ {
|
|
if _, err := inner.Job(context.Background(), seq); !errors.Is(err, printqueue.ErrContentGone) {
|
|
t.Errorf("content of job %d: %v, want ErrContentGone", seq, err)
|
|
}
|
|
}
|
|
if r := loaded.Records[1]; r.Size != 0 {
|
|
t.Errorf("job 4 keeps %d bytes of content", r.Size)
|
|
}
|
|
}
|
|
|
|
// waitFor polls cond until it holds.
|
|
func waitFor(t *testing.T, cond func() bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for !cond() {
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("condition not met")
|
|
}
|
|
time.Sleep(2 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// A job's source, reference and ntfy notification are stored with it.
|
|
func TestMetaIsStored(t *testing.T) {
|
|
inner := printqueue.NewMemoryStore()
|
|
q := newQueueOn(inner, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{})
|
|
meta := printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Sequence: "alerts/backup"}
|
|
st, err := q.Submit(job("x"), meta)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
loaded, err := inner.Load(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(loaded.Records) != 1 {
|
|
t.Fatalf("records %+v", loaded.Records)
|
|
}
|
|
r := loaded.Records[0]
|
|
if r.ID != st.ID || r.Source != meta.Source || r.Ref != meta.Ref || r.Sequence != meta.Sequence || r.Unique {
|
|
t.Errorf("record %+v, want the job's meta", r)
|
|
}
|
|
}
|