-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connection_test.go
More file actions
449 lines (368 loc) · 10.4 KB
/
db_connection_test.go
File metadata and controls
449 lines (368 loc) · 10.4 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package batchflow_test
import (
"context"
"database/sql"
"errors"
"sync"
"testing"
"time"
"github.com/rushairer/batchflow"
)
// MockDB 模拟数据库连接
type MockDB struct {
mu sync.RWMutex
shouldFail bool
errorMessage string
delay time.Duration
pingCount int
execCount int
}
func (m *MockDB) Ping() error {
m.mu.Lock()
m.pingCount++
shouldFail := m.shouldFail
errMsg := m.errorMessage
m.mu.Unlock()
if shouldFail {
return errors.New(errMsg)
}
return nil
}
func (m *MockDB) Exec(query string, args ...any) (sql.Result, error) {
m.mu.Lock()
m.execCount++
delay := m.delay
shouldFail := m.shouldFail
errMsg := m.errorMessage
m.mu.Unlock()
if delay > 0 {
time.Sleep(delay)
}
if shouldFail {
return nil, errors.New(errMsg)
}
return &MockResult{}, nil
}
func (m *MockDB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
m.mu.Lock()
m.execCount++
delay := m.delay
shouldFail := m.shouldFail
errMsg := m.errorMessage
m.mu.Unlock()
if delay > 0 {
select {
case <-time.After(delay):
case <-ctx.Done():
return nil, ctx.Err()
}
}
if shouldFail {
return nil, errors.New(errMsg)
}
return &MockResult{}, nil
}
// MockResult 模拟SQL执行结果
type MockResult struct{}
func (m *MockResult) LastInsertId() (int64, error) {
return 1, nil
}
func (m *MockResult) RowsAffected() (int64, error) {
return 1, nil
}
// 线程安全地更新 shouldFail
func (m *MockDB) SetShouldFail(v bool) {
m.mu.Lock()
m.shouldFail = v
m.mu.Unlock()
}
// MockBatchExecutor 模拟批量执行器,用于测试数据库连接异常
type MockDBExecutor struct {
db *MockDB
shouldFail bool
errorMessage string
}
func (m *MockDBExecutor) ExecuteBatch(ctx context.Context, schema batchflow.SchemaInterface, data []map[string]any) error {
if m.shouldFail {
return errors.New(m.errorMessage)
}
// 模拟数据库操作
if m.db != nil {
_, err := m.db.ExecContext(ctx, "INSERT INTO "+schema.Name()+" VALUES (?)", "test")
return err
}
return nil
}
func (m *MockDBExecutor) WithMetricsReporter(metricsReporter batchflow.MetricsReporter) batchflow.BatchExecutor {
// 测试用:直接返回自身,避免将执行器置空导致管道 nil 指针异常
return m
}
func TestDBConnection_ConnectionFailure(t *testing.T) {
ctx := context.Background()
// 创建会连接失败的模拟数据库
mockDB := &MockDB{
shouldFail: true,
errorMessage: "connection refused",
}
executor := &MockDBExecutor{
db: mockDB,
shouldFail: false,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, executor)
// 提前创建错误通道,避免并发修改内部通道
errorChan := batch.ErrorChan(10)
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id", "name")
request := batchflow.NewRequest(schema).
SetInt64("id", 1).
SetString("name", "test")
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit should not fail immediately: %v", err)
}
select {
case err := <-errorChan:
if err == nil {
t.Error("Expected connection error, but got nil")
}
if err.Error() != "connection refused" {
t.Errorf("Expected 'connection refused', got: %v", err)
}
case <-time.After(2 * time.Second):
t.Error("Expected connection error but timeout occurred")
}
}
func TestDBConnection_SlowConnection(t *testing.T) {
ctx := context.Background()
// 创建慢连接的模拟数据库
mockDB := &MockDB{
delay: 2 * time.Second,
}
executor := &MockDBExecutor{
db: mockDB,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, executor)
// 提前创建错误通道
errorChan := batch.ErrorChan(10)
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id", "name")
request := batchflow.NewRequest(schema).
SetInt64("id", 1).
SetString("name", "test")
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit should not fail 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, connection might be working normally")
}
}
func TestDBConnection_ConnectionRecovery(t *testing.T) {
ctx := context.Background()
// 创建初始失败但后来恢复的模拟数据库
mockDB := &MockDB{
shouldFail: true,
errorMessage: "temporary connection failure",
}
executor := &MockDBExecutor{
db: mockDB,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, executor)
// 提前创建错误通道
errorChan := batch.ErrorChan(10)
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id", "name")
// 提交第一个请求(应该失败)
request1 := batchflow.NewRequest(schema).
SetInt64("id", 1).
SetString("name", "test1")
err := batch.Submit(ctx, request1)
if err != nil {
t.Errorf("Submit should not fail immediately: %v", err)
}
// 等待第一个错误
select {
case err := <-errorChan:
if err == nil {
t.Error("Expected connection error, but got nil")
}
t.Logf("Received expected error: %v", err)
case <-time.After(2 * time.Second):
t.Error("Expected connection error but timeout occurred")
return
}
// 模拟连接恢复
mockDB.SetShouldFail(false)
// 提交第二个请求(应该成功)
request2 := batchflow.NewRequest(schema).
SetInt64("id", 2).
SetString("name", "test2")
err = batch.Submit(ctx, request2)
if err != nil {
t.Errorf("Submit after recovery should not fail: %v", err)
}
// 等待一段时间,确保没有更多错误
select {
case err := <-errorChan:
if err != nil {
t.Errorf("Unexpected error after recovery: %v", err)
}
case <-time.After(1 * time.Second):
// 没有错误是期望的
t.Log("No errors after connection recovery - good!")
}
}
func TestDBConnection_TransactionFailure(t *testing.T) {
ctx := context.Background()
// 创建在执行时失败的模拟数据库
mockDB := &MockDB{
shouldFail: true,
errorMessage: "transaction deadlock",
}
executor := &MockDBExecutor{
db: mockDB,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, executor)
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id", "name")
// 提交多个请求
for i := 0; i < 10; i++ {
request := batchflow.NewRequest(schema).
SetInt64("id", int64(i)).
SetString("name", "test"+string(rune('0'+i)))
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit %d should not fail immediately: %v", i, err)
}
}
// 监听错误通道
errorChan := batch.ErrorChan(10)
errorCount := 0
timeout := time.After(3 * time.Second)
for {
select {
case err := <-errorChan:
if err != nil {
errorCount++
if err.Error() != "transaction deadlock" {
t.Errorf("Expected 'transaction deadlock', got: %v", err)
}
}
case <-timeout:
if errorCount == 0 {
t.Error("Expected at least one transaction error")
} else {
t.Logf("Received %d transaction errors as expected", errorCount)
}
return
}
}
}
func TestDBConnection_ContextCancellationDuringExecution(t *testing.T) {
ctx := context.Background()
// 创建执行缓慢的模拟数据库
mockDB := &MockDB{
delay: 3 * time.Second,
}
executor := &MockDBExecutor{
db: mockDB,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, executor)
// 提前创建错误通道
errorChan := batch.ErrorChan(10)
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id", "name")
request := batchflow.NewRequest(schema).
SetInt64("id", 1).
SetString("name", "test")
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit should not fail immediately: %v", err)
}
// 等待错误(应该是上下文取消错误)
select {
case err := <-errorChan:
if err != nil && errors.Is(err, context.Canceled) {
t.Log("Received expected context cancellation error")
} else if err != nil {
t.Logf("Received error (might be timeout related): %v", err)
}
case <-time.After(5 * time.Second):
t.Log("No cancellation error received within timeout")
}
}
func TestDBConnection_MaxConnectionsExceeded(t *testing.T) {
ctx := context.Background()
// 模拟连接池耗尽的情况
executor := &MockDBExecutor{
shouldFail: true,
errorMessage: "too many connections",
}
batch := batchflow.NewBatchFlow(ctx, 100, 10, time.Second, executor)
// 提前创建错误通道
errorChan := batch.ErrorChan(50)
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id", "name")
// 提交大量请求
for i := 0; i < 50; i++ {
request := batchflow.NewRequest(schema).
SetInt64("id", int64(i)).
SetString("name", "test"+string(rune('0'+i%10)))
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit %d should not fail immediately: %v", i, err)
}
}
errorCount := 0
timeout := time.After(3 * time.Second)
for {
select {
case err := <-errorChan:
if err != nil {
errorCount++
if err.Error() != "too many connections" {
t.Errorf("Expected 'too many connections', got: %v", err)
}
}
case <-timeout:
if errorCount == 0 {
t.Error("Expected at least one connection pool error")
} else {
t.Logf("Received %d connection pool errors as expected", errorCount)
}
return
}
}
}
func TestDBConnection_NetworkPartition(t *testing.T) {
ctx := context.Background()
// 模拟网络分区导致的连接超时
mockDB := &MockDB{
delay: 5 * time.Second, // 很长的延迟模拟网络问题
shouldFail: false,
}
executor := &MockDBExecutor{
db: mockDB,
}
batch := batchflow.NewBatchFlow(ctx, 10, 5, time.Second, executor)
// 提前创建错误通道,期望超时错误
errorChan := batch.ErrorChan(10)
schema := batchflow.NewSQLSchema("test_table", batchflow.ConflictIgnoreOperationConfig, "id", "name")
request := batchflow.NewRequest(schema).
SetInt64("id", 1).
SetString("name", "test")
err := batch.Submit(ctx, request)
if err != nil {
t.Errorf("Submit should not fail immediately: %v", err)
}
select {
case err := <-errorChan:
if err != nil {
t.Logf("Received network-related error: %v", err)
// 网络分区可能导致各种错误,我们只是记录它们
}
case <-time.After(6 * time.Second):
t.Log("No network partition error received, operation might have completed")
}
}