package printqueue_test import ( "context" "fmt" "slices" "strings" "testing" "uberbringer/internal/pkg/printqueue" ) // A unique job submitted twice is queued once: the second Submit answers // with the first job, also once its status is gone. The same reference on // a job that is not unique, or from another source, is queued again. func TestUniqueJobQueuedOnce(t *testing.T) { var logs lockedBuffer store := newTestStore(printqueue.NewMemoryStore()) sender := &fakeSender{} q := newQueueOn(store, fakeRenderer{}, sender, slogTo(&logs), printqueue.Options{}) meta := printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Unique: true} first, err := q.Submit(job("card"), meta) if err != nil { t.Fatal(err) } again, err := q.Submit(job("card"), meta) if err != nil || again.ID != first.ID || again.State != printqueue.StateQueued { t.Errorf("second Submit = %+v, %v; want the first job", again, err) } if q.Pending() != 1 { t.Errorf("Pending = %d, want 1", q.Pending()) } if !strings.Contains(logs.String(), "job_id="+first.ID.String()) || !strings.Contains(logs.String(), "not queued again") { t.Errorf("log %q, want the repeat logged with the earlier job", logs.String()) } plain, _ := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Ref: "msg1"}) other, _ := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:work", Ref: "msg1", Unique: true}) noRef, _ := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Unique: true}) noRefAgain, _ := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Unique: true}) ids := []printqueue.JobID{first.ID, plain.ID, other.ID, noRef.ID, noRefAgain.ID} slices.SortFunc(ids, func(a, b printqueue.JobID) int { return strings.Compare(a.String(), b.String()) }) if len(slices.Compact(ids)) != 5 || q.Pending() != 5 { t.Errorf("Pending = %d; want a job each for the plain, the other source and the two without a reference", q.Pending()) } want := []commit{ {op: "add", seq: 1, state: printqueue.StoredQueued, synced: true}, {op: "add", seq: 2, state: printqueue.StoredQueued}, {op: "add", seq: 3, state: printqueue.StoredQueued, synced: true}, {op: "add", seq: 4, state: printqueue.StoredQueued}, {op: "add", seq: 5, state: printqueue.StoredQueued}, } if got := store.Commits(); !slices.Equal(got, want) { t.Errorf("commits %+v, want %+v: unique inserts synced", got, want) } } // The duplicate check outlives the status: with History 0 the first job's // status goes once it prints, and a repeat still gets its ID. func TestUniqueJobAfterItsStatusIsGone(t *testing.T) { sender := &fakeSender{} q := printqueue.New(fakeRenderer{}, sender, printqueue.NewMemoryStore(), discardLogger(), printqueue.Options{Size: 10, History: 0}) startQueue(t, q) meta := printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Unique: true} first, _ := q.Submit(job("card"), meta) waitFor(t, func() bool { return len(sender.Sent()) == 1 }) waitFor(t, func() bool { _, err := q.Status(first.ID); return err != nil }) again, err := q.Submit(job("card"), meta) if err != nil || again != (printqueue.Status{ID: first.ID, Source: "ntfy:home"}) { t.Errorf("Submit after the status went = %+v, %v; want only the first job's ID and source", again, err) } if q.Pending() != 0 || len(sender.Sent()) != 1 { t.Errorf("pending %d, sent %v; want nothing more", q.Pending(), sender.Sent()) } } // The references are kept KeptRefs per source: the oldest is forgotten. func TestUniqueRefsKeptPerSource(t *testing.T) { q := newQueueOn(printqueue.NewMemoryStore(), fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{Size: 2 * printqueue.KeptRefs}) var ids []printqueue.JobID for i := range printqueue.KeptRefs + 1 { st, err := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Ref: fmt.Sprint(i), Unique: true}) if err != nil { t.Fatal(err) } ids = append(ids, st.ID) } if st, _ := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Ref: "1", Unique: true}); st.ID != ids[1] { t.Errorf("reference 1 gave %s, want the job it named, %s", st.ID, ids[1]) } if st, _ := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Ref: "0", Unique: true}); st.ID == ids[0] { t.Error("the oldest reference was still kept") } } // A unique job queued before a restart is found after it, so an ntfy // message replayed after a crash prints once. func TestUniqueJobAcrossARestart(t *testing.T) { store := printqueue.NewDurableMemoryStore() meta := printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Unique: true} before := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{}) first, _ := before.Submit(job("card"), meta) sender := &fakeSender{} q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{}) if err := q.Restore(context.Background()); err != nil { t.Fatal(err) } again, err := q.Submit(job("card"), meta) if err != nil || again.ID != first.ID { t.Errorf("Submit after the restart = %+v, %v; want the job queued before it", again, err) } startQueue(t, q) waitForState(t, q, first.ID, printqueue.StateDone) if sent := sender.Sent(); !slices.Equal(sent, []string{"card"}) { t.Errorf("sent %v, want the card once", sent) } } // Holds answers for the kept references of unique jobs, also after a // restart, and for nothing else. func TestHolds(t *testing.T) { store := printqueue.NewDurableMemoryStore() q := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{}) if _, err := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Ref: "msg1", Unique: true}); err != nil { t.Fatal(err) } if _, err := q.Submit(job("card"), printqueue.Meta{Source: "ntfy:home", Ref: "req1"}); err != nil { t.Fatal(err) } after := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{}) if err := after.Restore(context.Background()); err != nil { t.Fatal(err) } for _, queue := range []*printqueue.Queue{q, after} { if !queue.Holds("ntfy:home", "msg1") || queue.Holds("ntfy:home", "req1") || queue.Holds("ntfy:work", "msg1") || queue.Holds("ntfy:home", "msg2") { t.Error("Holds answers other than for the unique job's source and reference") } } }