make the reconnect delay interruptible by the session context
This commit is contained in:
+8
-2
@@ -316,8 +316,14 @@ func (w *session) reset(ctx context.Context) bool {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
// refresh session
|
// refresh session timeout
|
||||||
time.Sleep(w.config.ReconnectDelay)
|
timer := time.NewTimer(w.config.ReconnectDelay)
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
timer.Stop()
|
||||||
|
return false
|
||||||
|
case <-timer.C:
|
||||||
|
}
|
||||||
w.restartCount.Add(1)
|
w.restartCount.Add(1)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -829,3 +829,28 @@ func TestSession_Send(t *testing.T) {
|
|||||||
assert.ErrorIs(t, err, ErrConnectionUnavailable)
|
assert.ErrorIs(t, err, ErrConnectionUnavailable)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSessionResetCancel(t *testing.T) {
|
||||||
|
w := &session{
|
||||||
|
config: SessionConfig{ReconnectDelay: 30 * time.Second},
|
||||||
|
restartCount: &atomic.Uint64{},
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
done := make(chan bool, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
done <- w.reset(ctx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
honeybeetest.Eventually(t, func() bool {
|
||||||
|
select {
|
||||||
|
case ok := <-done:
|
||||||
|
return !ok
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}, "expected reset to return false promptly after cancel")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user