-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_query_test.go
638 lines (515 loc) · 17.6 KB
/
cluster_query_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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
package gocb
import (
"bytes"
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
"time"
"github.com/couchbaselabs/gocbcore8alpha"
)
func TestQuery(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
if globalCluster.NotSupportsFeature(N1qlFeature) {
t.Skip("Skipping test as n1ql not supported.")
}
if globalTravelBucket == nil {
t.Skip("Skipping test as no travel-sample bucket")
}
t.Run("testSimpleQuery", testSimpleQuery)
t.Run("testPreparedQuery", testPreparedQuery)
t.Run("testQueryNamedParameters", testQueryNamedParameters)
t.Run("testQueryPositionalParameters", testQueryPositionalParameters)
}
func testSimpleQuery(t *testing.T) {
query := "SELECT `travel-sample`.* FROM `travel-sample` LIMIT 10;"
rows, err := globalCluster.Query(query, nil)
if err != nil {
t.Fatalf("Failed to execute query %v", err)
}
var samples []interface{}
var sample interface{}
for rows.Next(&sample) {
samples = append(samples, sample)
}
err = rows.Close()
if err != nil {
t.Fatalf("Rows close had error: %v", err)
}
if len(samples) != 10 {
t.Fatalf("Expected result to contain 10 documents but had %d", len(samples))
}
if rows.RequestID() == "" {
t.Fatalf("Result should have had non empty RequestID")
}
if rows.SourceEndpoint() == "" {
t.Fatalf("Result should have had non empty SourceEndpoint")
}
}
func testPreparedQuery(t *testing.T) {
query := "SELECT `travel-sample`.* FROM `travel-sample` LIMIT 10;"
rows, err := globalCluster.Query(query, &QueryOptions{Prepared: true})
if err != nil {
t.Fatalf("Failed to execute query %v", err)
}
var samples []interface{}
var sample interface{}
for rows.Next(&sample) {
samples = append(samples, sample)
}
err = rows.Close()
if err != nil {
t.Fatalf("Rows close had error: %v", err)
}
if len(samples) != 10 {
t.Fatalf("Expected result to contain 10 documents but had %d", len(samples))
}
if rows.RequestID() == "" {
t.Fatalf("Result should have had non empty RequestID")
}
if rows.SourceEndpoint() == "" {
t.Fatalf("Result should have had non empty SourceEndpoint")
}
if globalCluster.queryCache[query] == nil {
t.Fatalf("Query should have been in query cache after prepared statement execution")
}
}
func testQueryNamedParameters(t *testing.T) {
query := "SELECT `travel-sample`.* FROM `travel-sample` where `type`=$type AND `name`=$name LIMIT 10;"
params := make(map[string]interface{}, 1)
params["type"] = "hotel"
params["name"] = "Medway Youth Hostel"
rows, err := globalCluster.Query(query, &QueryOptions{NamedParameters: params})
if err != nil {
t.Fatalf("Failed to execute query %v", err)
}
var samples []interface{}
var sample interface{}
for rows.Next(&sample) {
samples = append(samples, sample)
}
err = rows.Close()
if err != nil {
t.Fatalf("Rows close had error: %v", err)
}
if len(samples) != 1 {
t.Fatalf("Expected breweries to contain 1 document but had %d", len(samples))
}
if rows.RequestID() == "" {
t.Fatalf("Result should have had non empty RequestID")
}
if rows.SourceEndpoint() == "" {
t.Fatalf("Result should have had non empty SourceEndpoint")
}
}
func testQueryPositionalParameters(t *testing.T) {
query := "SELECT `travel-sample`.* FROM `travel-sample` where `type`=? AND `name`=? LIMIT 10;"
rows, err := globalCluster.Query(query, &QueryOptions{PositionalParameters: []interface{}{"hotel", "Medway Youth Hostel"}})
if err != nil {
t.Fatalf("Failed to execute query %v", err)
}
var samples []interface{}
var sample interface{}
for rows.Next(&sample) {
samples = append(samples, sample)
}
err = rows.Close()
if err != nil {
t.Fatalf("Rows close had error: %v", err)
}
if len(samples) != 1 {
t.Fatalf("Expected breweries to contain 1 document but had %d", len(samples))
}
if rows.RequestID() == "" {
t.Fatalf("Result should have had non empty RequestID")
}
if rows.SourceEndpoint() == "" {
t.Fatalf("Result should have had non empty SourceEndpoint")
}
}
func TestBasicQuery(t *testing.T) {
dataBytes, err := loadRawTestDataset("beer_sample_query_dataset")
if err != nil {
t.Fatalf("Could not read test dataset: %v", err)
}
var expectedResult n1qlResponse
err = json.Unmarshal(dataBytes, &expectedResult)
if err != nil {
t.Fatalf("Failed to unmarshal dataset %v", err)
}
queryOptions := &QueryOptions{
PositionalParameters: []interface{}{"brewery"},
}
statement := "select `beer-sample`.* from `beer-sample` WHERE `type` = ? ORDER BY brewery_id, name"
timeout := 60 * time.Second
doHTTP := func(req *gocbcore.HttpRequest) (*gocbcore.HttpResponse, error) {
testAssertQueryRequest(t, req)
var opts map[string]interface{}
err := json.Unmarshal(req.Body, &opts)
if err != nil {
t.Fatalf("Failed to unmarshal request body %v", err)
}
if len(opts) != 3 {
t.Fatalf("Expected request body to contain 3 options but was %d, %v", len(opts), opts)
}
optsStatement, ok := opts["statement"]
if !ok {
t.Fatalf("Request query options missing statement")
}
if optsStatement != statement {
t.Fatalf("Expected statement to be %s but was %s", statement, optsStatement)
}
optsTimeout, ok := opts["timeout"]
if !ok {
t.Fatalf("Request query options missing timeout")
}
optsDuration, err := time.ParseDuration(optsTimeout.(string))
if err != nil {
t.Fatalf("Failed to parse request timeout %v", err)
}
if optsDuration < (timeout-50*time.Millisecond) || optsDuration > (timeout+50*time.Millisecond) {
t.Fatalf("Expected timeout to be %s but was %s", timeout, optsDuration)
}
optsParams, ok := opts["args"].([]interface{})
if !ok {
t.Fatalf("Request query options missing args")
}
if len(optsParams) != 1 {
t.Fatalf("Expected args to be length 1 but was %d", len(optsParams))
}
if optsParams[0] != "brewery" {
t.Fatalf("Expected args content to be brewery but was %s", optsParams[0])
}
return &gocbcore.HttpResponse{
Endpoint: "http://localhost:8093",
StatusCode: 200,
Body: &testReadCloser{bytes.NewBuffer(dataBytes), nil},
}, nil
}
provider := &mockHTTPProvider{
doFn: doHTTP,
}
cluster := testGetClusterForHTTP(provider, timeout, 0, 0)
res, err := cluster.Query(statement, queryOptions)
if err != nil {
t.Fatal(err)
}
testAssertQueryResult(t, &expectedResult, res, true)
}
func TestQueryError(t *testing.T) {
dataBytes, err := loadRawTestDataset("beer_sample_query_error")
if err != nil {
t.Fatalf("Could not read test dataset: %v", err)
}
var expectedResult n1qlResponse
err = json.Unmarshal(dataBytes, &expectedResult)
if err != nil {
t.Fatalf("Failed to unmarshal dataset %v", err)
}
queryOptions := &QueryOptions{
PositionalParameters: []interface{}{"brewery"},
}
statement := "select `beer-sample`.* from `beer-sample` WHERE `type` = ? ORDER BY brewery_id, name"
timeout := 60 * time.Second
doHTTP := func(req *gocbcore.HttpRequest) (*gocbcore.HttpResponse, error) {
testAssertQueryRequest(t, req)
return &gocbcore.HttpResponse{
Endpoint: "http://localhost:8093",
StatusCode: 400,
Body: &testReadCloser{bytes.NewBuffer(dataBytes), nil},
}, nil
}
provider := &mockHTTPProvider{
doFn: doHTTP,
}
cluster := testGetClusterForHTTP(provider, timeout, 0, 0)
res, err := cluster.Query(statement, queryOptions)
if err == nil {
t.Fatal("Expected query to return error")
}
if res != nil {
t.Fatalf("Expected result to be nil but was %v", res)
}
queryErrs, ok := err.(QueryErrors)
if !ok {
t.Fatalf("Expected error to be QueryErrors but was %s", reflect.TypeOf(err).String())
}
if queryErrs.Endpoint() != "localhost:8093" {
t.Fatalf("Expected error endpoint to be localhost:8093 but was %s", queryErrs.Endpoint())
}
if queryErrs.HTTPStatus() != 400 {
t.Fatalf("Expected error HTTP status to be 400 but was %d", queryErrs.HTTPStatus())
}
if queryErrs.ContextID() != expectedResult.ClientContextID {
t.Fatalf("Expected error ContextID to be %s but was %s", expectedResult.ClientContextID, queryErrs.ContextID())
}
if len(queryErrs.Errors()) != len(expectedResult.Errors) {
t.Fatalf("Expected errors to contain 1 error but contained %d", len(queryErrs.Errors()))
}
var errs []string
errors := queryErrs.Errors()
for i, err := range expectedResult.Errors {
msg := fmt.Sprintf("[%d] %s", err.ErrorCode, err.ErrorMessage)
errs = append(errs, msg)
if errors[i].Code() != err.ErrorCode {
t.Fatalf("Expected error code to be %d but was %d", errors[i].Code(), err.ErrorCode)
}
if errors[i].Message() != err.ErrorMessage {
t.Fatalf("Expected error message to be %s but was %s", errors[i].Message(), err.ErrorMessage)
}
if errors[i].Error() != msg {
t.Fatalf("Expected error Error() to be %s but was %s", errors[i].Error(), msg)
}
}
joinedErrs := strings.Join(errs, ", ")
if queryErrs.Error() != joinedErrs {
t.Fatalf("Expected error Error() to be %s but was %s", joinedErrs, queryErrs.Error())
}
}
func TestQueryServiceNotFound(t *testing.T) {
doHTTP := func(req *gocbcore.HttpRequest) (*gocbcore.HttpResponse, error) {
return nil, gocbcore.ErrNoN1qlService
}
provider := &mockHTTPProvider{
doFn: doHTTP,
}
statement := "select `beer-sample`.* from `beer-sample` WHERE `type` = ? ORDER BY brewery_id, name"
timeout := 60 * time.Second
cluster := testGetClusterForHTTP(provider, timeout, 0, 0)
res, err := cluster.Query(statement, nil)
if err == nil {
t.Fatal("Expected query to return error")
}
if res != nil {
t.Fatalf("Expected result to be nil but was %v", res)
}
if !IsServiceNotFoundError(err) {
t.Fatalf("Expected error to be ServiceNotFoundError but was %s", reflect.TypeOf(err).Name())
}
}
func TestQueryTimeout(t *testing.T) {
statement := "select `beer-sample`.* from `beer-sample` WHERE `type` = ? ORDER BY brewery_id, name"
timeout := 40 * time.Second
clusterTimeout := 50 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
doHTTP := func(req *gocbcore.HttpRequest) (*gocbcore.HttpResponse, error) {
testAssertQueryRequest(t, req)
var opts map[string]interface{}
err := json.Unmarshal(req.Body, &opts)
if err != nil {
t.Fatalf("Failed to unmarshal request body %v", err)
}
optsTimeout, ok := opts["timeout"]
if !ok {
t.Fatalf("Request query options missing timeout")
}
dur, err := time.ParseDuration(optsTimeout.(string))
if err != nil {
t.Fatalf("Could not parse timeout: %v", err)
}
if dur < (timeout-50*time.Millisecond) || dur > (timeout+50*time.Millisecond) {
t.Fatalf("Expected timeout to be %s but was %s", timeout.String(), optsTimeout)
}
cancel()
return nil, context.DeadlineExceeded
}
provider := &mockHTTPProvider{
doFn: doHTTP,
}
cluster := testGetClusterForHTTP(provider, clusterTimeout, 0, 0)
_, err := cluster.Query(statement, &QueryOptions{
Timeout: timeout,
Context: ctx,
})
if err != nil && !IsTimeoutError(err) {
t.Fatal(err)
}
}
func TestQueryContextTimeout(t *testing.T) {
statement := "select `beer-sample`.* from `beer-sample` WHERE `type` = ? ORDER BY brewery_id, name"
timeout := 50 * time.Second
clusterTimeout := 50 * time.Second
ctxTimeout := 30 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout)
defer cancel()
doHTTP := func(req *gocbcore.HttpRequest) (*gocbcore.HttpResponse, error) {
testAssertQueryRequest(t, req)
var opts map[string]interface{}
err := json.Unmarshal(req.Body, &opts)
if err != nil {
t.Fatalf("Failed to unmarshal request body %v", err)
}
optsTimeout, ok := opts["timeout"]
if !ok {
t.Fatalf("Request query options missing timeout")
}
dur, err := time.ParseDuration(optsTimeout.(string))
if err != nil {
t.Fatalf("Could not parse timeout: %v", err)
}
if dur < (ctxTimeout-50*time.Millisecond) || dur > (ctxTimeout+50*time.Millisecond) {
t.Fatalf("Expected timeout to be %s but was %s", ctxTimeout.String(), optsTimeout)
}
cancel()
return nil, context.DeadlineExceeded
}
provider := &mockHTTPProvider{
doFn: doHTTP,
}
cluster := testGetClusterForHTTP(provider, clusterTimeout, 0, 0)
_, err := cluster.Query(statement, &QueryOptions{
Timeout: timeout,
Context: ctx,
})
if err != nil && !IsTimeoutError(err) {
t.Fatal(err)
}
}
func TestQueryClusterTimeout(t *testing.T) {
statement := "select `beer-sample`.* from `beer-sample` WHERE `type` = ? ORDER BY brewery_id, name"
timeout := 50 * time.Second
clusterTimeout := 10 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
doHTTP := func(req *gocbcore.HttpRequest) (*gocbcore.HttpResponse, error) {
testAssertQueryRequest(t, req)
var opts map[string]interface{}
err := json.Unmarshal(req.Body, &opts)
if err != nil {
t.Fatalf("Failed to unmarshal request body %v", err)
}
optsTimeout, ok := opts["timeout"]
if !ok {
t.Fatalf("Request query options missing timeout")
}
dur, err := time.ParseDuration(optsTimeout.(string))
if err != nil {
t.Fatalf("Could not parse timeout: %v", err)
}
if dur < (clusterTimeout-50*time.Millisecond) || dur > (clusterTimeout+50*time.Millisecond) {
t.Fatalf("Expected timeout to be %s but was %s", clusterTimeout.String(), optsTimeout)
}
cancel()
return nil, context.DeadlineExceeded
}
provider := &mockHTTPProvider{
doFn: doHTTP,
}
cluster := testGetClusterForHTTP(provider, clusterTimeout, 0, 0)
_, err := cluster.Query(statement, &QueryOptions{
Timeout: timeout,
Context: ctx,
})
if err != nil && !IsTimeoutError(err) {
t.Fatal(err)
}
}
func testAssertQueryRequest(t *testing.T, req *gocbcore.HttpRequest) {
if req.Service != gocbcore.N1qlService {
t.Fatalf("Service should have been N1qlService but was %d", req.Service)
}
if req.Context == nil {
t.Fatalf("Context should not have been nil, but was")
}
_, ok := req.Context.Deadline()
if !ok {
t.Fatalf("Context should have had a deadline") // Difficult to test the actual deadline value
}
if req.Method != "POST" {
t.Fatalf("Request method should have been POST but was %s", req.Method)
}
if req.Path != "/query/service" {
t.Fatalf("Request path should have been /query/service but was %s", req.Path)
}
}
func testAssertQueryResult(t *testing.T, expectedResult *n1qlResponse, actualResult *QueryResults, expectData bool) {
if expectData {
var breweryDocs []testBreweryDocument
var resDoc testBreweryDocument
for actualResult.Next(&resDoc) {
breweryDocs = append(breweryDocs, resDoc)
}
var expectedDocs []testBreweryDocument
for _, doc := range expectedResult.Results {
var expectedDoc testBreweryDocument
err := json.Unmarshal(doc, &expectedDoc)
if err != nil {
t.Fatalf("Unmarshalling expected result document failed %v", err)
}
expectedDocs = append(expectedDocs, expectedDoc)
}
if len(breweryDocs) != len(expectedResult.Results) {
t.Fatalf("Expected results length to be %d but was %d", len(expectedResult.Results), len(breweryDocs))
}
for i, doc := range expectedDocs {
if breweryDocs[i] != doc {
t.Fatalf("Docs did not match, expected %v but was %v", doc, breweryDocs[i])
}
}
}
if actualResult.ClientContextID() != expectedResult.ClientContextID {
t.Fatalf("Expected ClientContextID to be %s but was %s", expectedResult.ClientContextID, actualResult.ClientContextID())
}
if actualResult.RequestID() != expectedResult.RequestID {
t.Fatalf("Expected RequestID to be %s but was %s", expectedResult.RequestID, actualResult.RequestID())
}
if actualResult.SourceEndpoint() != "localhost:8093" {
t.Fatalf("Expected endpoint to be %s but was %s", "localhost:8093", actualResult.SourceEndpoint())
}
metrics := actualResult.Metrics()
elapsedTime, err := time.ParseDuration(expectedResult.Metrics.ElapsedTime)
if err != nil {
t.Fatalf("Failed to parse ElapsedTime %v", err)
}
if metrics.ElapsedTime != elapsedTime {
t.Fatalf("Expected metrics ElapsedTime to be %s but was %s", metrics.ElapsedTime, elapsedTime)
}
executionTime, err := time.ParseDuration(expectedResult.Metrics.ExecutionTime)
if err != nil {
t.Fatalf("Failed to parse ElapsedTime %v", err)
}
if metrics.ExecutionTime != executionTime {
t.Fatalf("Expected metrics ElapsedTime to be %s but was %s", metrics.ExecutionTime, executionTime)
}
if metrics.MutationCount != expectedResult.Metrics.MutationCount {
t.Fatalf("Expected metrics MutationCount to be %d but was %d", metrics.MutationCount, expectedResult.Metrics.MutationCount)
}
if metrics.ErrorCount != expectedResult.Metrics.ErrorCount {
t.Fatalf("Expected metrics ErrorCount to be %d but was %d", metrics.ErrorCount, expectedResult.Metrics.ErrorCount)
}
if metrics.ResultCount != expectedResult.Metrics.ResultCount {
t.Fatalf("Expected metrics ResultCount to be %d but was %d", metrics.ResultCount, expectedResult.Metrics.ResultCount)
}
if metrics.ResultSize != expectedResult.Metrics.ResultSize {
t.Fatalf("Expected metrics ResultSize to be %d but was %d", metrics.ResultSize, expectedResult.Metrics.ResultSize)
}
if metrics.SortCount != expectedResult.Metrics.SortCount {
t.Fatalf("Expected metrics SortCount to be %d but was %d", metrics.SortCount, expectedResult.Metrics.SortCount)
}
if metrics.WarningCount != expectedResult.Metrics.WarningCount {
t.Fatalf("Expected metrics WarningCount to be %d but was %d", metrics.WarningCount, expectedResult.Metrics.WarningCount)
}
}
func testGetClusterForHTTP(provider *mockHTTPProvider, n1qlTimeout, analyticsTimeout, searchTimeout time.Duration) *Cluster {
clients := make(map[string]client)
cli := &mockClient{
bucketName: "mock",
collectionId: 0,
scopeId: 0,
useMutationTokens: false,
mockHTTPProvider: provider,
}
clients["mock-false"] = cli
c := &Cluster{
connections: clients,
}
c.sb.QueryTimeout = n1qlTimeout
c.sb.AnalyticsTimeout = analyticsTimeout
c.sb.SearchTimeout = searchTimeout
return c
}