mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2024-11-06 03:29:23 +01:00
Prevent race condition in TestConnectErrorRetry
Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
parent
2471b866f1
commit
1eeb4dda9c
2
irc.go
2
irc.go
@ -286,6 +286,8 @@ func (notifier *IRCNotifier) SetupPhase() {
|
|||||||
notifier.MaybeIdentifyNick()
|
notifier.MaybeIdentifyNick()
|
||||||
notifier.JoinChannels()
|
notifier.JoinChannels()
|
||||||
ircConnectedGauge.Set(1)
|
ircConnectedGauge.Set(1)
|
||||||
|
case <-notifier.sessionDownSignal:
|
||||||
|
log.Printf("Receiving a session down before the session is up, this is odd")
|
||||||
case <-notifier.ctx.Done():
|
case <-notifier.ctx.Done():
|
||||||
log.Printf("IRC routine asked to terminate")
|
log.Printf("IRC routine asked to terminate")
|
||||||
}
|
}
|
||||||
|
23
irc_test.go
23
irc_test.go
@ -196,14 +196,21 @@ func makeTestServer(t *testing.T) (*testServer, int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type FakeDelayer struct {
|
type FakeDelayer struct {
|
||||||
|
DelayOnChan bool
|
||||||
|
StopDelay chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeDelayer) Delay() {
|
func (f *FakeDelayer) Delay() {
|
||||||
log.Printf("Faking Backoff")
|
f.DelayContext(context.Background())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeDelayer) DelayContext(ctx context.Context) bool {
|
func (f *FakeDelayer) DelayContext(ctx context.Context) bool {
|
||||||
log.Printf("Faking Backoff")
|
log.Printf("Faking Backoff")
|
||||||
|
if f.DelayOnChan {
|
||||||
|
log.Printf("Waiting StopDelay signal")
|
||||||
|
<-f.StopDelay
|
||||||
|
log.Printf("Received StopDelay signal")
|
||||||
|
}
|
||||||
return true
|
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))
|
t.Fatal(fmt.Sprintf("Could not create IRC notifier: %s", err))
|
||||||
}
|
}
|
||||||
notifier.Client.Config().Flood = true
|
notifier.Client.Config().Flood = true
|
||||||
notifier.BackoffCounter = &FakeDelayer{}
|
notifier.BackoffCounter = &FakeDelayer{
|
||||||
|
DelayOnChan: false,
|
||||||
|
StopDelay: make(chan bool),
|
||||||
|
}
|
||||||
|
|
||||||
return notifier, alertMsgs, cancel, &stopWg
|
return notifier, alertMsgs, cancel, &stopWg
|
||||||
}
|
}
|
||||||
@ -628,6 +638,11 @@ func TestConnectErrorRetry(t *testing.T) {
|
|||||||
// a connection error.
|
// a connection error.
|
||||||
config.IRCUseSSL = true
|
config.IRCUseSSL = true
|
||||||
notifier, _, cancel, _ := makeTestNotifier(t, config)
|
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
|
var testStep, joinStep sync.WaitGroup
|
||||||
|
|
||||||
@ -640,6 +655,8 @@ func TestConnectErrorRetry(t *testing.T) {
|
|||||||
|
|
||||||
go notifier.Run()
|
go notifier.Run()
|
||||||
|
|
||||||
|
delayer.StopDelay <- true
|
||||||
|
|
||||||
testStep.Wait()
|
testStep.Wait()
|
||||||
|
|
||||||
// We have caused a connection failure, now check for a reconnection
|
// 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.SetHandler("JOIN", joinHandler)
|
||||||
server.SetCloseEarly(nil)
|
server.SetCloseEarly(nil)
|
||||||
|
|
||||||
|
delayer.StopDelay <- true
|
||||||
|
|
||||||
joinStep.Wait()
|
joinStep.Wait()
|
||||||
|
|
||||||
cancel()
|
cancel()
|
||||||
|
Loading…
Reference in New Issue
Block a user