diff --git a/irc.go b/irc.go index 48cdeae..3d2da05 100644 --- a/irc.go +++ b/irc.go @@ -286,6 +286,8 @@ func (notifier *IRCNotifier) SetupPhase() { notifier.MaybeIdentifyNick() notifier.JoinChannels() ircConnectedGauge.Set(1) + case <-notifier.sessionDownSignal: + log.Printf("Receiving a session down before the session is up, this is odd") case <-notifier.ctx.Done(): log.Printf("IRC routine asked to terminate") } diff --git a/irc_test.go b/irc_test.go index 106ca3e..970aeaf 100644 --- a/irc_test.go +++ b/irc_test.go @@ -196,14 +196,21 @@ func makeTestServer(t *testing.T) (*testServer, int) { } type FakeDelayer struct { + DelayOnChan bool + StopDelay chan bool } func (f *FakeDelayer) Delay() { - log.Printf("Faking Backoff") + f.DelayContext(context.Background()) } func (f *FakeDelayer) DelayContext(ctx context.Context) bool { log.Printf("Faking Backoff") + if f.DelayOnChan { + log.Printf("Waiting StopDelay signal") + <-f.StopDelay + log.Printf("Received StopDelay signal") + } return true } @@ -233,7 +240,10 @@ func makeTestNotifier(t *testing.T, config *Config) (*IRCNotifier, chan AlertMsg t.Fatal(fmt.Sprintf("Could not create IRC notifier: %s", err)) } notifier.Client.Config().Flood = true - notifier.BackoffCounter = &FakeDelayer{} + notifier.BackoffCounter = &FakeDelayer{ + DelayOnChan: false, + StopDelay: make(chan bool), + } return notifier, alertMsgs, cancel, &stopWg } @@ -628,6 +638,11 @@ func TestConnectErrorRetry(t *testing.T) { // a connection error. config.IRCUseSSL = true notifier, _, cancel, _ := makeTestNotifier(t, config) + // Pilot reconnect attempts via backoff delay to prevent race + // conditions in the test while we change the components behavior on + // the fly. + delayer := notifier.BackoffCounter.(*FakeDelayer) + delayer.DelayOnChan = true var testStep, joinStep sync.WaitGroup @@ -640,6 +655,8 @@ func TestConnectErrorRetry(t *testing.T) { go notifier.Run() + delayer.StopDelay <- true + testStep.Wait() // We have caused a connection failure, now check for a reconnection @@ -655,6 +672,8 @@ func TestConnectErrorRetry(t *testing.T) { server.SetHandler("JOIN", joinHandler) server.SetCloseEarly(nil) + delayer.StopDelay <- true + joinStep.Wait() cancel()