-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_dlq_test.go
More file actions
197 lines (159 loc) · 4.71 KB
/
executor_dlq_test.go
File metadata and controls
197 lines (159 loc) · 4.71 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package durex_test
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/simonovic86/durex"
"github.com/simonovic86/durex/storage"
)
func TestExecutor_DLQ_FailedCommandMovedToDLQ(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithDeadLetterQueue(),
)
executor.HandleFunc("fail", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("permanent failure")
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
inst, _ := executor.Add(ctx, durex.Spec{Name: "fail"})
time.Sleep(200 * time.Millisecond)
got, err := executor.Get(ctx, inst.ID)
if err != nil {
t.Fatalf("Get: %v", err)
}
if got.Status != durex.StatusDeadLetter {
t.Errorf("Status = %s, want DEAD_LETTER", got.Status)
}
}
func TestExecutor_DLQ_FindDeadLettered(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithDeadLetterQueue(),
)
executor.HandleFunc("fail", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("permanent failure")
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
executor.Add(ctx, durex.Spec{Name: "fail"})
executor.Add(ctx, durex.Spec{Name: "fail"})
time.Sleep(200 * time.Millisecond)
dlq, err := executor.FindDeadLettered(ctx)
if err != nil {
t.Fatalf("FindDeadLettered: %v", err)
}
if len(dlq) != 2 {
t.Errorf("FindDeadLettered = %d, want 2", len(dlq))
}
}
func TestExecutor_DLQ_ReplayFromDLQ(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithDeadLetterQueue(),
)
var attempts atomic.Int32
executor.HandleFunc("flaky", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
count := attempts.Add(1)
if count <= 1 {
return durex.Empty(), errors.New("temporary failure")
}
return durex.Empty(), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
inst, _ := executor.Add(ctx, durex.Spec{Name: "flaky"})
time.Sleep(200 * time.Millisecond)
// Should be in DLQ
got, _ := executor.Get(ctx, inst.ID)
if got.Status != durex.StatusDeadLetter {
t.Fatalf("Status = %s, want DEAD_LETTER", got.Status)
}
// Replay
if err := executor.ReplayFromDLQ(ctx, inst.ID); err != nil {
t.Fatalf("ReplayFromDLQ: %v", err)
}
time.Sleep(200 * time.Millisecond)
got, _ = executor.Get(ctx, inst.ID)
if got.Status != durex.StatusCompleted {
t.Errorf("After replay: Status = %s, want COMPLETED", got.Status)
}
}
func TestExecutor_DLQ_ReplayNonDLQCommand(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithDeadLetterQueue(),
)
executor.HandleFunc("ok", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
inst, _ := executor.Add(ctx, durex.Spec{Name: "ok"})
time.Sleep(200 * time.Millisecond)
err := executor.ReplayFromDLQ(ctx, inst.ID)
if err == nil {
t.Error("Expected error when replaying non-DLQ command")
}
}
func TestExecutor_DLQ_PurgeDLQ(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithDeadLetterQueue(),
)
executor.HandleFunc("fail", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("permanent failure")
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
executor.Add(ctx, durex.Spec{Name: "fail"})
time.Sleep(200 * time.Millisecond)
// Purge with 0 age (remove all)
count, err := executor.PurgeDLQ(ctx, 0)
if err != nil {
t.Fatalf("PurgeDLQ: %v", err)
}
if count != 1 {
t.Errorf("PurgeDLQ = %d, want 1", count)
}
// Should be empty now
dlq, _ := executor.FindDeadLettered(ctx)
if len(dlq) != 0 {
t.Errorf("After purge: %d in DLQ, want 0", len(dlq))
}
}
func TestExecutor_DLQ_PurgeRespectAge(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store,
durex.WithParallelism(1),
durex.WithDeadLetterQueue(),
)
executor.HandleFunc("fail", func(ctx context.Context, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), errors.New("permanent failure")
})
ctx := context.Background()
executor.Start(ctx)
defer executor.Stop()
executor.Add(ctx, durex.Spec{Name: "fail"})
time.Sleep(200 * time.Millisecond)
// Purge with 1 hour age — nothing should be purged
count, err := executor.PurgeDLQ(ctx, time.Hour)
if err != nil {
t.Fatalf("PurgeDLQ: %v", err)
}
if count != 0 {
t.Errorf("PurgeDLQ = %d, want 0 (too recent)", count)
}
}