forked from nikepan/clickhouse-bulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
136 lines (104 loc) · 4.07 KB
/
collector_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
package main
import (
"github.com/stretchr/testify/assert"
"net/url"
"strings"
"testing"
"time"
)
const qTitle = "INSERT INTO table3 (c1, c2, c3) FORMAT TabSeparated"
const qContent = "v11 v12 v13\nv21 v22 v23"
const qValuesTitle = "INSERT INTO table3 (c1, c2, c3) Values"
const qValuesTitleUpper = "INSERT INTO table3 (c1, c2, c3) VALUES"
const qValuesContent = "(v11,v12,v13),(v21,v22,v23)"
const qSelect = "SELECT 1"
const qParams = "user=user&password=111"
const qSelectAndParams = "query=" + qSelect + "&" + qParams
const badEscQuery = "query=INSERT %zdwfr"
var escTitle = url.QueryEscape(qTitle)
var escSelect = url.QueryEscape(qSelect)
var escParamsAndSelect = qParams + "&query=" + escSelect
func BenchmarkCollector_Push(t *testing.B) {
c := NewCollector(&fakeSender{}, 1000, 1000)
for i := 0; i < 30000; i++ {
c.Push(escTitle, qContent)
}
}
func TestCollector_Push(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000)
for i := 0; i < 10400; i++ {
c.Push(escTitle, qContent)
}
assert.Equal(t, c.Tables[escTitle].Count, 800)
}
func BenchmarkCollector_ParseQuery(b *testing.B) {
c := NewCollector(&fakeSender{}, 1000, 1000)
c.ParseQuery("", qTitle+" "+qContent)
c.ParseQuery(qParams, qTitle+" "+qContent)
c.ParseQuery("query="+escTitle, qContent)
c.ParseQuery(qParams+"&query="+escTitle, qContent)
}
func TestCollector_ParseQuery(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000)
var params string
var content string
var insert bool
params, content, insert = c.ParseQuery("", qTitle+" "+qContent)
assert.Equal(t, "query="+escTitle, params)
assert.Equal(t, qContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery(qParams, qTitle+" "+qContent)
assert.Equal(t, qParams+"&query="+escTitle, params)
assert.Equal(t, qContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery("query="+escTitle, qContent)
assert.Equal(t, "query="+escTitle, params)
assert.Equal(t, qContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery(qParams+"&query="+escTitle, qContent)
assert.Equal(t, qParams+"&query="+escTitle, params)
assert.Equal(t, qContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery("query="+escSelect, "")
assert.Equal(t, "query="+escSelect, params)
assert.Equal(t, "", content)
assert.Equal(t, false, insert)
params, content, insert = c.ParseQuery(qSelectAndParams, "")
assert.Equal(t, escParamsAndSelect, params)
assert.Equal(t, "", content)
assert.Equal(t, false, insert)
params, content, insert = c.ParseQuery("query="+url.QueryEscape(qValuesTitle+" "+qValuesContent), "")
assert.Equal(t, "query="+url.QueryEscape(qValuesTitle), params)
assert.Equal(t, qValuesContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery("", qSelect)
assert.Equal(t, "query="+escSelect, params)
assert.Equal(t, "", content)
assert.Equal(t, false, insert)
params, content, insert = c.ParseQuery("", strings.ToLower(qTitle)+" "+qContent)
assert.Equal(t, "query="+strings.ToLower(escTitle), strings.ToLower(params))
assert.Equal(t, qContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery("", strings.ToLower(qValuesTitle)+" "+qValuesContent)
assert.Equal(t, "query="+strings.ToLower(url.QueryEscape(qValuesTitle)), strings.ToLower(params))
assert.Equal(t, qValuesContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery("", qValuesTitleUpper+" "+qValuesContent)
assert.Equal(t, "query="+strings.ToLower(url.QueryEscape(qValuesTitleUpper)), strings.ToLower(params))
assert.Equal(t, qValuesContent, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery(badEscQuery, qValuesTitleUpper+" "+qValuesContent)
assert.False(t, insert)
}
func TestTable_CheckFlush(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1)
c.Push(qTitle, qContent)
for !c.Tables[qTitle].Empty() {
time.Sleep(10)
}
}
func TestCollector_FlushAll(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000)
c.Push(qTitle, qContent)
c.FlushAll()
}