-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtyped_test.go
More file actions
270 lines (234 loc) · 6.86 KB
/
Copy pathtyped_test.go
File metadata and controls
270 lines (234 loc) · 6.86 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package durex_test
import (
"context"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/simonovic86/durex"
"github.com/simonovic86/durex/storage"
)
func TestTyped_Success(t *testing.T) {
type EmailData struct {
To string `json:"to"`
Subject string `json:"subject"`
}
spec, err := durex.Typed("sendEmail", EmailData{
To: "user@example.com",
Subject: "Welcome!",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if spec.Name != "sendEmail" {
t.Errorf("expected name 'sendEmail', got %q", spec.Name)
}
if spec.Data["to"] != "user@example.com" {
t.Errorf("expected to 'user@example.com', got %v", spec.Data["to"])
}
if spec.Data["subject"] != "Welcome!" {
t.Errorf("expected subject 'Welcome!', got %v", spec.Data["subject"])
}
}
func TestTyped_MarshalError(t *testing.T) {
type BadData struct {
Ch chan int `json:"ch"`
}
_, err := durex.Typed("test", BadData{Ch: make(chan int)})
if err == nil {
t.Fatal("expected error for unmarshalable data")
}
if !strings.Contains(err.Error(), "failed to marshal data") {
t.Errorf("expected marshal error, got: %v", err)
}
}
func TestMustTyped_Success(t *testing.T) {
type Data struct {
Value string `json:"value"`
}
spec := durex.MustTyped("test", Data{Value: "hello"})
if spec.Name != "test" {
t.Errorf("expected name 'test', got %q", spec.Name)
}
}
func TestMustTyped_Panics(t *testing.T) {
type BadData struct {
Ch chan int `json:"ch"`
}
defer func() {
if r := recover(); r == nil {
t.Error("expected panic for unmarshalable data")
}
}()
durex.MustTyped("test", BadData{Ch: make(chan int)})
}
func TestTypedCommand_Recover_UnmarshalError(t *testing.T) {
store := storage.NewMemory()
executor := durex.New(store, durex.WithParallelism(1))
recoverCalled := atomic.Bool{}
type MyData struct {
Value string `json:"value"`
}
durex.HandleTyped(executor, "recoverTest",
func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
},
durex.WithRecover(func(ctx context.Context, data MyData, cmd *durex.Instance, err error) (durex.Result, error) {
recoverCalled.Store(true)
return durex.Empty(), nil
}),
)
// Test the Recover method directly with bad data via a TypedCommand
cmd := durex.NewTyped("directRecoverTest",
func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
},
durex.WithRecover(func(ctx context.Context, data MyData, cmd *durex.Instance, err error) (durex.Result, error) {
recoverCalled.Store(true)
return durex.Empty(), nil
}),
)
// Create an instance with data that can't unmarshal to MyData
instance := &durex.Instance{
ID: "test-1",
Name: "directRecoverTest",
Status: durex.StatusFailed,
Data: durex.M{"value": map[string]any{"nested": "object"}}, // string field with non-string value
CreatedAt: time.Now(),
ReadyAt: time.Now(),
}
_, recoverErr := cmd.Recover(context.Background(), instance, nil)
if recoverErr == nil {
t.Error("expected error from Recover with bad data")
}
if recoverCalled.Load() {
t.Error("recovery function should not have been called with bad data")
}
_ = executor
}
func TestTypedCommand_Expired(t *testing.T) {
type MyData struct {
Value string `json:"value"`
}
expiredCalled := atomic.Bool{}
var receivedValue string
cmd := durex.NewTyped("expireTest",
func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
},
durex.WithExpired(func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
expiredCalled.Store(true)
receivedValue = data.Value
return durex.Empty(), nil
}),
)
instance := &durex.Instance{
ID: "test-expired-1",
Name: "expireTest",
Status: durex.StatusPending,
Data: durex.M{"value": "hello"},
CreatedAt: time.Now(),
ReadyAt: time.Now(),
}
result, err := cmd.Expired(context.Background(), instance)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !expiredCalled.Load() {
t.Error("expired function should have been called")
}
if receivedValue != "hello" {
t.Errorf("expected value 'hello', got %q", receivedValue)
}
if len(result.Commands) != 0 {
t.Error("expected empty result")
}
}
func TestTypedCommand_Expired_Nil(t *testing.T) {
type MyData struct {
Value string `json:"value"`
}
cmd := durex.NewTyped("expireNilTest",
func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
},
)
instance := &durex.Instance{
ID: "test-expired-nil",
Name: "expireNilTest",
Status: durex.StatusPending,
Data: durex.M{"value": "hello"},
CreatedAt: time.Now(),
ReadyAt: time.Now(),
}
result, err := cmd.Expired(context.Background(), instance)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Commands) != 0 {
t.Error("expected empty result from nil handler")
}
}
func TestTypedCommand_Expired_UnmarshalError(t *testing.T) {
type MyData struct {
Value string `json:"value"`
}
expiredCalled := atomic.Bool{}
cmd := durex.NewTyped("expireUnmarshalTest",
func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
},
durex.WithExpired(func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
expiredCalled.Store(true)
return durex.Empty(), nil
}),
)
instance := &durex.Instance{
ID: "test-expired-bad",
Name: "expireUnmarshalTest",
Status: durex.StatusPending,
Data: durex.M{"value": map[string]any{"nested": "object"}},
CreatedAt: time.Now(),
ReadyAt: time.Now(),
}
_, err := cmd.Expired(context.Background(), instance)
if err == nil {
t.Error("expected error from Expired with bad data")
}
if expiredCalled.Load() {
t.Error("expired function should not have been called with bad data")
}
}
func TestTypedCommand_WithTags(t *testing.T) {
type MyData struct {
Value string `json:"value"`
}
cmd := durex.NewTyped("tagTest",
func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
},
durex.WithTags[MyData]("billing", "urgent"),
)
spec := cmd.Default()
if len(spec.Tags) != 2 {
t.Fatalf("expected 2 tags, got %d", len(spec.Tags))
}
if spec.Tags[0] != "billing" || spec.Tags[1] != "urgent" {
t.Errorf("expected tags [billing urgent], got %v", spec.Tags)
}
}
func TestHandleTyped_ReturnsExecutor(t *testing.T) {
type MyData struct {
Value string `json:"value"`
}
store := storage.NewMemory()
executor := durex.New(store)
returned := durex.HandleTyped(executor, "returnTest",
func(ctx context.Context, data MyData, cmd *durex.Instance) (durex.Result, error) {
return durex.Empty(), nil
},
)
if returned != executor {
t.Error("HandleTyped should return the same executor for chaining")
}
}