mirror of
https://github.com/google/alertmanager-irc-relay.git
synced 2024-12-29 06:12:51 +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 JitterFunc func(int) int
|
||||||
|
|
||||||
|
type DelayerMaker interface {
|
||||||
|
NewDelayer(float64, float64, time.Duration) Delayer
|
||||||
|
}
|
||||||
|
|
||||||
type Delayer interface {
|
type Delayer interface {
|
||||||
Delay()
|
Delay()
|
||||||
DelayContext(context.Context) bool
|
DelayContext(context.Context) bool
|
||||||
@ -62,8 +66,9 @@ func (r *RealTime) After(d time.Duration) <-chan time.Time {
|
|||||||
return time.After(d)
|
return time.After(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBackoff(maxBackoff float64, resetDelta float64,
|
type BackoffMaker struct{}
|
||||||
durationUnit time.Duration) *Backoff {
|
|
||||||
|
func (bm *BackoffMaker) NewDelayer(maxBackoff float64, resetDelta float64, durationUnit time.Duration) Delayer {
|
||||||
timeTeller := &RealTime{}
|
timeTeller := &RealTime{}
|
||||||
return NewBackoffForTesting(
|
return NewBackoffForTesting(
|
||||||
maxBackoff, resetDelta, durationUnit, jitterFunc, timeTeller)
|
maxBackoff, resetDelta, durationUnit, jitterFunc, timeTeller)
|
||||||
|
@ -17,8 +17,18 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"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 {
|
type FakeDelayer struct {
|
||||||
DelayOnChan bool
|
DelayOnChan bool
|
||||||
StopDelay chan bool
|
StopDelay chan bool
|
||||||
|
7
irc.go
7
irc.go
@ -91,7 +91,7 @@ type IRCNotifier struct {
|
|||||||
BackoffCounter Delayer
|
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 := irc.NewConfig(config.IRCNick)
|
||||||
ircConfig.Me.Ident = 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.Timeout = connectionTimeoutSecs * time.Second
|
||||||
ircConfig.NewNick = func(n string) string { return n + "^" }
|
ircConfig.NewNick = func(n string) string { return n + "^" }
|
||||||
|
|
||||||
backoffCounter := NewBackoff(
|
backoffCounter := delayerMaker.NewDelayer(
|
||||||
ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs,
|
ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs,
|
||||||
time.Second)
|
time.Second)
|
||||||
|
|
||||||
@ -183,9 +183,10 @@ func (n *IRCNotifier) JoinChannel(channel *IRCChannel) {
|
|||||||
}
|
}
|
||||||
log.Printf("Joining %s", channel.Name)
|
log.Printf("Joining %s", channel.Name)
|
||||||
n.Client.Join(channel.Name, channel.Password)
|
n.Client.Join(channel.Name, channel.Password)
|
||||||
|
bm := BackoffMaker{}
|
||||||
state := ChannelState{
|
state := ChannelState{
|
||||||
Channel: *channel,
|
Channel: *channel,
|
||||||
BackoffCounter: NewBackoff(
|
BackoffCounter: bm.NewDelayer(
|
||||||
ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs,
|
ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs,
|
||||||
time.Second),
|
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) {
|
func makeTestNotifier(t *testing.T, config *Config) (*IRCNotifier, chan AlertMsg, context.CancelFunc, *sync.WaitGroup) {
|
||||||
|
fakeDelayerMaker := &FakeDelayerMaker{}
|
||||||
alertMsgs := make(chan AlertMsg)
|
alertMsgs := make(chan AlertMsg)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
stopWg := sync.WaitGroup{}
|
stopWg := sync.WaitGroup{}
|
||||||
stopWg.Add(1)
|
stopWg.Add(1)
|
||||||
notifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs)
|
notifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs, fakeDelayerMaker)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
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{
|
|
||||||
DelayOnChan: false,
|
|
||||||
StopDelay: make(chan bool),
|
|
||||||
}
|
|
||||||
|
|
||||||
return notifier, alertMsgs, cancel, &stopWg
|
return notifier, alertMsgs, cancel, &stopWg
|
||||||
}
|
}
|
||||||
|
2
main.go
2
main.go
@ -59,7 +59,7 @@ func main() {
|
|||||||
alertMsgs := make(chan AlertMsg, config.AlertBufferSize)
|
alertMsgs := make(chan AlertMsg, config.AlertBufferSize)
|
||||||
|
|
||||||
stopWg.Add(1)
|
stopWg.Add(1)
|
||||||
ircNotifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs)
|
ircNotifier, err := NewIRCNotifier(ctx, &stopWg, config, alertMsgs, &BackoffMaker{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Could not create IRC notifier: %s", err)
|
log.Printf("Could not create IRC notifier: %s", err)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user