-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_test.go
168 lines (134 loc) · 3.51 KB
/
insert_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
package mtsdb
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"sync/atomic"
"testing"
"time"
)
// mockPool is a mock implementation of pgxpool.Pool.
type mockPool struct {
mock.Mock
}
// SendBatch is a mocked method that returns a mock expectation.
func (m *mockPool) SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults {
args := m.Called(ctx, batch)
data := args.Get(0).(pgx.BatchResults)
return data
}
// mockBatchResults is a mock implementation of pgx.BatchResults.
type mockBatchResults struct {
mock.Mock
}
// Close is a mocked method of pgx.BatchResults.
func (m *mockBatchResults) Close() error {
args := m.Called()
err := args.Error(0)
if args.Error(0) != nil {
return err
}
return nil
}
func (m *mockBatchResults) Exec() (pgconn.CommandTag, error) {
args := m.Called()
err := args.Error(1)
data := args.Get(0).(pgconn.CommandTag)
if args.Error(1) != nil {
return pgconn.CommandTag{}, err
}
return data, nil
}
func (m *mockBatchResults) Query() (pgx.Rows, error) {
args := m.Called()
err := args.Error(1)
data := args.Get(0).(pgx.Rows)
if args.Error(1) != nil {
return data, err
}
return data, nil
}
func (m *mockBatchResults) QueryRow() pgx.Row {
args := m.Called()
data := args.Get(0).(pgx.Row)
return data
}
func TestInsert(t *testing.T) {
t.Parallel()
assert := require.New(t)
mockPool := new(mockPool)
mockBatchResults := new(mockBatchResults)
// Set the expectations
mockPool.On("SendBatch", mock.Anything, mock.Anything).Return(mockBatchResults).Times(2)
mockBatchResults.On("Close").Return(nil).Times(2)
mockBatchResults.On("Exec").Return(pgconn.CommandTag{}, nil).Times(2)
tstConfig := Config{
InsertDuration: 10 * time.Minute,
WorkerPoolSize: 1,
BatchInsertSize: 3,
}
ctx := context.Background()
m, err := newMtsdb(ctx, mockPool, tstConfig)
assert.NoError(err)
c, err := NewMetricCounter(ctx, "testCounter", MetricCounterConfig{}, "label1")
assert.NoError(err)
m.MustRegister(c)
_ = c.Inc("one")
_ = c.Inc("two")
_ = c.Inc("three")
_ = c.Inc("four")
_ = c.Inc("three")
_ = c.Inc("four")
m.Flush()
m.wg.Wait()
mockPool.AssertExpectations(t)
mockBatchResults.AssertExpectations(t)
}
func TestInsertError(t *testing.T) {
t.Parallel()
assert := require.New(t)
mockPool := new(mockPool)
mockBatchResults := new(mockBatchResults)
// Set the expectations
mockPool.On("SendBatch", mock.Anything, mock.Anything).Return(mockBatchResults).Times(2)
mockBatchResults.On("Close").Return(nil).Times(2)
mockBatchResults.On("Exec").Return(pgconn.CommandTag{}, errors.New("test")).Times(2)
tstConfig := Config{
InsertDuration: 10 * time.Minute,
WorkerPoolSize: 1,
BatchInsertSize: 3,
}
ctx := context.Background()
m, err := newMtsdb(ctx, mockPool, tstConfig)
assert.NoError(err)
c, err := NewMetricCounter(ctx, "testCounter", MetricCounterConfig{}, "label1")
assert.NoError(err)
m.MustRegister(c)
insertErrorCnt := atomic.Uint64{}
go func() {
for range m.Errors() {
insertErrorCnt.Add(1)
}
}()
_ = c.Inc("one")
_ = c.Inc("two")
_ = c.Inc("three")
_ = c.Inc("four")
_ = c.Inc("three")
_ = c.Inc("four")
err = m.Close()
assert.NoError(err)
// wait for error insert error to be 2
for i := 0; i < 100; i++ {
if insertErrorCnt.Load() == uint64(2) {
break
}
time.Sleep(2 * time.Millisecond)
}
assert.Equal(uint64(2), insertErrorCnt.Load())
mockPool.AssertExpectations(t)
mockBatchResults.AssertExpectations(t)
}