38 lines
1.0 KiB
Go
38 lines
1.0 KiB
Go
package alerts
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
type recordingNotifier struct {
|
|
mu sync.Mutex
|
|
msgs []string
|
|
err error
|
|
}
|
|
|
|
func (r *recordingNotifier) Notify(_ context.Context, message string) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.msgs = append(r.msgs, message)
|
|
return r.err
|
|
}
|
|
|
|
func TestBroadcast_DeliversToEveryRecipientEvenIfOneFails(t *testing.T) {
|
|
failing := &recordingNotifier{err: errors.New("webhook mort")}
|
|
ok := &recordingNotifier{}
|
|
Broadcast(func(context.Context) ([]Notifier, error) { return []Notifier{failing, ok}, nil }, "incident")
|
|
|
|
if len(failing.msgs) != 1 || len(ok.msgs) != 1 || ok.msgs[0] != "incident" {
|
|
t.Errorf("failing=%v ok=%v : l'échec d'un canal a empêché l'autre", failing.msgs, ok.msgs)
|
|
}
|
|
}
|
|
|
|
func TestBroadcast_SwallowsRecipientErrorsAndEmptyLists(t *testing.T) {
|
|
// Ne doit ni paniquer ni bloquer.
|
|
Broadcast(func(context.Context) ([]Notifier, error) { return nil, errors.New("db indisponible") }, "x")
|
|
Broadcast(func(context.Context) ([]Notifier, error) { return nil, nil }, "x")
|
|
}
|