-
Notifications
You must be signed in to change notification settings - Fork 87
/
collector_test.go
169 lines (130 loc) · 5.41 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
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
package main
import (
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
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"
const qFormatInQuotesQuery = "INSERT INTO test (date, args) VALUES"
const qFormatInQuotesValues = "('2019-06-13', 'query=select%20args%20from%20test%20group%20by%20date%20FORMAT%20JSON')"
const qTSNamesTitle = "INSERT INTO table3 (c1, c2, c3) FORMAT TabSeparatedWithNames"
const qNames = "field1 field2 field3"
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, 0, true)
for i := 0; i < 30000; i++ {
c.Push(escTitle, qContent)
}
}
func TestCollector_Push(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000, 0, true)
for i := 0; i < 10400; i++ {
c.Push(escTitle, qContent)
}
assert.Equal(t, 400, c.Tables[escTitle].GetCount())
}
func BenchmarkCollector_ParseQuery(b *testing.B) {
c := NewCollector(&fakeSender{}, 1000, 1000, 0, true)
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, 0, true)
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)
params, content, insert = c.ParseQuery("", qFormatInQuotesQuery+" "+qFormatInQuotesValues)
assert.Equal(t, "query="+url.QueryEscape(qFormatInQuotesQuery), params)
assert.Equal(t, qFormatInQuotesValues, content)
assert.Equal(t, true, insert)
params, content, insert = c.ParseQuery("query="+url.QueryEscape(qFormatInQuotesQuery+" "+qFormatInQuotesValues), "")
assert.Equal(t, "query="+url.QueryEscape(qFormatInQuotesQuery), params)
assert.Equal(t, qFormatInQuotesValues, content)
assert.Equal(t, true, insert)
}
func TestCollector_separateQuery(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000, 0, true)
query, params := c.separateQuery(escParamsAndSelect)
assert.Equal(t, qSelect, query)
assert.Equal(t, qParams, params)
}
func TestTable_getFormat(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000, 0, true)
f := c.getFormat(qTitle)
assert.Equal(t, "TabSeparated", f)
}
func TestTable_CheckFlush(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000, 0, true)
c.Push(qTitle, qContent)
count := 0
for !c.Tables[qTitle].Empty() {
time.Sleep(time.Millisecond * time.Duration(100))
count++
}
assert.True(t, count >= 9)
}
func TestCollector_FlushAll(t *testing.T) {
c := NewCollector(&fakeSender{}, 1000, 1000, 0, true)
c.Push(qTitle, qContent)
c.FlushAll()
}