Add the job store interface and its memory store
The print queue is to keep its jobs in a store that can outlive the process. printqueue now defines that Store: a job is a Record with one or more deliveries, one per printer, each with a stored state (queued, sending, done, failed or uncertain), its attempts, error and times. Add, Sending and Finish commit each change; Finish also drops the jobs, deliveries and content retention lets go of, in the same commit. Load turns every delivery found in sending into uncertain and says which. MemoryStore implements it in memory, for when no state directory is set. It refuses to mark a finished delivery again, so a bug that would resend a job shows up as an error. The references of unique jobs are kept per source, the newest 512, apart from the jobs themselves, for the duplicate check to come. Nothing uses the store yet. Part of stage 1c of docs/plans/job-store.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
50db21190b
commit
888470bbdc
3 files changed
+575
No files matched your search
@@ -0,0 +1,217 @@
|
||||
package printqueue_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.jetify.com/typeid"
|
||||
|
||||
"uberbringer/internal/pkg/printqueue"
|
||||
)
|
||||
|
||||
func newJobID(t *testing.T) printqueue.JobID {
|
||||
t.Helper()
|
||||
id, err := typeid.New[printqueue.JobID]()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// record returns job seq with one queued delivery per printer, numbered
|
||||
// from seq*10.
|
||||
func record(t *testing.T, seq uint64, printers ...string) printqueue.Record {
|
||||
t.Helper()
|
||||
r := printqueue.Record{
|
||||
Seq: seq, ID: newJobID(t), Source: "print", Ref: fmt.Sprintf("req_%d", seq),
|
||||
CreatedAt: time.Date(2026, 12, 1, 12, 0, 0, int(seq), time.UTC),
|
||||
}
|
||||
for i, p := range printers {
|
||||
r.Deliveries = append(r.Deliveries, printqueue.Delivery{Seq: seq*10 + uint64(i), Printer: p, State: printqueue.StoredQueued})
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func TestMemoryStoreKeepsRecordsAndContent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := printqueue.NewMemoryStore()
|
||||
if s.Durable() || !printqueue.NewDurableMemoryStore().Durable() {
|
||||
t.Error("only the test store reports itself durable")
|
||||
}
|
||||
r := record(t, 2, "default", "kitchen", "bar")
|
||||
r.Sequence = "alerts/seq1"
|
||||
if err := s.Add(ctx, r, []byte(`{"elements":[]}`), false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Add(ctx, record(t, 1, "default"), []byte(`{}`), false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Add(ctx, record(t, 2, "default"), nil, false); err == nil {
|
||||
t.Error("a second job 2 was stored")
|
||||
}
|
||||
|
||||
loaded, err := s.Load(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(loaded.Records) != 2 || loaded.Records[0].Seq != 1 || len(loaded.Uncertain) != 0 {
|
||||
t.Fatalf("loaded %+v, want both jobs in seq order", loaded)
|
||||
}
|
||||
got := loaded.Records[1]
|
||||
want := r
|
||||
want.Size = int64(len(`{"elements":[]}`))
|
||||
if got.ID != want.ID || got.Sequence != want.Sequence || got.Size != want.Size || got.CreatedAt != want.CreatedAt ||
|
||||
!slices.Equal(got.Deliveries, want.Deliveries) {
|
||||
t.Errorf("record %+v, want %+v", got, want)
|
||||
}
|
||||
|
||||
// The loaded record is a copy.
|
||||
loaded.Records[1].Deliveries[0].State = printqueue.StoredDone
|
||||
if again, _ := s.Load(ctx); again.Records[1].Deliveries[0].State != printqueue.StoredQueued {
|
||||
t.Error("changing a loaded record changed the store")
|
||||
}
|
||||
|
||||
content, err := s.Job(ctx, 2)
|
||||
if err != nil || string(content) != `{"elements":[]}` {
|
||||
t.Errorf("Job = %q, %v", content, err)
|
||||
}
|
||||
if _, err := s.Job(ctx, 9); !errors.Is(err, printqueue.ErrContentGone) {
|
||||
t.Errorf("Job of an unknown seq: %v, want ErrContentGone", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Load turns a delivery in sending into uncertain, once.
|
||||
func TestMemoryStoreLoadEndsSendsUncertain(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := printqueue.NewDurableMemoryStore()
|
||||
r := record(t, 1, "default", "kitchen")
|
||||
if err := s.Add(ctx, r, []byte(`{}`), false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
started := time.Date(2026, 12, 1, 12, 0, 1, 0, time.UTC)
|
||||
sending := r.Deliveries[1]
|
||||
sending.Attempts, sending.StartedAt, sending.SentAt = 2, started, started.Add(time.Second)
|
||||
if err := s.Sending(ctx, sending); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
loaded, err := s.Load(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !slices.Equal(loaded.Uncertain, []uint64{11}) {
|
||||
t.Errorf("uncertain %v, want delivery 11", loaded.Uncertain)
|
||||
}
|
||||
ds := loaded.Records[0].Deliveries
|
||||
if ds[0].State != printqueue.StoredQueued {
|
||||
t.Errorf("delivery 10 %+v, want it still queued", ds[0])
|
||||
}
|
||||
d := ds[1]
|
||||
if d.State != printqueue.StoredUncertain || d.Error != printqueue.InterruptedSend || d.FinishedAt.IsZero() ||
|
||||
d.Attempts != 2 || d.StartedAt != started || d.SentAt != started.Add(time.Second) {
|
||||
t.Errorf("delivery 11 %+v, want it uncertain with its attempt", d)
|
||||
}
|
||||
if again, _ := s.Load(ctx); len(again.Uncertain) != 0 || again.Records[0].Deliveries[1].State != printqueue.StoredUncertain {
|
||||
t.Errorf("second load %+v, want nothing turned again", again)
|
||||
}
|
||||
if err := s.Sending(ctx, d); err == nil {
|
||||
t.Error("an uncertain delivery was marked sending again")
|
||||
}
|
||||
}
|
||||
|
||||
// Finish records the final state and drops jobs, deliveries and content in
|
||||
// the same call.
|
||||
func TestMemoryStoreFinishPrunes(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := printqueue.NewMemoryStore()
|
||||
for seq := uint64(1); seq <= 3; seq++ {
|
||||
if err := s.Add(ctx, record(t, seq, "default", "kitchen"), []byte(`{"n":1}`), false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
done := printqueue.Delivery{Seq: 30, Printer: "default", State: printqueue.StoredDone, Attempts: 1,
|
||||
FinishedAt: time.Date(2026, 12, 1, 12, 0, 2, 0, time.UTC)}
|
||||
if err := s.Finish(ctx, done, printqueue.Prune{Jobs: []uint64{1}, Deliveries: []uint64{21}, Content: []uint64{2}}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Finish(ctx, done, printqueue.Prune{}, false); err == nil {
|
||||
t.Error("a finished delivery was finished again")
|
||||
}
|
||||
if err := s.Finish(ctx, printqueue.Delivery{Seq: 31, State: printqueue.StoredSending}, printqueue.Prune{}, false); err == nil {
|
||||
t.Error("Finish took a state that is not final")
|
||||
}
|
||||
if err := s.Finish(ctx, printqueue.Delivery{Seq: 10, State: printqueue.StoredDone}, printqueue.Prune{}, false); err == nil {
|
||||
t.Error("a delivery of a dropped job was finished")
|
||||
}
|
||||
|
||||
loaded, _ := s.Load(ctx)
|
||||
if len(loaded.Records) != 2 || loaded.Records[0].Seq != 2 {
|
||||
t.Fatalf("records %+v, want jobs 2 and 3", loaded.Records)
|
||||
}
|
||||
if r := loaded.Records[0]; r.Size != 0 || len(r.Deliveries) != 1 || r.Deliveries[0].Seq != 20 {
|
||||
t.Errorf("job 2 %+v, want no content and only delivery 20", r)
|
||||
}
|
||||
if r := loaded.Records[1]; r.Size == 0 || r.Deliveries[0] != done {
|
||||
t.Errorf("job 3 %+v, want its content and delivery 30 done", r)
|
||||
}
|
||||
if _, err := s.Job(ctx, 2); !errors.Is(err, printqueue.ErrContentGone) {
|
||||
t.Errorf("Job(2) = %v, want ErrContentGone", err)
|
||||
}
|
||||
if _, err := s.Job(ctx, 1); !errors.Is(err, printqueue.ErrContentGone) {
|
||||
t.Errorf("Job(1) = %v, want ErrContentGone", err)
|
||||
}
|
||||
}
|
||||
|
||||
// The references of unique jobs are kept, KeptRefs per source, apart from
|
||||
// the jobs themselves.
|
||||
func TestMemoryStoreKeepsRefsPerSource(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s := printqueue.NewMemoryStore()
|
||||
var first printqueue.UniqueRef
|
||||
for seq := uint64(1); seq <= printqueue.KeptRefs+2; seq++ {
|
||||
r := record(t, seq, "default")
|
||||
r.Source, r.Unique = "ntfy:home", true
|
||||
if seq == 3 {
|
||||
first = printqueue.UniqueRef{Source: r.Source, Ref: r.Ref, ID: r.ID}
|
||||
}
|
||||
if err := s.Add(ctx, r, nil, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := r.Deliveries[0]
|
||||
done.State = printqueue.StoredDone
|
||||
if err := s.Finish(ctx, done, printqueue.Prune{Jobs: []uint64{seq}}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
other := record(t, 1000, "default")
|
||||
other.Source, other.Unique = "ntfy:work", true
|
||||
if err := s.Add(ctx, other, nil, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Add(ctx, record(t, 1001, "default"), nil, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
loaded, _ := s.Load(ctx)
|
||||
if len(loaded.Refs) != printqueue.KeptRefs+1 {
|
||||
t.Fatalf("%d refs, want %d for ntfy:home and 1 for ntfy:work", len(loaded.Refs), printqueue.KeptRefs+1)
|
||||
}
|
||||
if loaded.Refs[0] != first || loaded.Refs[printqueue.KeptRefs].Source != "ntfy:work" {
|
||||
t.Errorf("refs start with %+v and end with %+v", loaded.Refs[0], loaded.Refs[printqueue.KeptRefs])
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoredStateFinished(t *testing.T) {
|
||||
for state, want := range map[printqueue.StoredState]bool{
|
||||
printqueue.StoredQueued: false, printqueue.StoredSending: false,
|
||||
printqueue.StoredDone: true, printqueue.StoredFailed: true, printqueue.StoredUncertain: true,
|
||||
} {
|
||||
if state.Finished() != want {
|
||||
t.Errorf("%s.Finished() = %v", state, !want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user