-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_backend_test.go
625 lines (532 loc) · 15.7 KB
/
db_backend_test.go
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package delayed_job
import (
"database/sql"
"flag"
"math"
"net/http"
"os"
"strconv"
"strings"
"testing"
"time"
_ "github.com/microsoft/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/sijms/go-ora/v2"
_ "github.com/ziutek/mymysql/godrv"
_ "gitee.com/chunanyong/dm" // 达梦
)
var (
PostgreSQLUrl = "host=127.0.0.1 user=golang password=123456 dbname=golang sslmode=disable"
MySQLUrl = "golang:123456@tcp(localhost:3306)/golang?autocommit=true&parseTime=true&multiStatements=true"
MsSqlUrl = "sqlserver://golang:[email protected]?database=golang&connection+timeout=30"
DMSqlUrl = "dm://" + os.Getenv("dm_username") + ":" + os.Getenv("dm_password") + "@" + os.Getenv("dm_host") + "?noConvertToHex=true"
)
var (
TestDrv string
TestConnURL string
)
func init() {
flag.StringVar(&TestDrv, "dbDrv", "postgres", "")
flag.StringVar(&TestConnURL, "dbURL", "", "缺省值会根据 dbDrv 的值自动选择,请见 GetTestConnURL()")
//flag.StringVar(&TestConnURL, "dbURL", "golang:123456@tcp(localhost:3306)/golang?autocommit=true&parseTime=true&multiStatements=true", "")
//flag.StringVar(&TestConnURL, "dbURL", "sqlserver://golang:[email protected]?database=golang&connection+timeout=30", "")
}
func GetTestConnDrv() string {
return TestDrv
}
func GetTestConnURL() string {
if TestConnURL == "" {
switch TestDrv {
case "postgres", "":
return PostgreSQLUrl
case "mysql":
return MySQLUrl
case "sqlserver", "mssql":
return MsSqlUrl
case "dm":
return DMSqlUrl
}
}
return TestConnURL
}
func backendTest(t *testing.T, cb func(backend *dbBackend)) {
// old_mode := *run_mode
// *run_mode = "init_db"
// defer func() {
// *run_mode = old_mode
// }()
e := Main("init_db", GetTestConnDrv(), GetTestConnURL(), func(http.Handler) {})
if nil != e {
t.Error(e)
return
}
backend, e := newBackend(GetTestConnDrv(), GetTestConnURL(), nil)
if nil != e {
t.Error(e)
return
}
defer backend.Close()
// _, e = backend.db.Exec(`
// DROP TABLE IF EXISTS ` + *table_name + `;
// CREATE TABLE IF NOT EXISTS ` + *table_name + ` (
// id SERIAL PRIMARY KEY,
// priority int DEFAULT 0,
// attempts int DEFAULT 0,
// queue varchar(200),
// handler text NOT NULL,
// handler_id varchar(200),
// last_error varchar(2000),
// run_at timestamp,
// locked_at timestamp,
// failed_at timestamp,
// locked_by varchar(200),
// created_at timestamp NOT NULL,
// updated_at timestamp NOT NULL
// );`)
// if nil != e {
// t.Error(e)
// return
// }
cb(backend)
}
func TestEnqueue(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
row := backend.db.QueryRow("SELECT id, priority, attempts, queue, handler, handler_id, last_error, run_at, locked_at, failed_at, locked_by, created_at, updated_at FROM " + *table_name)
job := &Job{}
var queue sql.NullString
var last_error sql.NullString
var run_at NullTime
var locked_at NullTime
var failed_at NullTime
var locked_by sql.NullString
var handler NullString
var created_at NullTime
var updated_at NullTime
e = row.Scan(
&job.id,
&job.priority,
&job.attempts,
&queue,
&handler,
&job.handler_id,
&last_error,
&run_at,
&locked_at,
&failed_at,
&locked_by,
&created_at,
&updated_at)
if nil != e {
t.Error(e)
return
}
if handler.Valid {
job.handler = handler.String
}
if job.priority != 1 {
t.Error("excepted priority is 1, actual is ", job.priority)
}
if job.attempts != 0 {
t.Error("excepted attempts is 0, actual is ", job.attempts)
}
if queue.Valid && queue.String != "aa" {
t.Error("excepted queue is 'aa', actual is ", queue.String)
}
if !strings.Contains(job.handler, "\"type\": \"test\"") {
t.Error("excepted handler is 'aa', actual is ", job.handler)
}
if 0 == len(job.handler_id) {
t.Error("excepted handler_id is not empty, actual is ", job.handler_id)
}
if last_error.Valid && 0 != len(last_error.String) {
t.Error("excepted last_error is valid, actual is ", last_error.String)
}
if locked_at.Valid && !locked_at.Time.IsZero() && locked_at.Time.Format("2006-01-02") != "0001-01-01" {
t.Error("excepted locked_at is invalid actual is ", locked_at.Time)
}
if failed_at.Valid && !failed_at.Time.IsZero() && failed_at.Time.Format("2006-01-02") != "0001-01-01" {
t.Error("excepted failed_at is invalid, actual is ", failed_at.Time)
}
if locked_by.Valid && 0 != len(locked_by.String) {
t.Error("excepted locked_by is invalid, actual is ", locked_by.String)
}
if !created_at.Valid || created_at.Time.IsZero() || created_at.Time.Format("2006-01-02") == "0001-01-01" {
t.Error("excepted created_at is invalid actual is ", created_at.Time)
}
if !updated_at.Valid || updated_at.Time.IsZero() || updated_at.Time.Format("2006-01-02") == "0001-01-01" {
t.Error("excepted updated_at is valid, actual is ", updated_at.Time)
}
select {
case <-test_chan:
t.Error("unexcepted recv")
default:
}
})
}
func TestGetSimple(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
for i := 0; i < 10; i++ {
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
}
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil == job {
t.Error("excepted job is not nil, actual is nil")
return
}
row := backend.db.QueryRow("SELECT locked_at, locked_by FROM " + *table_name + " where id = " + strconv.FormatInt(job.id, 10))
var locked_at NullTime
var locked_by sql.NullString
e = row.Scan(&locked_at, &locked_by)
if nil != e {
t.Error(e)
return
}
if !locked_at.Valid {
t.Error("excepted locked_at is not empty, actual is invalid")
}
if !locked_by.Valid {
t.Error("excepted locked_by is not empty, actual is invalid")
}
if math.Abs(float64(locked_at.Time.Unix()-backend.db_time_now().Unix())) > 10 {
t.Log(locked_at.Time, backend.db_time_now())
t.Error("excepted locked_at is now, actual is", locked_at.Time)
}
if w.name != locked_by.String {
t.Error("excepted locked_at is now, actual is", locked_at.Time)
}
if job.priority != 1 {
t.Error("excepted priority is 1, actual is ", job.priority)
}
if job.attempts != 0 {
t.Error("excepted attempts is 0, actual is ", job.attempts)
}
if job.queue != "aa" {
t.Error("excepted queue is 'aa', actual is ", job.queue)
}
if !strings.Contains(job.handler, "\"type\": \"test\"") {
t.Error("excepted handler is 'aa', actual is ", job.handler)
}
if 0 == len(job.handler_id) {
t.Error("excepted handler_id is not empty, actual is ", job.handler_id)
}
if job.last_error != "" {
t.Error("excepted last_error is invalid, actual is ", job.last_error)
}
if !job.failed_at.IsZero() {
t.Error("excepted failed_at is invalid, actual is ", job.failed_at)
}
if "postgres" != backend.drv {
if !job.locked_at.IsZero() {
t.Error("excepted locked_at is invalid actual is ", job.locked_at)
}
if "" != job.locked_by {
t.Error("excepted locked_by is invalid, actual is ", job.locked_by)
}
}
select {
case <-test_chan:
t.Error("unexcepted recv")
default:
}
row = backend.db.QueryRow("SELECT count(*) FROM " + *table_name + " where locked_by is NULL AND locked_at is NULL")
var count int = 0
e = row.Scan(&count)
if nil != e {
t.Error(e)
return
}
if 9 != count {
t.Error("excepted read 1, actual is ", 10-count)
}
})
}
func TestGetWithLocked(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
if strings.Contains(GetTestConnDrv(), "odbc_with_mssql") {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET locked_at = SYSUTCDATETIME(), locked_by = 'aa'")
} else {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET locked_at = now(), locked_by = 'aa'")
}
if nil != e {
t.Error(e)
return
}
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil != job {
t.Error("excepted job is nil, actual is not nil")
return
}
})
}
func TestGetWithFailed(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
if strings.Contains(GetTestConnDrv(), "odbc_with_mssql") {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET failed_at = SYSUTCDATETIME(), last_error = 'aa'")
} else {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET failed_at = now(), last_error = 'aa'")
}
if nil != e {
t.Error(e)
return
}
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil != job {
t.Error("excepted job is nil, actual is not nil")
return
}
})
}
func TestLockedJobInGet(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
if "postgres" == backend.drv {
t.Skip("postgres is skipped.")
}
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
is_test_for_lock = true
defer func() {
is_test_for_lock = false
}()
go func() {
<-test_ch_for_lock
var e error
if strings.Contains(GetTestConnDrv(), "odbc_with_mssql") {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET locked_at = SYSUTCDATETIME(), locked_by = 'aa'")
} else {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET locked_at = now(), locked_by = 'aa'")
}
if nil != e {
t.Error(e)
}
test_ch_for_lock <- 1
}()
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil != job {
t.Error("excepted job is nil, actual is not nil")
return
}
})
}
func TestFailedJobInGet(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
if "postgres" == backend.drv {
t.Skip("postgres is skipped.")
}
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
is_test_for_lock = true
defer func() {
is_test_for_lock = false
}()
go func() {
<-test_ch_for_lock
var e error
if strings.Contains(GetTestConnDrv(), "odbc_with_mssql") {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET failed_at = SYSUTCDATETIME(), last_error = 'aa'")
} else {
_, e = backend.db.Exec("UPDATE " + *table_name + " SET failed_at = now(), last_error = 'aa'")
}
if nil != e {
t.Error(e)
return
}
test_ch_for_lock <- 1
}()
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil != job {
t.Error("excepted job is nil, actual is not nil")
return
}
})
}
func TestDestory(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil == job {
t.Error("excepted job is not nil, actual is nil")
return
}
job.destroyIt()
count := int64(-1)
e = backend.db.QueryRow("SELECT count(*) FROM " + *table_name + "").Scan(&count)
if nil != e {
t.Error(e)
return
}
if count != 0 {
t.Error("excepted job is empty after destory it, actual is ", count)
return
}
})
}
func TestFailIt(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil == job {
t.Error("excepted job is not nil, actual is nil")
return
}
e = job.failIt("1234")
if nil != e {
t.Error(e)
return
}
row := backend.db.QueryRow("SELECT failed_at, last_error FROM " + *table_name + " where id = " + strconv.FormatInt(job.id, 10))
var failed_at NullTime
var last_error sql.NullString
e = row.Scan(&failed_at, &last_error)
if nil != e {
t.Error(e)
return
}
if !failed_at.Valid {
t.Error("excepted failed_at is not empty, actual is invalid")
} else if math.Abs(float64(failed_at.Time.Unix()-backend.db_time_now().Unix())) > 10 {
t.Error("excepted failed_at is now, actual is", failed_at.Time)
}
if !last_error.Valid {
t.Error("excepted last_error is not empty, actual is invalid")
} else if "1234" != last_error.String {
t.Error("excepted last_error is '1234', and actual is ", last_error.String)
}
})
}
func TestRescheduleIt(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
e := backend.enqueue(1, 0, "", 0, "aa", time.Time{}, map[string]interface{}{"type": "test"})
if nil != e {
t.Error(e)
return
}
w := &worker{min_priority: -1, max_priority: -1, name: "aa_pid:123", max_run_time: 1 * time.Minute}
job, e := backend.reserve(w)
if nil != e {
t.Error(e)
return
}
if nil == job {
t.Error("excepted job is not nil, actual is nil")
return
}
now := backend.db_time_now()
job.will_update_attributes()["@handler"] = map[string]interface{}{"type": "test", "aa": "testsss"}
e = job.rescheduleIt(now, "throw s")
if nil != e {
t.Error(e)
return
}
row := backend.db.QueryRow("SELECT attempts, run_at, locked_at, locked_by, handler, last_error FROM " + *table_name + " where id = " + strconv.FormatInt(job.id, 10))
var attempts int64
var run_at NullTime
var locked_at NullTime
var locked_by sql.NullString
var handler NullString
var last_error sql.NullString
e = row.Scan(&attempts, &run_at, &locked_at, &locked_by, &handler, &last_error)
if nil != e {
t.Error(e)
return
}
if !run_at.Valid {
t.Error("excepted run_at is valid, actual is invalid")
}
if locked_at.Valid && !locked_at.Time.IsZero() {
t.Error("excepted locked_at is invalid, actual is valid")
}
if locked_by.Valid {
t.Error("excepted locked_by is invalid, actual is valid")
}
if !handler.Valid {
t.Error("excepted handler is not empty, actual is invalid")
}
if !last_error.Valid {
t.Error("excepted last_error is not empty, actual is invalid")
}
if 1 != attempts {
t.Error("excepted attempts is '1', and actual is ", attempts)
}
if math.Abs(float64(run_at.Time.Unix()-now.Unix())) > 2 {
t.Error("excepted run_at is ", run_at.Time, ", actual is", now)
}
if !strings.Contains(handler.String, "\"type\": \"test\"") {
t.Error("excepted handler contains '\"type\": \"test\"', actual is ", handler.String)
}
if !strings.Contains(handler.String, "\"aa\": \"testsss\"") {
t.Error("excepted handler is '\"aa\": \"testsss\"', actual is ", handler.String)
}
if last_error.String != "throw s" {
t.Error("excepted last_error is 'throw s', actual is ", last_error.String)
}
})
}