This repository has been archived by the owner on Feb 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sync_worker_test.go
936 lines (795 loc) · 33.2 KB
/
sync_worker_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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
package bqstreamer
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"regexp"
"runtime"
"strconv"
"sync"
"testing"
"time"
bigquery "google.golang.org/api/bigquery/v2"
"google.golang.org/api/googleapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSyncWorkerNew tests creating a new Worker.
func TestSyncWorkerNewSyncWorker(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
var err error
// Test bad arguments.
//
// NOTE we're testing a bad argument for the second option, to make sure
// the options are evaluated one after the other, and not just the first
// one or anything of the sort.
_, err = NewSyncWorker(&http.Client{}, SetSyncMaxRetries(3), SetSyncRetryInterval(0))
assert.EqualError(err, "retry interval value must be a positive time.Duration")
// Test valid arguments.
w, err := NewSyncWorker(&http.Client{}, SetSyncMaxRetries(10), SetSyncRetryInterval(1*time.Second))
require.NoError(err)
assert.NotNil(w.rows)
assert.Empty(w.rows, 0)
assert.Equal(rowSize, cap(w.rows))
assert.Equal(10, w.maxRetries)
assert.Equal(1*time.Second, w.retryInterval)
}
// TestSyncWorkerEnqueue tests queueing a row.
func TestSyncWorkerEnqueueAndRowLen(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
w, err := NewSyncWorker(&http.Client{})
require.NoError(err)
w.Enqueue(NewRowWithID("p", "d", "t", "r1", map[string]bigquery.JsonValue{"k1": "v1"}))
w.Enqueue(NewRowWithID("p", "d", "t", "r2", map[string]bigquery.JsonValue{"k2": "v2"}))
w.Enqueue(NewRowWithID("p", "d", "t", "r3", map[string]bigquery.JsonValue{"k3": "v3"}))
require.Len(w.rows, 3)
assert.Equal(3, w.RowLen())
assert.Equal(
w.rows,
[]Row{
NewRowWithID("p", "d", "t", "r1", map[string]bigquery.JsonValue{"k1": "v1"}),
NewRowWithID("p", "d", "t", "r2", map[string]bigquery.JsonValue{"k2": "v2"}),
NewRowWithID("p", "d", "t", "r3", map[string]bigquery.JsonValue{"k3": "v3"}),
})
}
// TestSyncWorkerInsertAll queues 20 rows to 4 tables, 5 to each row,
// and tests if rows were inserted to a mock project-dataset-table-rows type.
func TestSyncWorkerInsertAll(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
// Cache all rows from all tables to a local variable.
// Will be used for testing which rows were actually "inserted".
inserted := make(chan struct{})
var ps projects
client := http.Client{
Transport: newTransport(func(req *http.Request) (*http.Response, error) {
// Get project, dataset, table IDs of current request.
pID, dID, tID := getInsertMetadata(req.URL.Path)
var tableReq bigquery.TableDataInsertAllRequest
b, _ := ioutil.ReadAll(req.Body)
require.NoError(json.Unmarshal(b, &tableReq))
// Add all table rows to a local projects-datasets-tables map.
// This mocks the rows that were inserted to BigQuery, and we will test against them.
for _, tr := range tableReq.Rows {
assert.NotNil(tr)
// Mock "insert row" to table: Create project, dataset and table
// if uninitalized.
initTableIfNotExists(ps, pID, dID, tID)
ps[pID][dID][tID] = append(ps[pID][dID][tID], &bigquery.TableDataInsertAllRequestRows{tr.InsertId, tr.Json, []string{}})
}
// Notify that this table was mock "inserted".
inserted <- struct{}{}
res := http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 200,
// Empty JSON body, meaning "no errors".
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`))}
return &res, nil
})}
// We intend to insert 20 rows in this test, so set maxRows = 20 and delay
// to be long enough so flush will occur due to rows queue filling up.
//
// Test both "simple" insertTable, and insert with retry functions.
w, err := NewSyncWorker(&client)
require.NoError(err)
for _, insertFunc := range []func() *InsertErrors{
w.Insert,
w.InsertWithRetry,
} {
ps = make(projects)
funcName := runtime.FuncForPC(reflect.ValueOf(insertFunc).Pointer()).Name()
// Distribute 5 rows to 4 tables in 2 datasets in 2 projects (total 20 rows).
for i := 0; i < 5; i++ {
k := fmt.Sprintf("k%d", i)
v := fmt.Sprintf("v%d", i)
w.Enqueue(NewRowWithID("p1", "d1", "t1", "p1d1t1"+k, map[string]bigquery.JsonValue{k: v}))
w.Enqueue(NewRowWithID("p1", "d1", "t2", "p1d1t2"+k, map[string]bigquery.JsonValue{k: v}))
w.Enqueue(NewRowWithID("p1", "d2", "t1", "p1d2t1"+k, map[string]bigquery.JsonValue{k: v}))
w.Enqueue(NewRowWithID("p2", "d1", "t1", "p2d1t1"+k, map[string]bigquery.JsonValue{k: v}))
}
// Start Worker and wait for 4 flushes to happen, one for each table.
// Fail if flushes take too long.
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
select {
case <-inserted:
case <-time.After(1 * time.Second):
require.Fail("insert wasn't called fast enough", strconv.Itoa(i), funcName)
}
}(i)
}
insertErrs := insertFunc()
done := make(chan struct{})
go func() { wg.Wait(); close(done) }()
select {
case <-done:
case <-time.After(1 * time.Second):
require.Fail("insert didn't occur as expected", funcName)
}
assert.Len(inserted, 0, funcName) // Test no more inserts happened.
assert.Len(w.rows, 0, funcName) // Test rows were reset after insert
// Test rows slice wasn't re-allocated with a larger size.
// This make sure we're keeping the same row slice size throught the
// Worker's life.
assert.Equal(rowSize, cap(w.rows), funcName)
// Test no insert
tables := insertErrs.All()
require.Len(tables, 4, funcName)
for _, table := range tables {
for _, row := range table.Attempts() {
assert.NoError(row.Error(), funcName)
assert.Empty(row.All(), funcName)
}
}
assert.Equal(
projects{
"p1": project{
"d1": dataset{
"t1": table{
&bigquery.TableDataInsertAllRequestRows{"p1d1t1k0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t1k1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t1k2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t1k3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t1k4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
},
"t2": table{
&bigquery.TableDataInsertAllRequestRows{"p1d1t2k0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t2k1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t2k2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t2k3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d1t2k4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}},
"d2": dataset{
"t1": table{
&bigquery.TableDataInsertAllRequestRows{"p1d2t1k0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d2t1k1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d2t1k2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d2t1k3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p1d2t1k4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}}},
"p2": project{
"d1": dataset{
"t1": table{
&bigquery.TableDataInsertAllRequestRows{"p2d1t1k0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p2d1t1k1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p2d1t1k2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p2d1t1k3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"p2d1t1k4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}}}},
ps, funcName)
}
}
// TestSyncSkipInvalidRows tests wether enabling SkipInvalidRows option in the
// worker causes insert requests to include the SkipInvalidRows flag = true.
func TestSyncSkipInvalidRows(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
// Mock response to return mocked errors, and notify table was mock "inserted".
inserted := make(chan struct{})
ps := make(projects)
client := http.Client{
Transport: newTransport(func(req *http.Request) (*http.Response, error) {
pID, dID, tID := getInsertMetadata(req.URL.Path)
var tableReq bigquery.TableDataInsertAllRequest
b, _ := ioutil.ReadAll(req.Body)
require.NoError(json.Unmarshal(b, &tableReq))
// Test the SkipInvalidRows flag is enabled.
assert.True(tableReq.SkipInvalidRows)
// Add all table rows to a local projects-datasets-tables map.
// This mocks the rows that were inserted to BigQuery, and we will test against them.
for _, tr := range tableReq.Rows {
assert.NotNil(tr)
// Mock "insert row" to table: Create project, dataset and table
// if uninitalized.
initTableIfNotExists(ps, pID, dID, tID)
ps[pID][dID][tID] = append(ps[pID][dID][tID], &bigquery.TableDataInsertAllRequestRows{tr.InsertId, tr.Json, []string{}})
}
res := http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 200,
// Empty JSON body, meaning "no errors".
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`))}
// Notify that this table was mock "inserted".
inserted <- struct{}{}
return &res, nil
})}
// Make retry sleep delay as small as possible and != 0.
w, err := NewSyncWorker(&client, SetSyncSkipInvalidRows(true))
require.NoError(err)
// Queue 5 rows to the same table.
w.Enqueue(NewRowWithID("p", "d", "t", "id0", map[string]bigquery.JsonValue{"k0": "v0"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id1", map[string]bigquery.JsonValue{"k1": "v1"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id2", map[string]bigquery.JsonValue{"k2": "v2"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id3", map[string]bigquery.JsonValue{"k3": "v3"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id4", map[string]bigquery.JsonValue{"k4": "v4"}))
// Test HTTP 503 server error response triggered a retry.
//
// First loop is for testing initial insert,
// second is for testing retry insert happened.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-inserted:
case <-time.After(1 * time.Second):
assert.Fail("insert didn't happen fast enough")
}
}()
insertErrs := w.InsertWithRetry()
done := make(chan struct{})
go func() { wg.Wait(); close(done) }()
select {
case <-done:
case <-time.After(1 * time.Second):
require.Fail("insert didn't occur as expected")
}
assert.Len(inserted, 0) // Test no more inserts happened.
assert.Len(w.rows, 0) // Test rows were reset after insert.
// Test rows slice wasn't re-allocated with a larger size.
// This make sure we're keeping the same row slice size throught the
// Worker's life.
assert.Equal(rowSize, cap(w.rows))
// Test insert
tables := insertErrs.All()
require.Len(tables, 1)
require.Len(tables[0].Attempts(), 1)
assert.NoError(tables[0].Attempts()[0].Error())
assert.Empty(tables[0].Attempts()[0].All())
assert.Equal(
projects{
"p": project{
"d": dataset{
"t": table{
&bigquery.TableDataInsertAllRequestRows{"id0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}}}},
ps)
}
// TestSyncIgnoreUnknownValues tests wether enabling IgnoreUnknownValues option in the
// worker causes insert requests to include the IgnoreUnknownValues flag = true.
func TestSyncIgnoreUnknownValues(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
// Mock response to return mocked errors, and notify table was mock "inserted".
inserted := make(chan struct{})
ps := make(projects)
client := http.Client{
Transport: newTransport(func(req *http.Request) (*http.Response, error) {
pID, dID, tID := getInsertMetadata(req.URL.Path)
var tableReq bigquery.TableDataInsertAllRequest
b, _ := ioutil.ReadAll(req.Body)
require.NoError(json.Unmarshal(b, &tableReq))
// Test the SkipInvalidRows flag is enabled.
assert.True(tableReq.IgnoreUnknownValues)
// Add all table rows to a local projects-datasets-tables map.
// This mocks the rows that were inserted to BigQuery, and we will test against them.
for _, tr := range tableReq.Rows {
assert.NotNil(tr)
// Mock "insert row" to table: Create project, dataset and table
// if uninitalized.
initTableIfNotExists(ps, pID, dID, tID)
ps[pID][dID][tID] = append(ps[pID][dID][tID], &bigquery.TableDataInsertAllRequestRows{tr.InsertId, tr.Json, []string{}})
}
res := http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 200,
// Empty JSON body, meaning "no errors".
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`))}
// Notify that this table was mock "inserted".
inserted <- struct{}{}
return &res, nil
})}
// Make retry sleep delay as small as possible and != 0.
w, err := NewSyncWorker(&client, SetSyncIgnoreUnknownValues(true))
require.NoError(err)
// Queue 5 rows to the same table.
w.Enqueue(NewRowWithID("p", "d", "t", "id0", map[string]bigquery.JsonValue{"k0": "v0"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id1", map[string]bigquery.JsonValue{"k1": "v1"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id2", map[string]bigquery.JsonValue{"k2": "v2"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id3", map[string]bigquery.JsonValue{"k3": "v3"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id4", map[string]bigquery.JsonValue{"k4": "v4"}))
// Test HTTP 503 server error response triggered a retry.
//
// First loop is for testing initial insert,
// second is for testing retry insert happened.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-inserted:
case <-time.After(1 * time.Second):
assert.Fail("insert didn't happen fast enough")
}
}()
insertErrs := w.InsertWithRetry()
done := make(chan struct{})
go func() { wg.Wait(); close(done) }()
select {
case <-done:
case <-time.After(1 * time.Second):
require.Fail("insert didn't occur as expected")
}
assert.Len(inserted, 0) // Test no more inserts happened.
assert.Len(w.rows, 0) // Test rows were reset after insert.
// Test rows slice wasn't re-allocated with a larger size.
// This make sure we're keeping the same row slice size throught the
// Worker's life.
assert.Equal(rowSize, cap(w.rows))
// Test insert
tables := insertErrs.All()
require.Len(tables, 1)
require.Len(tables[0].Attempts(), 1)
assert.NoError(tables[0].Attempts()[0].Error())
assert.Empty(tables[0].Attempts()[0].All())
assert.Equal(
projects{
"p": project{
"d": dataset{
"t": table{
&bigquery.TableDataInsertAllRequestRows{"id0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}}}},
ps)
}
// TestSyncWorkerShouldRetryInsert tests if the shouldRetryInsert function correctly
// returns true for Google API 500, 503
func TestSyncWorkerShouldRetryInsert(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
w, err := NewSyncWorker(&http.Client{}, SetSyncMaxRetries(10), SetSyncRetryInterval(100*time.Millisecond))
require.NoError(err)
// Test whether a GoogleAPI HTTP 501, 503 errors returns true.
// i.e. the function decides the request related to this error should be retried.
for _, tt := range []struct {
code int
retry bool
}{
{500, true},
{503, true},
{501, false},
{400, false},
{404, false},
} {
start := time.Now()
retry := w.shouldRetryInsert(
&googleapi.Error{
Code: tt.code,
Message: "m",
Body: "b",
Errors: []googleapi.ErrorItem{
googleapi.ErrorItem{Reason: "r1", Message: "m1"},
googleapi.ErrorItem{Reason: "r2", Message: "m2"},
}})
assert.Equal(tt.retry, retry, tt.code)
// Test backoff time was according to SleepBeforeRetry setting + 10ms extra.
if tt.retry {
assert.WithinDuration(time.Now(), start, 110*time.Millisecond)
}
}
// Test a non-GoogleAPI error, but a generic error instead.
assert.False(w.shouldRetryInsert(errors.New("Non-GoogleAPI error")))
}
// TestSyncWorkerInsertAllWithServerErrorResponse tests if an insert failed with a server
// error (500, 503) triggers a retry insert.
func TestSyncWorkerInsertAllWithServerErrorResponse(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
// Mock response to return mocked errors, and notify table was mock "inserted".
// Also count the times the insert function was called to make sure it's retried exactly once.
inserted := make(chan struct{})
calledNum := 0
ps := make(projects)
client := http.Client{
Transport: newTransport(func(req *http.Request) (*http.Response, error) {
pID, dID, tID := getInsertMetadata(req.URL.Path)
var tableReq bigquery.TableDataInsertAllRequest
b, _ := ioutil.ReadAll(req.Body)
require.NoError(json.Unmarshal(b, &tableReq))
// Return 503 server error on first call, and test if it triggered
// a retry insert with exactly same data on the second call.
var err error
// Default response.
var res http.Response
switch calledNum {
case 0:
err = &googleapi.Error{
Code: 503,
Message: "m", Body: "b",
Errors: []googleapi.ErrorItem{
googleapi.ErrorItem{Reason: "r1", Message: "m1"},
googleapi.ErrorItem{Reason: "r2", Message: "m2"},
}}
// Response should be HTTP 503.
if b, err := json.Marshal(err); assert.NoError(err) {
res = http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 503,
Body: ioutil.NopCloser(bytes.NewBuffer(b))}
}
case 1:
// This insert should keep the table as it is,
// and trigger retry after server error.
// Add all table rows to a local projects-datasets-tables map.
// This mocks the rows that were inserted to BigQuery, and we will test against them.
for _, tr := range tableReq.Rows {
assert.NotNil(tr)
// Mock "insert row" to table: Create project, dataset and table
// if uninitalized.
initTableIfNotExists(ps, pID, dID, tID)
ps[pID][dID][tID] = append(ps[pID][dID][tID], &bigquery.TableDataInsertAllRequestRows{tr.InsertId, tr.Json, []string{}})
}
res = http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 200,
// Empty JSON body, meaning "no errors".
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`))}
default:
require.Fail("Insert was called more than 2 times")
}
// Notify that this table was mock "inserted".
inserted <- struct{}{}
calledNum++
return &res, nil
})}
// Make retry sleep delay as small as possible and != 0.
w, err := NewSyncWorker(&client, SetSyncMaxRetries(10), SetSyncRetryInterval(1*time.Nanosecond))
require.NoError(err)
// Queue 5 rows to the same table.
w.Enqueue(NewRowWithID("p", "d", "t", "id0", map[string]bigquery.JsonValue{"k0": "v0"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id1", map[string]bigquery.JsonValue{"k1": "v1"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id2", map[string]bigquery.JsonValue{"k2": "v2"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id3", map[string]bigquery.JsonValue{"k3": "v3"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id4", map[string]bigquery.JsonValue{"k4": "v4"}))
// Test HTTP 503 server error response triggered a retry.
//
// First loop is for testing initial insert,
// second is for testing retry insert happened.
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
select {
case <-inserted:
case <-time.After(1 * time.Second):
assert.Fail("insert didn't happen fast enough", strconv.Itoa(i))
}
}(i)
}
insertErrs := w.InsertWithRetry()
done := make(chan struct{})
go func() { wg.Wait(); close(done) }()
select {
case <-done:
case <-time.After(1 * time.Second):
require.Fail("insert didn't occur as expected")
}
assert.Len(inserted, 0) // Test no more inserts happened.
assert.Len(w.rows, 0) // Test rows were reset after insert.
// Test rows slice wasn't re-allocated with a larger size.
// This make sure we're keeping the same row slice size throught the
// Worker's life.
assert.Equal(rowSize, cap(w.rows))
// Test insert
tables := insertErrs.All()
require.Len(tables, 1)
require.Len(tables[0].Attempts(), 2)
assert.EqualError(
tables[0].Attempts()[0].Error(),
`googleapi: got HTTP response code 503 with body: {"code":503,"message":"m","Body":"b","Header":null,"Errors":[{"reason":"r1","message":"m1"},{"reason":"r2","message":"m2"}]}`)
assert.NoError(tables[0].Attempts()[1].Error())
assert.Empty(tables[0].Attempts()[0].All())
assert.Empty(tables[0].Attempts()[1].All())
assert.Equal(
projects{
"p": project{
"d": dataset{
"t": table{
&bigquery.TableDataInsertAllRequestRows{"id0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}}}},
ps)
}
// TestSyncWorkerInsertAllWithNonServerErrorResponse tests if an insert failed with an error
// which is NOT a server error (503) does NOT trigger a retry insert.
func TestSyncWorkerInsertAllWithNonServerErrorResponse(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
// Mock response to return mocked errors, and notify table was mock "inserted".
// Also count the times the insert function was called to make sure it's retried exactly once.
inserted := make(chan struct{})
ps := make(projects)
calledNum := 0
client := http.Client{
Transport: newTransport(func(req *http.Request) (*http.Response, error) {
pID, dID, tID := getInsertMetadata(req.URL.Path)
var tableReq bigquery.TableDataInsertAllRequest
b, _ := ioutil.ReadAll(req.Body)
require.NoError(json.Unmarshal(b, &tableReq))
var err error
// Default response.
res := http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 200,
// Empty JSON body, meaning "no errors".
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`))}
// Return a 501 (not 500, 503) server error on first call,
// and test if it did NOT trigger a retry insert.
switch calledNum {
case 0:
err = &googleapi.Error{
Code: 501,
Message: "m", Body: "b",
Errors: []googleapi.ErrorItem{
googleapi.ErrorItem{Reason: "r1", Message: "m1"},
googleapi.ErrorItem{Reason: "r2", Message: "m2"},
}}
if b, err := json.Marshal(err); assert.NoError(err) {
// Response should be HTTP 503.
res = http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 501,
Body: ioutil.NopCloser(bytes.NewBuffer(b))}
}
for _, tr := range tableReq.Rows {
assert.NotNil(tr)
// Mock "insert row" to table: Create project, dataset and table
// if uninitalized.
initTableIfNotExists(ps, pID, dID, tID)
ps[pID][dID][tID] = append(ps[pID][dID][tID], &bigquery.TableDataInsertAllRequestRows{tr.InsertId, tr.Json, []string{}})
}
default:
require.Fail("Insert was called more than once")
}
// Notify that this table was mock "flushed".
inserted <- struct{}{}
calledNum++
return &res, nil
})}
// Make retry sleep delay as small as possible and != 0.
w, err := NewSyncWorker(&client, SetSyncMaxRetries(10), SetSyncRetryInterval(1*time.Nanosecond))
require.NoError(err)
// Queue 5 rows to the same table.
w.Enqueue(NewRowWithID("p", "d", "t", "id0", map[string]bigquery.JsonValue{"k0": "v0"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id1", map[string]bigquery.JsonValue{"k1": "v1"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id2", map[string]bigquery.JsonValue{"k2": "v2"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id3", map[string]bigquery.JsonValue{"k3": "v3"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id4", map[string]bigquery.JsonValue{"k4": "v4"}))
// Test only a single insert occured.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-inserted:
case <-time.After(1 * time.Second):
assert.Fail("initial insert didn't occur as expected")
}
}()
insertErrs := w.InsertWithRetry()
done := make(chan struct{})
go func() { wg.Wait(); close(done) }()
select {
case <-done:
case <-time.After(1 * time.Second):
require.Fail("insert didn't occur as expected")
}
assert.Len(inserted, 0) // Test no more inserts happened.
assert.Len(w.rows, 0) // Test rows were reset after insert.
// Test rows slice wasn't re-allocated with a larger size.
// This make sure we're keeping the same row slice size throught the
// Worker's life.
assert.Equal(rowSize, cap(w.rows))
// A 501 error should cause an insertError with a 501 error
// to be returned, along with the result (which is empty for this test).
tables := insertErrs.All()
require.Len(tables, 1)
require.Len(tables[0].Attempts(), 1)
assert.EqualError(
tables[0].Attempts()[0].Error(),
`googleapi: got HTTP response code 501 with body: {"code":501,"message":"m","Body":"b","Header":null,"Errors":[{"reason":"r1","message":"m1"},{"reason":"r2","message":"m2"}]}`)
assert.Empty(tables[0].Attempts()[0].All())
assert.Equal(
projects{
"p": project{
"d": dataset{
"t": table{
&bigquery.TableDataInsertAllRequestRows{"id0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}}}},
ps)
}
// TestSyncWorkerMaxRetryInsert tests if a repeatedly failing insert attempt
// (failing with non-rejected rows errors) is eventually dropped and ignored,
// and Worker is moving on to the next table insert.
func TestSyncWorkerMaxRetryInsert(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
// Mock response to return mocked errors, and notify table was mock "flushed".
// Also count the times the insert function was called to make sure it's retried exactly 3 times.
inserted := make(chan struct{})
calledNum := 0
client := http.Client{
Transport: newTransport(func(req *http.Request) (*http.Response, error) {
pID, dID, tID := getInsertMetadata(req.URL.Path)
var tableReq bigquery.TableDataInsertAllRequest
b, _ := ioutil.ReadAll(req.Body)
require.NoError(json.Unmarshal(b, &tableReq))
var err error
// Default response.
res := http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 200,
// Empty JSON body, meaning "no errors".
Body: ioutil.NopCloser(bytes.NewBufferString(`{}`))}
// Return Google API HTTP 503 error on every call, which should be retried and logged.
// Thus, we should expect 3 logged
if calledNum < 3 {
err = &googleapi.Error{
Code: 503,
Message: "m",
Body: "b",
Errors: []googleapi.ErrorItem{
googleapi.ErrorItem{Reason: "r1", Message: "m1"},
googleapi.ErrorItem{Reason: "r2", Message: "m2"},
}}
if b, err := json.Marshal(err); assert.NoError(err) {
// Response should be HTTP 503.
res = http.Response{
Header: make(http.Header),
Request: req,
StatusCode: 503,
Body: ioutil.NopCloser(bytes.NewBuffer(b))}
}
// Test request payload.
ps := projects{}
for _, tr := range tableReq.Rows {
assert.NotNil(tr)
// Mock "insert row" to table: Create project, dataset and table
// if uninitalized.
initTableIfNotExists(ps, pID, dID, tID)
ps[pID][dID][tID] = append(ps[pID][dID][tID], &bigquery.TableDataInsertAllRequestRows{tr.InsertId, tr.Json, []string{}})
}
assert.Equal(
projects{
"p": project{
"d": dataset{
"t": table{
&bigquery.TableDataInsertAllRequestRows{"id0", map[string]bigquery.JsonValue{"k0": "v0"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id1", map[string]bigquery.JsonValue{"k1": "v1"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id2", map[string]bigquery.JsonValue{"k2": "v2"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id3", map[string]bigquery.JsonValue{"k3": "v3"}, []string{}},
&bigquery.TableDataInsertAllRequestRows{"id4", map[string]bigquery.JsonValue{"k4": "v4"}, []string{}},
}}}},
ps)
} else {
assert.Fail("Insert was called more than 3 times")
}
// Notify that this table was mock "inserted".
inserted <- struct{}{}
calledNum++
return &res, nil
})}
// Set a row threshold to 5 so it will flush immediately on calling Start().
// Also make retry sleep delay as small as possible and != 0.
w, err := NewSyncWorker(&client, SetSyncMaxRetries(3), SetSyncRetryInterval(1*time.Nanosecond))
require.NoError(err)
// Queue 5 rows to the same table.
w.Enqueue(NewRowWithID("p", "d", "t", "id0", map[string]bigquery.JsonValue{"k0": "v0"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id1", map[string]bigquery.JsonValue{"k1": "v1"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id2", map[string]bigquery.JsonValue{"k2": "v2"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id3", map[string]bigquery.JsonValue{"k3": "v3"}))
w.Enqueue(NewRowWithID("p", "d", "t", "id4", map[string]bigquery.JsonValue{"k4": "v4"}))
// Test only a single insert occured.
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
select {
case <-inserted:
case <-time.After(1 * time.Second):
assert.Fail("insert didn't occur as expected", strconv.Itoa(i))
}
}(i)
}
insertErrs := w.InsertWithRetry()
done := make(chan struct{})
go func() { wg.Wait(); close(done) }()
select {
case <-done:
case <-time.After(1 * time.Second):
require.Fail("insert didn't occur as expected")
}
assert.Len(inserted, 0) // Test no more inserts happened.
assert.Len(w.rows, 0) // Test rows were reset after insert.
tables := insertErrs.All()
require.Len(tables, 1)
// 3 attempts and the 4th is the one that aborts the insert operation.
require.Len(tables[0].Attempts(), 4)
assert.EqualError(tables[0].Attempts()[0].Error(), `googleapi: got HTTP response code 503 with body: {"code":503,"message":"m","Body":"b","Header":null,"Errors":[{"reason":"r1","message":"m1"},{"reason":"r2","message":"m2"}]}`)
assert.EqualError(tables[0].Attempts()[1].Error(), `googleapi: got HTTP response code 503 with body: {"code":503,"message":"m","Body":"b","Header":null,"Errors":[{"reason":"r1","message":"m1"},{"reason":"r2","message":"m2"}]}`)
assert.EqualError(tables[0].Attempts()[2].Error(), `googleapi: got HTTP response code 503 with body: {"code":503,"message":"m","Body":"b","Header":null,"Errors":[{"reason":"r1","message":"m1"},{"reason":"r2","message":"m2"}]}`)
assert.EqualError(tables[0].Attempts()[3].Error(), "Insert table p.d.t retried 4 times, dropping insert and moving on")
}
// getInsertMetadata is a helper function that fetches the project, dataset,
// and table IDs from a url string.
func getInsertMetadata(url string) (projectID, datasetID, tableID string) {
re := regexp.MustCompile(`bigquery/v2/projects/(?P<projectID>.+?)/datasets/(?P<datasetID>.+?)/tables/(?P<tableId>.+?)/insertAll`)
res := re.FindAllStringSubmatch(url, -1)
p := res[0][1]
d := res[0][2]
t := res[0][3]
return p, d, t
}
// transport is a mock http.Transport, and implements http.RoundTripper
// interface.
//
// It is used for mocking BigQuery responses via bigquery.Service.
type transport struct {
roundTrip func(*http.Request) (*http.Response, error)
}
func newTransport(roundTrip func(*http.Request) (*http.Response, error)) *transport {
return &transport{roundTrip}
}
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { return t.roundTrip(req) }