|
| 1 | +package babelqueue |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestIsReplayAndBypassExternalEffects(t *testing.T) { |
| 11 | + bg := context.Background() |
| 12 | + if IsReplay(bg) { |
| 13 | + t.Error("a plain context must not be a replay") |
| 14 | + } |
| 15 | + rp := withReplay(bg) |
| 16 | + if !IsReplay(rp) { |
| 17 | + t.Error("a withReplay context must be a replay") |
| 18 | + } |
| 19 | + |
| 20 | + ran := false |
| 21 | + if err := BypassExternalEffects(bg, func() error { ran = true; return nil }); err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + if !ran { |
| 25 | + t.Error("fn must run when not a replay") |
| 26 | + } |
| 27 | + |
| 28 | + ran = false |
| 29 | + if err := BypassExternalEffects(rp, func() error { ran = true; return errors.New("must not run") }); err != nil { |
| 30 | + t.Fatalf("a replay must skip fn and return nil, got %v", err) |
| 31 | + } |
| 32 | + if ran { |
| 33 | + t.Error("fn must be skipped on a replay") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestInMemoryTransportHeadersRoundTrip(t *testing.T) { |
| 38 | + tr := NewInMemoryTransport() |
| 39 | + if err := tr.PublishWithHeaders(context.Background(), "q", "body", map[string]string{HeaderReplayBypass: "1"}); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + msg, _ := tr.Pop(context.Background(), "q", 0) |
| 43 | + if msg == nil || msg.Headers[HeaderReplayBypass] != "1" { |
| 44 | + t.Fatalf("headers not carried through Pop: %+v", msg) |
| 45 | + } |
| 46 | + _ = tr.Publish(context.Background(), "q", "plain") |
| 47 | + m2, _ := tr.Pop(context.Background(), "q", 0) |
| 48 | + if m2.Headers[HeaderReplayBypass] != "" { |
| 49 | + t.Error("a plain Publish must carry no replay header") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestRedriveBypassStampsHeaderAndConsumeSkipsEffects(t *testing.T) { |
| 54 | + tr := NewInMemoryTransport() |
| 55 | + deadLettered(t, tr, "orders.dlq", "urn:babel:orders:created", "orders", map[string]any{"order_id": 1}) |
| 56 | + |
| 57 | + res, err := Redrive(context.Background(), tr, "orders.dlq", RedriveOptions{Bypass: true}) |
| 58 | + if err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + if res.Redriven != 1 || len(res.Items) != 1 || !res.Items[0].Bypassed { |
| 62 | + t.Fatalf("expected one bypassed redrive: %+v", res) |
| 63 | + } |
| 64 | + |
| 65 | + msg, _ := tr.Pop(context.Background(), "orders", 0) |
| 66 | + if msg == nil || msg.Headers[HeaderReplayBypass] != "1" { |
| 67 | + t.Fatalf("redriven message is missing the bypass header: %+v", msg) |
| 68 | + } |
| 69 | + |
| 70 | + emailed := false |
| 71 | + app := NewApp(tr) |
| 72 | + app.Handle("urn:babel:orders:created", func(ctx context.Context, _ Envelope) error { |
| 73 | + if !IsReplay(ctx) { |
| 74 | + t.Error("the handler should see this delivery as a replay") |
| 75 | + } |
| 76 | + return BypassExternalEffects(ctx, func() error { emailed = true; return nil }) |
| 77 | + }) |
| 78 | + app.dispatch(context.Background(), msg) |
| 79 | + if emailed { |
| 80 | + t.Error("the external side-effect must be skipped on a bypassed replay") |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestRedriveBypassWithoutHeaderSupportFallsBack(t *testing.T) { |
| 85 | + base := NewInMemoryTransport() |
| 86 | + deadLettered(t, base, "dlq", "urn:babel:orders:created", "orders", nil) |
| 87 | + tr := plainTransport{inner: base} |
| 88 | + |
| 89 | + res, err := Redrive(context.Background(), tr, "dlq", RedriveOptions{Bypass: true}) |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + if res.Redriven != 1 || res.Items[0].Bypassed { |
| 94 | + t.Fatalf("Bypass must be a no-op when the transport is not a HeaderPublisher: %+v", res) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// plainTransport wraps InMemoryTransport but exposes only the base Transport methods, so it is |
| 99 | +// deliberately NOT a HeaderPublisher. |
| 100 | +type plainTransport struct{ inner *InMemoryTransport } |
| 101 | + |
| 102 | +func (p plainTransport) Publish(ctx context.Context, q, b string) error { |
| 103 | + return p.inner.Publish(ctx, q, b) |
| 104 | +} |
| 105 | + |
| 106 | +func (p plainTransport) Pop(ctx context.Context, q string, d time.Duration) (*ReceivedMessage, error) { |
| 107 | + return p.inner.Pop(ctx, q, d) |
| 108 | +} |
| 109 | + |
| 110 | +func (p plainTransport) Ack(ctx context.Context, m *ReceivedMessage) error { |
| 111 | + return p.inner.Ack(ctx, m) |
| 112 | +} |
0 commit comments