Make sure all shoutrrr notifications are sent (#564)

This commit is contained in:
Cédric Finance 2020-08-08 22:55:51 +02:00 committed by GitHub
parent b8408269bb
commit fdecd40189
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 122 additions and 15 deletions

View file

@ -1,6 +1,7 @@
package notifications
import (
"github.com/containrrr/shoutrrr/pkg/types"
"testing"
"text/template"
@ -102,3 +103,69 @@ func TestShoutrrrInvalidTemplateUsesTemplate(t *testing.T) {
require.Equal(t, sd, s)
}
type blockingRouter struct {
unlock chan bool
sent chan bool
}
func (b blockingRouter) Send(message string, params *types.Params) []error {
_ = <-b.unlock
b.sent <- true
return nil
}
func TestSlowNotificationNotSent(t *testing.T) {
_, blockingRouter := sendNotificationsWithBlockingRouter()
notifSent := false
select {
case notifSent = <-blockingRouter.sent:
default:
}
require.Equal(t, false, notifSent)
}
func TestSlowNotificationSent(t *testing.T) {
shoutrrr, blockingRouter := sendNotificationsWithBlockingRouter()
blockingRouter.unlock <- true
shoutrrr.Close()
notifSent := false
select {
case notifSent = <-blockingRouter.sent:
default:
}
require.Equal(t, true, notifSent)
}
func sendNotificationsWithBlockingRouter() (*shoutrrrTypeNotifier, *blockingRouter) {
cmd := new(cobra.Command)
router := &blockingRouter{
unlock: make(chan bool, 1),
sent: make(chan bool, 1),
}
shoutrrr := &shoutrrrTypeNotifier{
template: getShoutrrrTemplate(cmd),
messages: make(chan string, 1),
done: make(chan bool),
Router: router,
}
entry := &log.Entry{
Message: "foo bar",
}
go sendNotifications(shoutrrr)
shoutrrr.StartNotification()
shoutrrr.Fire(entry)
shoutrrr.SendNotification()
return shoutrrr, router
}