-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterable_test.go
More file actions
318 lines (272 loc) · 7.27 KB
/
Copy pathiterable_test.go
File metadata and controls
318 lines (272 loc) · 7.27 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package senna
import (
"context"
"testing"
)
func TestCursorFrom(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value any
want string
}{
{"int", 42, "42"},
{"string", "hello", `"hello"`},
{"nil", nil, ""},
{"map", map[string]int{"a": 1}, `{"a":1}`},
{"slice", []int{1, 2, 3}, "[1,2,3]"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cursor := CursorFrom(tt.value)
if tt.value == nil {
if cursor != nil {
t.Errorf("CursorFrom(nil) = %v, want nil", cursor)
}
return
}
if string(cursor) != tt.want {
t.Errorf("CursorFrom(%v) = %s, want %s", tt.value, cursor, tt.want)
}
})
}
}
func TestCursorTo(t *testing.T) {
t.Parallel()
t.Run("int", func(t *testing.T) {
cursor := CursorFrom(42)
got, err := CursorTo[int](cursor)
if err != nil {
t.Errorf("CursorTo[int] error = %v", err)
}
if got != 42 {
t.Errorf("CursorTo[int] = %d, want 42", got)
}
})
t.Run("string", func(t *testing.T) {
cursor := CursorFrom("hello")
got, err := CursorTo[string](cursor)
if err != nil {
t.Errorf("CursorTo[string] error = %v", err)
}
if got != "hello" {
t.Errorf("CursorTo[string] = %s, want hello", got)
}
})
t.Run("nil cursor", func(t *testing.T) {
got, err := CursorTo[int](nil)
if err != nil {
t.Errorf("CursorTo[int](nil) error = %v", err)
}
if got != 0 {
t.Errorf("CursorTo[int](nil) = %d, want 0", got)
}
})
t.Run("complex type", func(t *testing.T) {
type position struct {
Offset int `json:"offset"`
Key string `json:"key"`
}
cursor := CursorFrom(position{Offset: 100, Key: "abc"})
got, err := CursorTo[position](cursor)
if err != nil {
t.Errorf("CursorTo[position] error = %v", err)
}
if got.Offset != 100 || got.Key != "abc" {
t.Errorf("CursorTo[position] = %+v, want {Offset:100 Key:abc}", got)
}
})
}
func TestSliceIterator(t *testing.T) {
t.Parallel()
items := []string{"a", "b", "c", "d", "e"}
ctx := context.Background()
t.Run("iterate all from start", func(t *testing.T) {
iter := SliceIterator(items, 0)
defer func() { _ = iter.Close() }()
var collected []string
for iter.Next(ctx) {
collected = append(collected, iter.Item().(string))
}
if iter.Err() != nil {
t.Errorf("iterator error = %v", iter.Err())
}
if len(collected) != len(items) {
t.Errorf("collected %d items, want %d", len(collected), len(items))
}
for i, item := range collected {
if item != items[i] {
t.Errorf("item[%d] = %s, want %s", i, item, items[i])
}
}
})
t.Run("resume from offset", func(t *testing.T) {
iter := SliceIterator(items, 2) // Start from index 2 ("c")
defer func() { _ = iter.Close() }()
var collected []string
for iter.Next(ctx) {
collected = append(collected, iter.Item().(string))
}
expected := []string{"c", "d", "e"}
if len(collected) != len(expected) {
t.Errorf("collected %d items, want %d", len(collected), len(expected))
}
for i, item := range collected {
if item != expected[i] {
t.Errorf("item[%d] = %s, want %s", i, item, expected[i])
}
}
})
t.Run("cursor tracks position", func(t *testing.T) {
iter := SliceIterator(items, 0)
defer func() { _ = iter.Close() }()
i := 0
for iter.Next(ctx) {
cursor := iter.Cursor()
pos, _ := CursorTo[int](cursor)
// Cursor should be next position to resume from
if pos != i+1 {
t.Errorf("cursor after item %d = %d, want %d", i, pos, i+1)
}
i++
}
})
t.Run("empty slice", func(t *testing.T) {
iter := SliceIterator([]string{}, 0)
defer func() { _ = iter.Close() }()
if iter.Next(ctx) {
t.Error("expected Next() to return false for empty slice")
}
})
}
func TestRangeIterator(t *testing.T) {
t.Parallel()
ctx := context.Background()
t.Run("ascending range", func(t *testing.T) {
iter := RangeIterator(0, 5, 1)
defer func() { _ = iter.Close() }()
var collected []int64
for iter.Next(ctx) {
collected = append(collected, iter.Item().(int64))
}
expected := []int64{0, 1, 2, 3, 4}
if len(collected) != len(expected) {
t.Errorf("collected %d items, want %d", len(collected), len(expected))
}
for i, val := range collected {
if val != expected[i] {
t.Errorf("item[%d] = %d, want %d", i, val, expected[i])
}
}
})
t.Run("step of 2", func(t *testing.T) {
iter := RangeIterator(0, 10, 2)
defer func() { _ = iter.Close() }()
var collected []int64
for iter.Next(ctx) {
collected = append(collected, iter.Item().(int64))
}
expected := []int64{0, 2, 4, 6, 8}
if len(collected) != len(expected) {
t.Errorf("collected %d items, want %d", len(collected), len(expected))
}
})
t.Run("cursor tracks position", func(t *testing.T) {
iter := RangeIterator(10, 15, 1)
defer func() { _ = iter.Close() }()
expectedCursors := []int64{11, 12, 13, 14, 15}
i := 0
for iter.Next(ctx) {
cursor := iter.Cursor()
pos, _ := CursorTo[int64](cursor)
if pos != expectedCursors[i] {
t.Errorf("cursor at step %d = %d, want %d", i, pos, expectedCursors[i])
}
i++
}
})
t.Run("empty range", func(t *testing.T) {
iter := RangeIterator(5, 5, 1) // start == end
defer func() { _ = iter.Close() }()
if iter.Next(ctx) {
t.Error("expected Next() to return false for empty range")
}
})
}
func TestIterableFunc(t *testing.T) {
t.Parallel()
ctx := context.Background()
job := NewJob("test_job", nil)
buildCalled := false
processCalled := 0
handler := IterableFunc(
func(ctx context.Context, job *Job, cursor Cursor) (Iterator, error) {
buildCalled = true
offset := 0
if cursor != nil {
offset, _ = CursorTo[int](cursor)
}
return SliceIterator([]int{1, 2, 3}, offset), nil
},
func(ctx context.Context, job *Job, item any) error {
processCalled++
return nil
},
)
iter, err := handler.BuildIterator(ctx, job, nil)
if err != nil {
t.Fatalf("BuildIterator error = %v", err)
}
defer func() { _ = iter.Close() }()
if !buildCalled {
t.Error("build function not called")
}
for iter.Next(ctx) {
if err := handler.ProcessItem(ctx, job, iter.Item()); err != nil {
t.Errorf("ProcessItem error = %v", err)
}
}
if processCalled != 3 {
t.Errorf("process called %d times, want 3", processCalled)
}
}
func TestInterrupted(t *testing.T) {
t.Parallel()
t.Run("not interrupted", func(t *testing.T) {
ctx := context.Background()
if Interrupted(ctx) {
t.Error("expected Interrupted to return false for active context")
}
})
t.Run("cancelled context", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
if !Interrupted(ctx) {
t.Error("expected Interrupted to return true for cancelled context")
}
})
}
func TestSkipItemError(t *testing.T) {
t.Parallel()
err := &SkipItemError{Reason: "invalid format"}
expected := "skipping item: invalid format"
if err.Error() != expected {
t.Errorf("Error() = %s, want %s", err.Error(), expected)
}
}
func TestStopIterationError(t *testing.T) {
t.Parallel()
err := &StopIterationError{Reason: "found target"}
expected := "stopping iteration: found target"
if err.Error() != expected {
t.Errorf("Error() = %s, want %s", err.Error(), expected)
}
}
func TestInterruptedError(t *testing.T) {
t.Parallel()
err := &InterruptedError{}
expected := "job interrupted"
if err.Error() != expected {
t.Errorf("Error() = %s, want %s", err.Error(), expected)
}
}