Skip to content

Commit

Permalink
Make sure that failing Client can be closed
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Nov 27, 2014
1 parent e199c16 commit 2e7c90f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func (c *Client) connect() *connInfo {
var lastErr error
consecutiveFailures := 0
for {
if c.isClosed() {
log.Tracef("Connection closed, stop trying to connect")
return &connInfo{
err: closedError,
}
}
if consecutiveFailures > c.ReconnectAttempts {
log.Tracef("Done trying to connect: %s", lastErr)
return &connInfo{
Expand Down
14 changes: 13 additions & 1 deletion waddell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,23 @@ func TestBadDialerWithMultipleReconnect(t *testing.T) {
start := time.Now()
_, err := client.Connect()
delta := time.Now().Sub(start)
assert.Error(t, err, "Connecting with no reconnect attempts should have failed")
assert.Error(t, err, "Connecting with 2 reconnect attempts should have failed")
expectedDelta := reconnectDelayInterval * 3
assert.True(t, delta >= expectedDelta, fmt.Sprintf("Reconnecting didn't wait long enough. Should have waited %s, only waited %s", expectedDelta, delta))
}

func TestCloseFailing(t *testing.T) {
client := &Client{
ReconnectAttempts: 100,
Dial: func() (net.Conn, error) {
return nil, fmt.Errorf("I won't dial, no way!")
},
}
go client.Connect()
time.Sleep(100 * time.Millisecond)
client.Close()
}

func TestCloseUnconnected(t *testing.T) {
client := &Client{}
err := client.Close()
Expand Down

0 comments on commit 2e7c90f

Please sign in to comment.