-
Notifications
You must be signed in to change notification settings - Fork 21
/
analyticscomponent_test.go
338 lines (286 loc) · 7.72 KB
/
analyticscomponent_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package gocbcore
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"testing"
"time"
)
type analyticsTestHelper struct {
TestName string
NumDocs int
TestDocs *testDocs
suite *StandardTestSuite
}
func hlpRunAnalyticsQuery(t *testing.T, agent *AgentGroup, opts AnalyticsQueryOptions) ([][]byte, error) {
t.Helper()
resCh := make(chan *AnalyticsRowReader, 1)
errCh := make(chan error, 1)
_, err := agent.AnalyticsQuery(opts, func(reader *AnalyticsRowReader, err error) {
if err != nil {
errCh <- err
return
}
resCh <- reader
})
if err != nil {
return nil, err
}
var rows *AnalyticsRowReader
select {
case err := <-errCh:
return nil, err
case res := <-resCh:
rows = res
}
var rowBytes [][]byte
for {
row := rows.NextRow()
if row == nil {
break
}
rowBytes = append(rowBytes, row)
}
err = rows.Err()
return rowBytes, err
}
func hlpEnsureDataset(t *testing.T, agent *AgentGroup, bucketName string) {
t.Helper()
payloadStr := fmt.Sprintf("{\"statement\":\"CREATE DATASET IF NOT EXISTS `%s` ON `%s`\"}", bucketName, bucketName)
_, err := hlpRunAnalyticsQuery(t, agent, AnalyticsQueryOptions{
Payload: []byte(payloadStr),
Deadline: time.Now().Add(30000 * time.Millisecond),
})
if err != nil {
t.Logf("Error occurred creating dataset: %s\n", err)
}
payloadStr = "{\"statement\":\"CONNECT LINK Local\"}"
_, err = hlpRunAnalyticsQuery(t, agent, AnalyticsQueryOptions{
Payload: []byte(payloadStr),
Deadline: time.Now().Add(30000 * time.Millisecond),
})
if err != nil {
t.Logf("Error occurred connecting link: %s\n", err)
}
}
func (nqh *analyticsTestHelper) testSetup(t *testing.T) {
agent := nqh.suite.DefaultAgent()
ag := nqh.suite.AgentGroup()
nqh.TestDocs = makeTestDocs(t, agent, nqh.TestName, nqh.NumDocs)
hlpEnsureDataset(t, ag, nqh.suite.BucketName)
}
func (nqh *analyticsTestHelper) testCleanup(t *testing.T) {
if nqh.TestDocs != nil {
nqh.TestDocs.Remove()
nqh.TestDocs = nil
}
}
func (nqh *analyticsTestHelper) testBasic(t *testing.T) {
ag := nqh.suite.AgentGroup()
deadline := time.Now().Add(60000 * time.Millisecond)
runTestQuery := func() ([]testDoc, error) {
test := map[string]interface{}{
"statement": fmt.Sprintf("SELECT i,testName FROM %s WHERE testName=\"%s\"", nqh.suite.BucketName, nqh.TestName),
"client_context_id": "1235",
}
payload, err := json.Marshal(test)
if err != nil {
t.Errorf("failed to marshal test payload: %s", err)
}
iterDeadline := time.Now().Add(5000 * time.Millisecond)
if iterDeadline.After(deadline) {
iterDeadline = deadline
}
resCh := make(chan *AnalyticsRowReader)
errCh := make(chan error)
_, err = ag.AnalyticsQuery(AnalyticsQueryOptions{
Payload: payload,
RetryStrategy: nil,
Deadline: iterDeadline,
}, func(reader *AnalyticsRowReader, err error) {
if err != nil {
errCh <- err
return
}
resCh <- reader
})
if err != nil {
return nil, err
}
var rows *AnalyticsRowReader
select {
case err := <-errCh:
return nil, err
case res := <-resCh:
rows = res
}
var docs []testDoc
for {
row := rows.NextRow()
if row == nil {
break
}
var doc testDoc
err := json.Unmarshal(row, &doc)
if err != nil {
return nil, err
}
docs = append(docs, doc)
}
err = rows.Err()
if err != nil {
return nil, err
}
return docs, nil
}
lastError := ""
for {
docs, err := runTestQuery()
if err == nil {
testFailed := false
for _, doc := range docs {
if doc.I < 1 || doc.I > nqh.NumDocs {
lastError = fmt.Sprintf("query test read invalid row i=%d", doc.I)
testFailed = true
}
}
numDocs := len(docs)
if numDocs != nqh.NumDocs {
lastError = fmt.Sprintf("query test read invalid number of rows %d!=%d", numDocs, 5)
testFailed = true
}
if !testFailed {
break
}
} else {
t.Logf("Error occurred running analytics query, will retry: %s\n", err)
}
sleepDeadline := time.Now().Add(1000 * time.Millisecond)
if sleepDeadline.After(deadline) {
sleepDeadline = deadline
}
time.Sleep(sleepDeadline.Sub(time.Now()))
if sleepDeadline == deadline {
t.Errorf("timed out waiting for indexing: %s", lastError)
break
}
}
}
func (suite *StandardTestSuite) TestAnalytics() {
suite.EnsureSupportsFeature(TestFeatureCbas)
helper := &analyticsTestHelper{
TestName: "testAnalyticsQuery",
NumDocs: 5,
suite: suite,
}
if suite.T().Run("setup", helper.testSetup) {
suite.tracer.Reset()
suite.T().Run("Basic", helper.testBasic)
if suite.Assert().Contains(suite.tracer.Spans, nil) {
nilParents := suite.tracer.Spans[nil]
if suite.Assert().GreaterOrEqual(len(nilParents), 1) {
for i := 0; i < len(nilParents); i++ {
suite.AssertHTTPSpan(nilParents[i], "AnalyticsQuery")
}
}
}
suite.VerifyMetrics(suite.meter, "cbas:AnalyticsQuery", 1, true, false)
}
suite.T().Run("cleanup", helper.testCleanup)
}
func (suite *StandardTestSuite) TestAnalyticsCancel() {
suite.EnsureSupportsFeature(TestFeatureCbas)
agent := suite.DefaultAgent()
rt := &roundTripper{delay: 1 * time.Second, tsport: agent.http.cli.Transport}
httpCpt := newHTTPComponentWithClient(
httpComponentProps{},
&http.Client{Transport: rt},
agent.httpMux,
agent.tracer,
)
cbasCpt := newAnalyticsQueryComponent(httpCpt, agent.tracer)
resCh := make(chan *AnalyticsRowReader)
errCh := make(chan error)
payloadStr := `{"statement":"SELECT * FROM test LIMIT 1","client_context_id":"1235"}`
op, err := cbasCpt.AnalyticsQuery(AnalyticsQueryOptions{
Payload: []byte(payloadStr),
Deadline: time.Now().Add(5 * time.Second),
}, func(reader *AnalyticsRowReader, err error) {
if err != nil {
errCh <- err
return
}
resCh <- reader
})
if err != nil {
suite.T().Fatalf("Failed to execute query %s", err)
}
op.Cancel()
var rows *AnalyticsRowReader
var resErr error
select {
case err := <-errCh:
resErr = err
case res := <-resCh:
rows = res
}
if rows != nil {
suite.T().Fatal("Received rows but should not have")
}
if !errors.Is(resErr, ErrRequestCanceled) {
suite.T().Fatalf("Error should have been request canceled but was %s", resErr)
}
if suite.Assert().Contains(suite.tracer.Spans, nil) {
nilParents := suite.tracer.Spans[nil]
if suite.Assert().GreaterOrEqual(len(nilParents), 1) {
for i := 0; i < len(nilParents); i++ {
suite.AssertHTTPSpan(nilParents[i], "AnalyticsQuery")
}
}
}
suite.VerifyMetrics(suite.meter, "cbas:AnalyticsQuery", 1, false, false)
}
func (suite *StandardTestSuite) TestAnalyticsTimeout() {
suite.EnsureSupportsFeature(TestFeatureCbas)
ag := suite.AgentGroup()
resCh := make(chan *AnalyticsRowReader)
errCh := make(chan error)
payloadStr := fmt.Sprintf(`{"statement":"SELECT * FROM %s LIMIT 1","client_context_id":"12345"}`, suite.BucketName)
_, err := ag.AnalyticsQuery(AnalyticsQueryOptions{
Payload: []byte(payloadStr),
Deadline: time.Now().Add(1 * time.Microsecond),
}, func(reader *AnalyticsRowReader, err error) {
if err != nil {
errCh <- err
return
}
resCh <- reader
})
if err != nil {
suite.T().Fatalf("Failed to execute query %s", err)
}
var rows *AnalyticsRowReader
var resErr error
select {
case err := <-errCh:
resErr = err
case res := <-resCh:
rows = res
}
if rows != nil {
suite.T().Fatal("Received rows but should not have")
}
if !errors.Is(resErr, ErrTimeout) {
suite.T().Fatalf("Error should have been timeout but was %s", resErr)
}
if suite.Assert().Contains(suite.tracer.Spans, nil) {
nilParents := suite.tracer.Spans[nil]
if suite.Assert().GreaterOrEqual(len(nilParents), 1) {
if suite.Assert().Equal(len(nilParents), 1) {
suite.AssertHTTPSpanNoDispatch(nilParents[0], "AnalyticsQuery")
}
}
}
suite.VerifyMetrics(suite.meter, "cbas:AnalyticsQuery", 1, false, false)
}