-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handling_test.go
More file actions
237 lines (193 loc) · 6.55 KB
/
error_handling_test.go
File metadata and controls
237 lines (193 loc) · 6.55 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
package batchflow_test
import (
"context"
"errors"
"testing"
"time"
"github.com/rushairer/batchflow"
)
// MockErrorExecutor 模拟错误的执行器
type MockErrorExecutor struct {
shouldFailExec bool
errorMessage string
delay time.Duration
}
func (m *MockErrorExecutor) ExecuteBatch(ctx context.Context, schema batchflow.SchemaInterface, data []map[string]any) error {
if m.delay > 0 {
select {
case <-time.After(m.delay):
case <-ctx.Done():
return ctx.Err()
}
}
if m.shouldFailExec {
return errors.New(m.errorMessage)
}
return nil
}
func (m *MockErrorExecutor) WithMetricsReporter(metricsReporter batchflow.MetricsReporter) batchflow.BatchExecutor {
// 测试用:直接返回自身,避免将执行器置空导致管道 nil 指针异常
return m
}
func TestErrorHandling_ExecutionError(t *testing.T) {
ctx := context.Background()
// 创建会在执行时失败的执行器
errorExecutor := &MockErrorExecutor{
shouldFailExec: true,
errorMessage: "Database connection failed",
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, errorExecutor)
// 提前创建错误通道,避免并发期间修改内部通道
errorChan := batch.ErrorChan(10)
// 创建 schema 和请求
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id")
request := batchflow.NewRequest(schema).SetInt64("id", 1)
// 提交数据
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit should not return error immediately: %v", err)
}
// 等待错误
select {
case err := <-errorChan:
if err == nil {
t.Error("Expected error from execution, but got nil")
}
if err.Error() != "Database connection failed" {
t.Errorf("Expected 'Database connection failed', got: %v", err)
}
case <-time.After(2 * time.Second):
t.Error("Expected error but timeout occurred")
}
}
func TestErrorHandling_ContextTimeout(t *testing.T) {
ctx := context.Background()
// 创建会延迟执行的执行器
slowExecutor := &MockErrorExecutor{
delay: 2 * time.Second,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, slowExecutor)
// 提前创建错误通道,使用短超时
errorChan := batch.ErrorChan(10)
// 创建 schema 和请求
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id")
request := batchflow.NewRequest(schema).SetInt64("id", 1)
// 提交数据
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit should not return error immediately: %v", err)
}
// 等待错误或超时
select {
case err := <-errorChan:
if err != nil && errors.Is(err, context.DeadlineExceeded) {
// 这是我们期望的超时错误
return
}
t.Errorf("Expected timeout error, got: %v", err)
case <-time.After(3 * time.Second):
// 如果没有收到错误,说明超时机制可能没有正常工作
t.Log("No timeout error received, this might be expected in some cases")
}
}
func TestErrorHandling_ContextCancellation(t *testing.T) {
ctx := context.Background()
// 创建会延迟执行的执行器
slowExecutor := &MockErrorExecutor{
delay: 2 * time.Second,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, slowExecutor)
// 提前创建错误通道
errorChan := batch.ErrorChan(10)
// 创建 schema 和请求
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id")
request := batchflow.NewRequest(schema).SetInt64("id", 1)
// 提交数据
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit should not return error immediately: %v", err)
}
// 等待错误或超时
select {
case err := <-errorChan:
if err != nil && errors.Is(err, context.Canceled) {
// 这是我们期望的取消错误
return
}
t.Errorf("Expected cancellation error, got: %v", err)
case <-time.After(3 * time.Second):
// 如果没有收到错误,说明取消机制可能没有正常工作
t.Log("No cancellation error received, this might be expected in some cases")
}
}
func TestErrorHandling_InvalidData(t *testing.T) {
ctx := context.Background()
config := batchflow.PipelineConfig{
BufferSize: 10,
FlushSize: 5,
FlushInterval: time.Second,
}
batch, _ := batchflow.NewBatchFlowWithMock(ctx, config)
// 测试 nil 请求
err := batch.Submit(ctx, nil)
if err == nil {
t.Error("Expected error for nil request, but got nil")
}
// 测试空 schema
emptySchema := batchflow.NewSQLSchema("", batchflow.ConflictIgnoreOperationConfig)
emptyRequest := batchflow.NewRequest(emptySchema)
err = batch.Submit(ctx, emptyRequest)
if err == nil {
t.Error("Expected error for empty table name, but got nil")
}
}
func TestErrorHandling_MultipleErrors(t *testing.T) {
ctx := context.Background()
// 创建会在执行时失败的执行器
errorExecutor := &MockErrorExecutor{
shouldFailExec: true,
errorMessage: "Multiple execution failed",
}
batch := batchflow.NewBatchFlow(ctx, 10, 2, time.Second, errorExecutor)
// 提前创建错误通道
errorChan := batch.ErrorChan(10)
// 创建多个不同的 schema
schema1 := batchflow.NewSQLSchema("table1", batchflow.ConflictIgnoreOperationConfig, "id")
schema2 := batchflow.NewSQLSchema("table2", batchflow.ConflictIgnoreOperationConfig, "id")
schema3 := batchflow.NewSQLSchema("table3", batchflow.ConflictIgnoreOperationConfig, "id")
// 提交多个表的数据
_ = batch.Submit(ctx, batchflow.NewRequest(schema1).SetInt64("id", 1))
_ = batch.Submit(ctx, batchflow.NewRequest(schema2).SetInt64("id", 2))
_ = batch.Submit(ctx, batchflow.NewRequest(schema3).SetInt64("id", 3))
// 等待错误
select {
case err := <-errorChan:
if err == nil {
t.Error("Expected error from multiple executions, but got nil")
}
if err.Error() != "Multiple execution failed" {
t.Errorf("Expected 'Multiple execution failed', got: %v", err)
}
case <-time.After(2 * time.Second):
t.Error("Expected error but timeout occurred")
}
}
func TestErrorHandling_CloseWithPendingData(t *testing.T) {
ctx := context.Background()
config := batchflow.PipelineConfig{
BufferSize: 100, // 大缓冲区,确保数据不会自动刷新
FlushSize: 50,
FlushInterval: time.Hour, // 很长的间隔,确保不会自动刷新
}
batch, _ := batchflow.NewBatchFlowWithMock(ctx, config)
// 创建 schema 和请求
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id")
// 提交一些数据但不刷新
for i := 0; i < 10; i++ {
request := batchflow.NewRequest(schema).SetInt64("id", int64(i))
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit failed: %v", err)
}
}
}