mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2024-12-28 13:52:46 +01:00
Add factory-like interface to generate Delayers
Will be used to inject fake delayers in objects created during tests Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
parent
bc13e4be9c
commit
cb65b4d28d
@ -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)
|
||||
|
@ -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
|
||||
|
7
irc.go
7
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),
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
2
main.go
2
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
|
||||
|
Loading…
Reference in New Issue
Block a user