-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatches_test.go
85 lines (64 loc) · 1.87 KB
/
batches_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
package yadb
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
clickhouse "github.com/undiabler/clickhouse-driver"
)
var calls = 0
func TestBatches(t *testing.T) {
var err error
_, err = NewBatchWriter("events", []string{"uuid", "event_type", "event_time", "event_date"}, 1, time.Second)
assert.Error(t, err)
_, err = NewBatchWriter("events", []string{}, 5, time.Second)
assert.Error(t, err)
}
func TestWriterErrors(t *testing.T) {
tr := &badTransport{err: errors.New("Error inserting"), calls: 0}
conn := clickhouse.NewConn("host.local", tr)
bw, err := NewBatchWriter("events", []string{"uuid", "event_type", "event_time", "event_date"}, 5, time.Second)
if err != nil {
t.Fatal(err)
}
// skip insert
bw.InsertMap(map[string]interface{}{
"uuid": "prog_test3",
"event_type": 4,
"event_date": time.Now().Format("2006-01-02"),
"event_time": time.Now().Format("2006-01-02 15:04:05"),
})
time.Sleep(2 * time.Second)
assert.Equal(t, 0, tr.calls)
bw.SetConn(func() clickhouse.Connector {
return nil
})
// no conn
bw.InsertMap(map[string]interface{}{
"uuid": "prog_test3",
"event_type": 4,
"event_date": time.Now().Format("2006-01-02"),
"event_time": time.Now().Format("2006-01-02 15:04:05"),
})
time.Sleep(11 * time.Second)
assert.Equal(t, 0, tr.calls)
bw.SetConn(func() clickhouse.Connector {
return conn
})
bw.InsertMap(map[string]interface{}{
"uuid": "prog_test3",
"event_type": 4,
"event_date": time.Now().Format("2006-01-02"),
"event_time": time.Now().Format("2006-01-02 15:04:05"),
})
time.Sleep(11 * time.Second)
assert.Equal(t, 10, tr.calls)
CloseAll()
erre := bw.InsertMap(map[string]interface{}{
"uuid": "prog_test3",
"event_type": 4,
"event_date": time.Now().Format("2006-01-02"),
"event_time": time.Now().Format("2006-01-02 15:04:05"),
})
assert.Error(t, erre)
}