Skip to content

Commit 8a4ec01

Browse files
authored
Merge pull request scylladb#510 from sylwiaszunejko/query-error-unit
Add unit test for QueryError
2 parents 65f7e82 + f3b441f commit 8a4ec01

File tree

2 files changed

+189
-15
lines changed

2 files changed

+189
-15
lines changed

policies_test.go

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -400,20 +400,49 @@ func TestSimpleRetryPolicy(t *testing.T) {
400400
// this should allow a total of 3 tries.
401401
rt := &SimpleRetryPolicy{NumRetries: 2}
402402

403+
regular_error := errors.New("regular error")
404+
405+
qe1 := &QueryError{
406+
err: errors.New("connection error"),
407+
potentiallyExecuted: false,
408+
isIdempotent: false,
409+
}
410+
411+
qe2 := &QueryError{
412+
err: errors.New("timeout error"),
413+
potentiallyExecuted: true,
414+
isIdempotent: true,
415+
}
416+
417+
qe3 := &QueryError{
418+
err: errors.New("write timeout"),
419+
potentiallyExecuted: true,
420+
isIdempotent: false,
421+
}
422+
403423
cases := []struct {
404-
attempts int
405-
allow bool
424+
attempts int
425+
allow bool
426+
err error
427+
retryType RetryType
428+
LWTRetryType RetryType
406429
}{
407-
{0, true},
408-
{1, true},
409-
{2, true},
410-
{3, false},
411-
{4, false},
412-
{5, false},
430+
{0, true, qe1, RetryNextHost, Retry},
431+
{1, true, qe2, RetryNextHost, Retry},
432+
{2, true, qe3, Rethrow, Rethrow},
433+
{3, false, regular_error, RetryNextHost, Retry},
434+
{4, false, regular_error, RetryNextHost, Retry},
435+
{5, false, regular_error, RetryNextHost, Retry},
413436
}
414437

415438
for _, c := range cases {
416439
q.metrics = preFilledQueryMetrics(map[string]*hostMetrics{"127.0.0.1": {Attempts: c.attempts}})
440+
if c.retryType != rt.GetRetryType(c.err) {
441+
t.Fatalf("retry type for %v should be %v", c.err, c.retryType)
442+
}
443+
if c.LWTRetryType != rt.GetRetryTypeLWT(c.err) {
444+
t.Fatalf("LWT retry type for %v should be %v", c.err, c.LWTRetryType)
445+
}
417446
if c.allow && !rt.Attempt(q) {
418447
t.Fatalf("should allow retry after %d attempts", c.attempts)
419448
}
@@ -439,17 +468,45 @@ func TestExponentialBackoffPolicy(t *testing.T) {
439468
// test with defaults
440469
sut := &ExponentialBackoffRetryPolicy{NumRetries: 2}
441470

471+
regular_error := errors.New("regular error")
472+
473+
qe1 := &QueryError{
474+
err: errors.New("connection error"),
475+
potentiallyExecuted: false,
476+
isIdempotent: false,
477+
}
478+
479+
qe2 := &QueryError{
480+
err: errors.New("timeout error"),
481+
potentiallyExecuted: true,
482+
isIdempotent: true,
483+
}
484+
485+
qe3 := &QueryError{
486+
err: errors.New("write timeout"),
487+
potentiallyExecuted: true,
488+
isIdempotent: false,
489+
}
490+
442491
cases := []struct {
443-
attempts int
444-
delay time.Duration
492+
attempts int
493+
delay time.Duration
494+
err error
495+
retryType RetryType
496+
LWTRetryType RetryType
445497
}{
446-
447-
{1, 100 * time.Millisecond},
448-
{2, (2) * 100 * time.Millisecond},
449-
{3, (2 * 2) * 100 * time.Millisecond},
450-
{4, (2 * 2 * 2) * 100 * time.Millisecond},
498+
{1, 100 * time.Millisecond, qe1, RetryNextHost, Retry},
499+
{2, (2) * 100 * time.Millisecond, qe2, RetryNextHost, Retry},
500+
{3, (2 * 2) * 100 * time.Millisecond, qe3, Rethrow, Rethrow},
501+
{4, (2 * 2 * 2) * 100 * time.Millisecond, regular_error, RetryNextHost, Retry},
451502
}
452503
for _, c := range cases {
504+
if c.retryType != sut.GetRetryType(c.err) {
505+
t.Fatalf("retry type for %v should be %v", c.err, c.retryType)
506+
}
507+
if c.LWTRetryType != sut.GetRetryTypeLWT(c.err) {
508+
t.Fatalf("LWT retry type for %v should be %v", c.err, c.LWTRetryType)
509+
}
453510
// test 100 times for each case
454511
for i := 0; i < 100; i++ {
455512
d := sut.napTime(c.attempts)

query_error_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//go:build unit
2+
// +build unit
3+
4+
package gocql
5+
6+
import (
7+
"errors"
8+
"testing"
9+
)
10+
11+
func TestQueryError_PotentiallyExecuted(t *testing.T) {
12+
t.Parallel()
13+
14+
tests := []struct {
15+
name string
16+
potentiallyExecuted bool
17+
expected bool
18+
}{
19+
{
20+
name: "potentially executed true",
21+
potentiallyExecuted: true,
22+
expected: true,
23+
},
24+
{
25+
name: "potentially executed false",
26+
potentiallyExecuted: false,
27+
expected: false,
28+
},
29+
}
30+
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
qErr := &QueryError{
34+
err: errors.New("test error"),
35+
potentiallyExecuted: tt.potentiallyExecuted,
36+
}
37+
38+
got := qErr.PotentiallyExecuted()
39+
if got != tt.expected {
40+
t.Fatalf("QueryError.PotentiallyExecuted() = %v, expected %v", got, tt.expected)
41+
}
42+
})
43+
}
44+
}
45+
46+
func TestQueryError_IsIdempotent(t *testing.T) {
47+
t.Parallel()
48+
49+
tests := []struct {
50+
name string
51+
isIdempotent bool
52+
expected bool
53+
}{
54+
{
55+
name: "idempotent true",
56+
isIdempotent: true,
57+
expected: true,
58+
},
59+
{
60+
name: "idempotent false",
61+
isIdempotent: false,
62+
expected: false,
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
qErr := &QueryError{
69+
err: errors.New("test error"),
70+
isIdempotent: tt.isIdempotent,
71+
}
72+
73+
got := qErr.IsIdempotent()
74+
if got != tt.expected {
75+
t.Errorf("QueryError.IsIdempotent() = %v, expected %v", got, tt.expected)
76+
}
77+
})
78+
}
79+
}
80+
81+
func TestQueryError_Error(t *testing.T) {
82+
t.Parallel()
83+
84+
tests := []struct {
85+
name string
86+
err error
87+
potentiallyExecuted bool
88+
expected string
89+
}{
90+
{
91+
name: "with potentially executed true",
92+
err: errors.New("connection error"),
93+
potentiallyExecuted: true,
94+
expected: "connection error (potentially executed: true)",
95+
},
96+
{
97+
name: "with potentially executed false",
98+
err: errors.New("syntax error"),
99+
potentiallyExecuted: false,
100+
expected: "syntax error (potentially executed: false)",
101+
},
102+
}
103+
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
qErr := &QueryError{
107+
err: tt.err,
108+
potentiallyExecuted: tt.potentiallyExecuted,
109+
}
110+
111+
got := qErr.Error()
112+
if got != tt.expected {
113+
t.Errorf("QueryError.Error() = %v, expected %v", got, tt.expected)
114+
}
115+
})
116+
}
117+
}

0 commit comments

Comments
 (0)