-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_edge_cases_test.go
More file actions
261 lines (204 loc) · 6.75 KB
/
process_edge_cases_test.go
File metadata and controls
261 lines (204 loc) · 6.75 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
package comet
import (
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
// TestProcessMultiProcessMode tests Process() in multi-process mode
func TestProcessMultiProcessMode(t *testing.T) {
dir := t.TempDir()
config := DeprecatedMultiProcessConfig(0, 2) // Use multi-process config
client, err := NewClient(dir, config)
if err != nil {
t.Fatal(err)
}
defer client.Close()
ctx := context.Background()
stream := "test:v1:shard:0166" // Use higher shard number like user had
// Write multiple batches worth of data
batchSize := 25
totalMessages := 100
t.Logf("Writing %d messages to %s in multi-process mode", totalMessages, stream)
var messages [][]byte
for i := 0; i < totalMessages; i++ {
messages = append(messages, []byte(fmt.Sprintf("multi-process-msg-%d", i)))
}
_, err = client.Append(ctx, stream, messages)
if err != nil {
t.Fatal(err)
}
// Sync to ensure data is visible
if err := client.Sync(ctx); err != nil {
t.Fatal(err)
}
// Process with wildcards (like user was doing)
consumer := NewConsumer(client, ConsumerOptions{Group: "multi-test"})
defer consumer.Close()
var processedCount int64
var batchCount int64
processCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
processFunc := func(ctx context.Context, msgs []StreamMessage) error {
currentBatch := atomic.AddInt64(&batchCount, 1)
processed := atomic.AddInt64(&processedCount, int64(len(msgs)))
t.Logf("Multi-process batch %d: processed %d messages (total: %d/%d)",
currentBatch, len(msgs), processed, totalMessages)
if int(processed) >= totalMessages {
t.Logf("All multi-process messages processed, canceling")
cancel()
}
return nil
}
consumer.Process(processCtx, processFunc,
WithStream("test:v1:shard:*"), // Wildcard pattern
WithBatchSize(batchSize),
WithAutoAck(true),
)
finalProcessed := atomic.LoadInt64(&processedCount)
finalBatches := atomic.LoadInt64(&batchCount)
t.Logf("Multi-process final: processed %d/%d messages in %d batches",
finalProcessed, totalMessages, finalBatches)
if finalProcessed < int64(totalMessages) {
t.Errorf("MULTI-PROCESS BUG: Expected %d messages, got %d", totalMessages, finalProcessed)
}
}
// TestProcessWithErrors tests Process() behavior with processing errors
func TestProcessWithErrors(t *testing.T) {
dir := t.TempDir()
config := DefaultCometConfig()
client, err := NewClient(dir, config)
if err != nil {
t.Fatal(err)
}
defer client.Close()
ctx := context.Background()
stream := "test:v1:shard:0000"
// Write messages
totalMessages := 75
var messages [][]byte
for i := 0; i < totalMessages; i++ {
messages = append(messages, []byte(fmt.Sprintf("error-test-%d", i)))
}
_, err = client.Append(ctx, stream, messages)
if err != nil {
t.Fatal(err)
}
// Sync to make messages durable and readable by consumer
if err := client.Sync(ctx); err != nil {
t.Fatal(err)
}
consumer := NewConsumer(client, ConsumerOptions{Group: "error-test"})
defer consumer.Close()
var processedCount int64
var errorCount int64
processCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
processFunc := func(ctx context.Context, msgs []StreamMessage) error {
processed := atomic.AddInt64(&processedCount, int64(len(msgs)))
// Simulate error on first batch only
if processed <= 25 {
atomic.AddInt64(&errorCount, 1)
t.Logf("Simulating error on batch with %d messages (total processed: %d)", len(msgs), processed)
return fmt.Errorf("simulated error")
}
t.Logf("Successfully processed batch with %d messages (total: %d)", len(msgs), processed)
if int(processed) >= totalMessages {
cancel()
}
return nil
}
consumer.Process(processCtx, processFunc,
WithStream("test:v1:shard:*"),
WithBatchSize(25),
WithMaxRetries(2), // Allow retries
WithAutoAck(true),
)
finalProcessed := atomic.LoadInt64(&processedCount)
finalErrors := atomic.LoadInt64(&errorCount)
t.Logf("Error test final: processed %d/%d messages, %d errors",
finalProcessed, totalMessages, finalErrors)
// Should process all messages even with initial errors (due to retries)
if finalProcessed < int64(totalMessages) {
t.Errorf("ERROR HANDLING BUG: Expected %d messages, got %d", totalMessages, finalProcessed)
}
}
// TestProcessLongRunning tests Process() over longer time periods
func TestProcessLongRunning(t *testing.T) {
if testing.Short() {
t.Skip("skipping long running test")
}
dir := t.TempDir()
config := DefaultCometConfig()
client, err := NewClient(dir, config)
if err != nil {
t.Fatal(err)
}
defer client.Close()
ctx := context.Background()
stream := "test:v1:shard:0000"
// Write initial batch
initialMessages := 50
var messages [][]byte
for i := 0; i < initialMessages; i++ {
messages = append(messages, []byte(fmt.Sprintf("long-running-%d", i)))
}
_, err = client.Append(ctx, stream, messages)
if err != nil {
t.Fatal(err)
}
// Sync to ensure data is visible to consumers
err = client.Sync(ctx)
if err != nil {
t.Fatal(err)
}
consumer := NewConsumer(client, ConsumerOptions{Group: "long-test"})
defer consumer.Close()
var processedCount int64
var wg sync.WaitGroup
// Run for longer period
processCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
processFunc := func(ctx context.Context, msgs []StreamMessage) error {
processed := atomic.AddInt64(&processedCount, int64(len(msgs)))
t.Logf("Long-running: processed batch of %d (total: %d)", len(msgs), processed)
// After processing initial messages, add more messages to test continuous processing
if processed == int64(initialMessages) {
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(1 * time.Second)
additionalMessages := 25
var newMessages [][]byte
for i := 0; i < additionalMessages; i++ {
newMessages = append(newMessages, []byte(fmt.Sprintf("additional-%d", i)))
}
client.Append(context.Background(), stream, newMessages)
client.Sync(context.Background()) // Ensure additional messages are visible
t.Logf("Added %d additional messages", additionalMessages)
}()
}
// Cancel after processing both batches
if processed >= int64(initialMessages+25) {
t.Logf("Processed all messages including additional ones")
cancel()
}
return nil
}
consumer.Process(processCtx, processFunc,
WithStream("test:v1:shard:*"),
WithBatchSize(10),
WithAutoAck(true),
WithPollInterval(500*time.Millisecond), // Poll every 500ms for new data
)
// Wait for any background goroutines to complete
wg.Wait()
finalProcessed := atomic.LoadInt64(&processedCount)
expectedTotal := int64(initialMessages + 25)
t.Logf("Long-running final: processed %d/%d messages", finalProcessed, expectedTotal)
if finalProcessed < expectedTotal {
t.Errorf("LONG-RUNNING BUG: Expected %d messages, got %d", expectedTotal, finalProcessed)
}
}