-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_test.go
159 lines (142 loc) · 3.41 KB
/
connection_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
package hansip
import (
"errors"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type ConnectionTestSuite struct {
suite.Suite
}
func TestConnection(t *testing.T) {
s := &ConnectionTestSuite{}
suite.Run(t, s)
}
func (s *ConnectionTestSuite) TestPingOK() {
c := &connection{
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 1 * time.Second,
connCheckDelay: 100 * time.Millisecond,
s: &dummySQL{},
pingFn: func() error {
return nil
},
}
s.Nil(c.ping())
}
func (s *ConnectionTestSuite) TestPingFail() {
c := &connection{
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 1 * time.Second,
connCheckDelay: 100 * time.Millisecond,
s: &dummySQL{},
pingFn: func() error {
return errors.New("something fails")
},
}
s.EqualError(c.ping(), "something fails")
}
func (s *ConnectionTestSuite) TestPingTimeout() {
c := &connection{
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 100 * time.Millisecond,
pingFn: func() error {
time.Sleep(200 * time.Millisecond)
return errors.New("fail")
},
}
s.Equal(errPingTimeout, c.ping())
}
func (s *ConnectionTestSuite) TestUpdateStatusWithPingOK() {
c := &connection{
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 1 * time.Second,
connCheckDelay: 100 * time.Millisecond,
s: &dummySQL{},
pingFn: func() error {
return nil
},
}
c.updateStatus()
s.True(c.getConnected())
}
func (s *ConnectionTestSuite) TestUpdateStatusWithPingError() {
c := &connection{
connected: 1,
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 1 * time.Second,
connCheckDelay: 100 * time.Millisecond,
s: &dummySQL{},
pingFn: func() error {
return errors.New("something fails")
},
}
c.updateStatus()
s.False(c.getConnected())
}
func (s *ConnectionTestSuite) TestConnected() {
c := &connection{
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 1 * time.Second,
connCheckDelay: 100 * time.Millisecond,
s: &dummySQL{},
pingFn: func() error {
return errors.New("something fails")
},
}
c.setConnected(true)
s.True(c.getConnected())
c.setConnected(false)
s.False(c.getConnected())
}
func (s *ConnectionTestSuite) TestQuit() {
var closed bool
c := &connection{
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 1 * time.Second,
connCheckDelay: 100 * time.Millisecond,
s: &dummySQL{},
pingFn: func() error {
return errors.New("fail")
},
closeFn: func() {
closed = true
},
}
go c.loop()
c.quit()
time.Sleep(300 * time.Millisecond) // wait quit to finish running
s.True(closed)
s.False(c.getConnected())
}
func (s *ConnectionTestSuite) TestLoopTriggersUpdateStatus() {
var pingErrFlag int32
c := &connection{
host: "dummy",
quitChan: make(chan struct{}),
pingTimeout: 1 * time.Second,
connCheckDelay: 100 * time.Millisecond,
s: &dummySQL{},
pingFn: func() error {
if atomic.LoadInt32(&pingErrFlag) == 0 {
return nil
}
return errors.New("something fails")
},
}
go c.loop()
// wait loop to kick in
time.Sleep(300 * time.Millisecond)
s.True(c.getConnected())
atomic.StoreInt32(&pingErrFlag, 1)
time.Sleep(300 * time.Millisecond)
s.False(c.getConnected())
}