-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretry_binding_selector_test.go
103 lines (81 loc) · 2.64 KB
/
retry_binding_selector_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
package sqldb
import (
"context"
"errors"
"testing"
"time"
"github.com/smarty/assertions"
"github.com/smarty/assertions/should"
"github.com/smarty/gunit"
)
func TestRetryBindingSelectorFixture(t *testing.T) {
gunit.Run(new(RetryBindingSelectorFixture), t)
}
type RetryBindingSelectorFixture struct {
*gunit.Fixture
inner *FakeRetrySelector
selector *RetryBindingSelector
}
func (this *RetryBindingSelectorFixture) Setup() {
this.inner = &FakeRetrySelector{}
this.selector = NewRetryBindingSelector(this.inner, time.Millisecond*3)
}
///////////////////////////////////////////////////////////////
func (this *RetryBindingSelectorFixture) TestSelectWithoutErrors() {
err := this.selector.BindSelect(context.Background(), nil, "statement", 1, 2, 3)
this.So(err, should.Equal, err)
this.So(this.inner.count, should.Equal, 1)
this.So(this.inner.statement, should.Equal, "statement")
this.So(this.inner.parameters, should.Resemble, []interface{}{1, 2, 3})
}
func (this *RetryBindingSelectorFixture) TestRetryUntilSuccess() {
this.inner.errorCount = 4
started := time.Now().UTC()
err := this.selector.BindSelect(context.Background(), nil, "statement", 1, 2, 3)
this.So(err, should.Equal, err)
this.So(this.inner.count, should.Equal, 5) // last attempt is successful
this.So(time.Since(started), should.BeGreaterThan, time.Millisecond*3*4) // 3ms * 4 sleeps
}
///////////////////////////////////////////////////////////////
type FakeRetrySelector struct {
count int
errorCount int
binder Binder
statement string
parameters []interface{}
}
func (this *FakeRetrySelector) Ping(_ context.Context) error {
panic("Should not be called.")
}
func (this *FakeRetrySelector) BeginTransaction(_ context.Context) (BindingTransaction, error) {
panic("Should not be called.")
}
func (this *FakeRetrySelector) Close() error {
panic("Should not be called.")
}
func (this *FakeRetrySelector) BindSelect(_ context.Context, binder Binder, statement string, parameters ...interface{}) error {
if this.binder == nil {
this.binder = binder
} else {
assertions.So(this.binder, should.Equal, binder)
}
if this.statement == "" {
this.statement = statement
} else {
assertions.So(this.statement, should.Equal, statement)
}
if len(this.parameters) == 0 {
this.parameters = parameters
} else {
assertions.So(this.parameters, should.Resemble, parameters)
}
this.count++
if this.errorCount > 0 && this.count <= this.errorCount {
return errors.New("")
} else {
return nil
}
}
func (this *FakeRetrySelector) Execute(_ context.Context, _ string, _ ...interface{}) (uint64, error) {
panic("Should not be called.")
}