-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_runtime_test.go
More file actions
156 lines (136 loc) · 4.35 KB
/
app_runtime_test.go
File metadata and controls
156 lines (136 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package babelqueue_test
import (
"context"
"errors"
"testing"
"time"
babelqueue "github.com/babelqueue/babelqueue-go"
)
func TestAppConsumeStopsOnContextCancel(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr, babelqueue.WithDefaultQueue("q"), babelqueue.WithPollTimeout(time.Millisecond))
done := make(chan struct{})
app.Handle("urn:x", func(_ context.Context, _ babelqueue.Envelope) error {
close(done)
return nil
})
if _, err := app.Publish(context.Background(), "urn:x", map[string]any{"a": 1}); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error, 1)
go func() { errCh <- app.Consume(ctx) }()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("message was not consumed")
}
cancel()
if err := <-errCh; !errors.Is(err, context.Canceled) {
t.Fatalf("Consume returned %v, want context.Canceled", err)
}
}
func TestAppRunProcessesThenStops(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr, babelqueue.WithPollTimeout(time.Millisecond)) // default queue
done := make(chan struct{})
app.Handle("urn:y", func(_ context.Context, _ babelqueue.Envelope) error {
close(done)
return nil
})
if _, err := app.Publish(context.Background(), "urn:y", map[string]any{}); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error, 1)
go func() { errCh <- app.Run(ctx) }()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("message was not consumed by Run")
}
cancel()
<-errCh
}
func TestAppDeadLetterQueueOverride(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr,
babelqueue.WithDefaultQueue("orders"),
babelqueue.WithMaxAttempts(1),
babelqueue.WithDeadLetter(true),
babelqueue.WithDeadLetterQueue("graveyard"),
babelqueue.WithPollTimeout(time.Millisecond),
)
app.Handle("urn:boom", func(_ context.Context, _ babelqueue.Envelope) error {
return errors.New("boom")
})
if _, err := app.Publish(context.Background(), "urn:boom", map[string]any{}); err != nil {
t.Fatal(err)
}
if _, err := app.Drain(context.Background(), "orders", 0); err != nil {
t.Fatal(err)
}
if tr.Size("graveyard") != 1 {
t.Fatalf("explicit dead-letter queue size = %d, want 1", tr.Size("graveyard"))
}
if tr.Size("orders.dlq") != 0 {
t.Error("must not use the default suffix queue when an explicit DLQ is set")
}
}
func TestAppDeadLetterSuffixOverride(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr,
babelqueue.WithDefaultQueue("orders"),
babelqueue.WithMaxAttempts(1),
babelqueue.WithDeadLetter(true),
babelqueue.WithDeadLetterSuffix(".dead"),
)
app.Handle("urn:boom", func(_ context.Context, _ babelqueue.Envelope) error {
return errors.New("boom")
})
if _, err := app.Publish(context.Background(), "urn:boom", map[string]any{}); err != nil {
t.Fatal(err)
}
if _, err := app.Drain(context.Background(), "orders", 0); err != nil {
t.Fatal(err)
}
if tr.Size("orders.dead") != 1 {
t.Fatalf("custom-suffix dead-letter queue size = %d, want 1", tr.Size("orders.dead"))
}
}
func TestAppUnknownURNRelease(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr,
babelqueue.WithDefaultQueue("q"),
babelqueue.WithUnknownURNStrategy(babelqueue.StrategyRelease),
)
if _, err := app.Publish(context.Background(), "urn:unmapped", map[string]any{}); err != nil {
t.Fatal(err)
}
n, err := app.Drain(context.Background(), "q", 1)
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Fatalf("processed %d, want 1", n)
}
if tr.Size("q") != 1 {
t.Fatalf("release must re-publish to the same queue; size = %d, want 1", tr.Size("q"))
}
}
func TestAppUnknownURNFailRequeues(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr, babelqueue.WithDefaultQueue("q")) // default StrategyFail, maxAttempts 3
if _, err := app.Publish(context.Background(), "urn:unmapped", map[string]any{}); err != nil {
t.Fatal(err)
}
if _, err := app.Drain(context.Background(), "q", 1); err != nil {
t.Fatal(err)
}
// StrategyFail routes through the retry path: attempts 1 < 3 → requeued.
if tr.Size("q") != 1 {
t.Fatalf("fail strategy should requeue the message; size = %d, want 1", tr.Size("q"))
}
}