diff --git a/backoff.go b/backoff.go index 90492d2..2d16ffe 100644 --- a/backoff.go +++ b/backoff.go @@ -24,6 +24,10 @@ import ( type JitterFunc func(int) int +type DelayerMaker interface { + NewDelayer(float64, float64, time.Duration) Delayer +} + type Delayer interface { Delay() DelayContext(context.Context) bool @@ -62,8 +66,9 @@ func (r *RealTime) After(d time.Duration) <-chan time.Time { return time.After(d) } -func NewBackoff(maxBackoff float64, resetDelta float64, - durationUnit time.Duration) *Backoff { +type BackoffMaker struct{} + +func (bm *BackoffMaker) NewDelayer(maxBackoff float64, resetDelta float64, durationUnit time.Duration) Delayer { timeTeller := &RealTime{} return NewBackoffForTesting( maxBackoff, resetDelta, durationUnit, jitterFunc, timeTeller) diff --git a/fake_delayer.go b/fake_delayer.go index 1b181fb..19b200a 100644 --- a/fake_delayer.go +++ b/fake_delayer.go @@ -17,8 +17,18 @@ package main import ( "context" "log" + "time" ) +type FakeDelayerMaker struct{} + +func (fdm *FakeDelayerMaker) NewDelayer(_ float64, _ float64, _ time.Duration) Delayer { + return &FakeDelayer{ + DelayOnChan: false, + StopDelay: make(chan bool), + } +} + type FakeDelayer struct { DelayOnChan bool StopDelay chan bool diff --git a/irc.go b/irc.go index bdd3eb5..f4b5ff0 100644 --- a/irc.go +++ b/irc.go @@ -91,7 +91,7 @@ type IRCNotifier struct { BackoffCounter Delayer } -func NewIRCNotifier(stopCtx context.Context, stopWg *sync.WaitGroup, config *Config, alertMsgs chan AlertMsg) (*IRCNotifier, error) { +func NewIRCNotifier(stopCtx context.Context, stopWg *sync.WaitGroup, config *Config, alertMsgs chan AlertMsg, delayerMaker DelayerMaker) (*IRCNotifier, error) { ircConfig := irc.NewConfig(config.IRCNick) ircConfig.Me.Ident = config.IRCNick @@ -108,7 +108,7 @@ func NewIRCNotifier(stopCtx context.Context, stopWg *sync.WaitGroup, config *Con ircConfig.Timeout = connectionTimeoutSecs * time.Second ircConfig.NewNick = func(n string) string { return n + "^" } - backoffCounter := NewBackoff( + backoffCounter := delayerMaker.NewDelayer( ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs, time.Second) @@ -183,9 +183,10 @@ func (n *IRCNotifier) JoinChannel(channel *IRCChannel) { } log.Printf("Joining %s", channel.Name) n.Client.Join(channel.Name, channel.Password) + bm := BackoffMaker{} state := ChannelState{ Channel: *channel, - BackoffCounter: NewBackoff( + BackoffCounter: bm.NewDelayer( ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs, time.Second), } diff --git a/irc_test.go b/irc_test.go index bba4cd8..45cd4cf 100644 --- a/irc_test.go +++ b/irc_test.go @@ -212,19 +212,16 @@ func makeTestIRCConfig(IRCPort int) *Config { } func makeTestNotifier(t *testing.T, config *Config) (*IRCNotifier, chan AlertMsg, context.CancelFunc, *sync.WaitGroup) { + fakeDelayerMaker := &FakeDelayerMaker{} alertMsgs := make(chan AlertMsg) ctx, cancel := context.WithCancel(context.Background()) stopWg := sync.WaitGroup{} stopWg.Add(1) - notifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs) + notifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs, fakeDelayerMaker) if err != nil { t.Fatal(fmt.Sprintf("Could not create IRC notifier: %s", err)) } notifier.Client.Config().Flood = true - notifier.BackoffCounter = &FakeDelayer{ - DelayOnChan: false, - StopDelay: make(chan bool), - } return notifier, alertMsgs, cancel, &stopWg } diff --git a/main.go b/main.go index 97a4ea2..8a318dc 100644 --- a/main.go +++ b/main.go @@ -59,7 +59,7 @@ func main() { alertMsgs := make(chan AlertMsg, config.AlertBufferSize) stopWg.Add(1) - ircNotifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs) + ircNotifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs, &BackoffMaker{}) if err != nil { log.Printf("Could not create IRC notifier: %s", err) return