-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
183 lines (158 loc) · 4.17 KB
/
integration_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
// +build integration
package ballistic_test
import (
"context"
"database/sql"
"encoding/json"
_ "github.com/ClickHouse/clickhouse-go"
"github.com/farwydi/ballistic/sender"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
)
var b2i = map[bool]int8{
false: 0,
true: 1,
}
// testStruct1
type testStruct1 struct {
RecordTime time.Time `json:"record_time"`
StringVal string `json:"string_val"`
BoolVar bool `json:"bool_var"`
Int32Val int32 `json:"int_32_val"`
UInt64Val uint64 `json:"u_int_64_val"`
IntVal int `json:"int_val"`
Float32Val float64 `json:"float_32_val"`
Float64Val float64 `json:"float_64_val"`
}
func (t testStruct1) MarshalBinary() (data []byte, err error) {
return json.Marshal(t)
}
func (t *testStruct1) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, t)
}
func (t *testStruct1) SQL() string {
return "INSERT INTO test.table_1" +
"(record_time, string_val, bool_var, int_32_val, u_int_64_val, int_val, float_32_val, float_64_val)" +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
}
func (t *testStruct1) ToExec() []interface{} {
return []interface{}{
t.RecordTime,
t.StringVal,
b2i[t.BoolVar],
t.Int32Val,
t.UInt64Val,
t.IntVal,
t.Float32Val,
t.Float64Val,
}
}
// testStruct2
type testStruct2 struct {
RecordTime time.Time `json:"record_time"`
StringVal string `json:"string_val"`
BoolVar bool `json:"bool_var"`
Int32Val int32 `json:"int_32_val"`
UInt64Val uint64 `json:"u_int_64_val"`
IntVal int `json:"int_val"`
Float32Val float64 `json:"float_32_val"`
Float64Val float64 `json:"float_64_val"`
}
func (t testStruct2) MarshalBinary() (data []byte, err error) {
return json.Marshal(t)
}
func (t *testStruct2) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, t)
}
func (t *testStruct2) SQL() string {
return "INSERT INTO test.table_2" +
"(record_time, string_val, bool_var, int_32_val, u_int_64_val, int_val, float_32_val, float_64_val)" +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
}
func (t *testStruct2) ToExec() []interface{} {
return []interface{}{
t.RecordTime,
t.StringVal,
b2i[t.BoolVar],
t.Int32Val,
t.UInt64Val,
t.IntVal,
t.Float32Val,
t.Float64Val,
}
}
func TestSenderInRealDatabase(t *testing.T) {
time.Local = time.UTC
tempDir, err := ioutil.TempDir("", "ballistic")
require.NoError(t, err)
defer func() {
// TODO: remove temp dir
}()
conn, err := sql.Open("clickhouse",
"tcp://localhost:9000?&database=test&read_timeout=10&write_timeout=20")
require.NoError(t, err)
s := sender.NewSender(conn, sender.Config{
UseMemoryFallback: true,
FileWorkspace: tempDir,
SendLimit: 1000,
SendInterval: 100 * time.Millisecond,
})
go s.RunPusher(context.Background())
var it int32
var wg sync.WaitGroup
for n := 0; n < 10; n++ {
wg.Add(1)
go func() {
var err error
defer wg.Done()
for i := 0; i < 2000; i++ {
err = s.Push(&testStruct1{
RecordTime: time.Now(),
StringVal: "test",
BoolVar: true,
Int32Val: rand.Int31(),
UInt64Val: rand.Uint64(),
IntVal: rand.Int(),
Float32Val: rand.Float64(),
Float64Val: rand.Float64(),
})
assert.NoError(t, err)
err = s.Push(&testStruct2{
RecordTime: time.Now(),
StringVal: "test",
BoolVar: true,
Int32Val: rand.Int31(),
UInt64Val: rand.Uint64(),
IntVal: rand.Int(),
Float32Val: rand.Float64(),
Float64Val: rand.Float64(),
})
assert.NoError(t, err)
atomic.AddInt32(&it, 1)
if atomic.LoadInt32(&it)%500 == 0 {
time.Sleep(50 * time.Millisecond)
t.Logf("insert: %d", atomic.LoadInt32(&it))
}
}
}()
}
wg.Wait()
s.Stop(true)
// test data
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
var count int
err = conn.QueryRowContext(ctx, `SELECT count(1) FROM test.table_1`).Scan(&count)
assert.NoError(t, err)
assert.EqualValues(t, count, it)
err = conn.QueryRowContext(ctx, `SELECT count(1) FROM test.table_2`).Scan(&count)
assert.NoError(t, err)
assert.EqualValues(t, count, it)
}