-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplexed_handler_test.go
112 lines (99 loc) · 3.51 KB
/
multiplexed_handler_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
package delayed_job
import (
"testing"
)
func TestMultiplexedHandlerParameterError(t *testing.T) {
_, e := newMultiplexedHandler(nil,
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "ctx is nil" != e.Error() {
t.Error("excepted error is 'ctx is nil', but actual is", e)
}
_, e = newMultiplexedHandler(map[string]interface{}{},
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "backend in the ctx is required" != e.Error() {
t.Error("excepted error is 'backend in the ctx is required', but actual is", e)
}
_, e = newMultiplexedHandler(map[string]interface{}{"backend": 0},
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "backend in the ctx is not a backend - int" != e.Error() {
t.Error("excepted error is 'backend in the ctx is not a backend - int', but actual is", e)
}
var client *dbBackend = nil
_, e = newMultiplexedHandler(map[string]interface{}{"backend": client},
map[string]interface{}{"rules": []interface{}{}})
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "backend in the ctx is nil" != e.Error() {
t.Error("excepted error is 'backend in the ctx is nil', but actual is", e)
}
_, e = newMultiplexedHandler(map[string]interface{}{}, nil)
if nil == e {
t.Error("excepted error is not nil, but actual is nil")
} else if "params is nil" != e.Error() {
t.Error("excepted error is 'params is nil', but actual is", e)
}
}
func TestMultiplexedHandler(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
multiplexed, e := newMultiplexedHandler(map[string]interface{}{"backend": backend},
map[string]interface{}{"rules": []interface{}{}})
if nil != e {
t.Error(e)
return
}
e = multiplexed.Perform()
if nil != e {
t.Error(e)
return
}
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, actual is ", count)
return
}
})
}
func TestMultiplexedHandler2(t *testing.T) {
backendTest(t, func(backend *dbBackend) {
multiplexed, e := newMultiplexedHandler(map[string]interface{}{"backend": backend}, map[string]interface{}{"priority": 21, "queue": "cc", "rules": []interface{}{
map[string]interface{}{"type": "test", "priority": 23}, map[string]interface{}{"type": "test", "queue": "aa"}}})
if nil != e {
t.Error(e)
return
}
e = multiplexed.Perform()
if nil != e {
t.Error(e)
return
}
assertCount := func(t *testing.T, sql string, excepted int64) {
count := int64(-1)
e := backend.db.QueryRow(sql).Scan(&count)
if nil != e {
t.Error(e)
return
}
if count != excepted {
t.Error("excepted \"", sql, "\" is ", excepted, ", actual is ", count)
}
}
assertCount(t, "SELECT count(*) FROM "+*table_name, 2)
assertCount(t, "SELECT count(*) FROM "+*table_name+" where priority = 23", 1)
assertCount(t, "SELECT count(*) FROM "+*table_name+" where queue = 'aa'", 1)
assertCount(t, "SELECT count(*) FROM "+*table_name+" where queue = 'cc'", 1)
assertCount(t, "SELECT count(*) FROM "+*table_name+" where priority = 21", 1)
assertCount(t, "SELECT count(*) FROM "+*table_name+" where priority = 21 and queue = 'aa'", 1)
assertCount(t, "SELECT count(*) FROM "+*table_name+" where priority = 23 and queue = 'cc'", 1)
})
}