package printqueue_test import ( "context" "encoding/json" "errors" "testing" "uberbringer/internal/pkg/printqueue" ) // contentKept reports whether store still has job seq's content. func contentKept(t *testing.T, store printqueue.Store, seq uint64) bool { t.Helper() _, err := store.Job(context.Background(), seq) if err != nil && !errors.Is(err, printqueue.ErrContentGone) { t.Fatal(err) } return err == nil } // Finished jobs keep their content newest first within ContentBytes. An // uncertain job keeps its content, uncounted, while its status stays, and // an unfinished job always keeps its own. func TestRetentionByContentBytes(t *testing.T) { ctx := context.Background() content, _ := json.Marshal(job("aaaa")) size := int64(len(content)) store := printqueue.NewDurableMemoryStore() uncertain := storeJob(t, store, 1, newJobID(t), job("a long receipt that may have printed"), printqueue.DefaultPrinter) sending := uncertain.Deliveries[0] sending.Attempts = 1 if err := store.Sending(ctx, sending); err != nil { t.Fatal(err) } sender := &fakeSender{gate: make(chan struct{})} q := newQueueOn(store, fakeRenderer{}, sender, discardLogger(), printqueue.Options{History: 10, ContentBytes: 2*size + size/2}) if err := q.Restore(ctx); err != nil { t.Fatal(err) } startQueue(t, q) var ids []printqueue.JobID for range 4 { st, err := q.Submit(job("aaaa"), printqueue.Meta{}) if err != nil { t.Fatal(err) } ids = append(ids, st.ID) } for _, id := range ids { sender.gate <- struct{}{} waitForState(t, q, id, printqueue.StateDone) } waiting, _ := q.Submit(job("aaaa"), printqueue.Meta{}) waitFor(t, func() bool { st, _ := q.Status(waiting.ID); return st.State == printqueue.StatePrinting }) for seq, want := range map[uint64]bool{1: true, 2: false, 3: false, 4: true, 5: true, 6: true} { if got := contentKept(t, store, seq); got != want { t.Errorf("job %d keeps its content: %v, want %v", seq, got, want) } } for _, id := range append(ids, uncertain.ID) { if _, err := q.Status(id); err != nil { t.Errorf("status of %s: %v; dropping content must keep it", id, err) } } close(sender.gate) waitForState(t, q, waiting.ID, printqueue.StateDone) } // An uncertain job's content goes with its status. func TestRetentionDropsUncertainContentWithItsStatus(t *testing.T) { ctx := context.Background() store := printqueue.NewDurableMemoryStore() uncertain := storeJob(t, store, 1, newJobID(t), job("x"), printqueue.DefaultPrinter) if err := store.Sending(ctx, uncertain.Deliveries[0]); err != nil { t.Fatal(err) } q := newQueueOn(store, fakeRenderer{}, &fakeSender{}, discardLogger(), printqueue.Options{History: 1, ContentBytes: 1 << 20}) if err := q.Restore(ctx); err != nil { t.Fatal(err) } if !contentKept(t, store, 1) { t.Fatal("the uncertain job lost its content at the restart") } startQueue(t, q) st, _ := q.Submit(job("y"), printqueue.Meta{}) waitForState(t, q, st.ID, printqueue.StateDone) if _, err := q.Status(uncertain.ID); !errors.Is(err, printqueue.ErrNotFound) { t.Errorf("uncertain job's status: %v, want it gone past History", err) } if contentKept(t, store, 1) || !contentKept(t, store, 2) { t.Error("want the uncertain job's content gone with its status, and the new job's kept") } }