-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
160 lines (144 loc) · 4.66 KB
/
app_test.go
File metadata and controls
160 lines (144 loc) · 4.66 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
157
158
159
160
package babelqueue_test
import (
"context"
"errors"
"testing"
babelqueue "github.com/babelqueue/babelqueue-go"
)
func TestAppPublishAndConsume(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr, babelqueue.WithDefaultQueue("orders"))
var got babelqueue.Envelope
calls := 0
app.Handle("urn:babel:orders:created", func(_ context.Context, env babelqueue.Envelope) error {
calls++
got = env
return nil
})
id, err := app.Publish(context.Background(), "urn:babel:orders:created", map[string]any{"order_id": 1042})
if err != nil {
t.Fatalf("Publish: %v", err)
}
if id == "" {
t.Fatal("Publish must return the message id")
}
n, err := app.Drain(context.Background(), "orders", 0)
if err != nil {
t.Fatal(err)
}
if n != 1 || calls != 1 {
t.Fatalf("processed %d (calls=%d), want 1", n, calls)
}
if got.URN() != "urn:babel:orders:created" || got.Meta.Queue != "orders" {
t.Errorf("handler got %+v", got)
}
if oid, ok := got.Data["order_id"].(float64); !ok || oid != 1042 {
t.Errorf("data.order_id = %v", got.Data["order_id"])
}
if tr.Size("orders") != 0 {
t.Error("queue must be empty after a successful ack")
}
}
func TestAppRetriesThenDeadLetters(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr,
babelqueue.WithDefaultQueue("orders"),
babelqueue.WithMaxAttempts(3),
babelqueue.WithDeadLetter(true),
)
calls := 0
app.Handle("urn:babel:orders:created", func(_ context.Context, _ babelqueue.Envelope) error {
calls++
return errors.New("boom")
})
if _, err := app.Publish(context.Background(), "urn:babel:orders:created", map[string]any{"order_id": 1}); err != nil {
t.Fatal(err)
}
if _, err := app.Drain(context.Background(), "orders", 0); err != nil {
t.Fatal(err)
}
if calls != 3 {
t.Fatalf("handler called %d times, want 3 (maxAttempts)", calls)
}
if tr.Size("orders") != 0 {
t.Error("source queue must be empty")
}
if tr.Size("orders.dlq") != 1 {
t.Fatalf("expected 1 message on orders.dlq, got %d", tr.Size("orders.dlq"))
}
dl, err := tr.Pop(context.Background(), "orders.dlq", 0)
if err != nil || dl == nil {
t.Fatal("could not pop the dead-lettered message")
}
env, _ := babelqueue.Decode([]byte(dl.Body))
if env.DeadLetter == nil || env.DeadLetter.Reason != "failed" {
t.Errorf("dead_letter = %+v", env.DeadLetter)
}
if env.DeadLetter.OriginalQueue != "orders" {
t.Errorf("original_queue = %q, want orders", env.DeadLetter.OriginalQueue)
}
if env.DeadLetter.Error == nil || *env.DeadLetter.Error != "boom" {
t.Errorf("dead_letter.error = %v", env.DeadLetter.Error)
}
}
func TestAppRetriesThenDropsWithoutDLQ(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr, babelqueue.WithDefaultQueue("q"), babelqueue.WithMaxAttempts(2))
calls := 0
app.Handle("urn:babel:x", func(_ context.Context, _ babelqueue.Envelope) error {
calls++
return errors.New("nope")
})
if _, err := app.Publish(context.Background(), "urn:babel:x", map[string]any{}); err != nil {
t.Fatal(err)
}
if _, err := app.Drain(context.Background(), "q", 0); err != nil {
t.Fatal(err)
}
if calls != 2 {
t.Fatalf("handler called %d, want 2", calls)
}
if tr.Size("q") != 0 || tr.Size("q.dlq") != 0 {
t.Error("message should be dropped (no DLQ configured)")
}
}
func TestAppUnknownURNDeadLetter(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr,
babelqueue.WithDefaultQueue("orders"),
babelqueue.WithUnknownURNStrategy(babelqueue.StrategyDeadLetter),
babelqueue.WithDeadLetter(true),
)
// no handler registered for this URN
if _, err := app.Publish(context.Background(), "urn:babel:orders:created", map[string]any{"x": 1}); err != nil {
t.Fatal(err)
}
if _, err := app.Drain(context.Background(), "orders", 0); err != nil {
t.Fatal(err)
}
if tr.Size("orders.dlq") != 1 {
t.Fatalf("expected unknown-URN message on DLQ, got %d", tr.Size("orders.dlq"))
}
dl, _ := tr.Pop(context.Background(), "orders.dlq", 0)
env, _ := babelqueue.Decode([]byte(dl.Body))
if env.DeadLetter == nil || env.DeadLetter.Reason != "unknown_urn" {
t.Errorf("dead_letter = %+v", env.DeadLetter)
}
}
func TestAppUnknownURNDelete(t *testing.T) {
tr := babelqueue.NewInMemoryTransport()
app := babelqueue.NewApp(tr,
babelqueue.WithDefaultQueue("orders"),
babelqueue.WithUnknownURNStrategy(babelqueue.StrategyDelete),
)
if _, err := app.Publish(context.Background(), "urn:babel:orders:created", map[string]any{"x": 1}); err != nil {
t.Fatal(err)
}
n, err := app.Drain(context.Background(), "orders", 0)
if err != nil {
t.Fatal(err)
}
if n != 1 || tr.Size("orders") != 0 {
t.Error("unknown-URN message should be deleted")
}
}