-
Notifications
You must be signed in to change notification settings - Fork 218
/
send_test.go
80 lines (62 loc) · 1.91 KB
/
send_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package notify
import (
"context"
"testing"
"github.com/nikoksr/notify/service/mail"
)
func TestNotifySend(t *testing.T) {
t.Parallel()
n := New()
if n.Disabled {
t.Fatal("New() returned disabled Notifier")
}
// Nil context
//nolint:staticcheck // Purposefully testing with nil context.
if err := n.Send(nil, "subject", "message"); err != nil {
t.Errorf("Send() returned error: %v", err)
}
if r := recover(); r != nil {
t.Errorf("Send() panicked: %v", r)
}
ctx := context.Background()
if err := n.Send(ctx, "subject", "message"); err != nil {
t.Errorf("Send() returned error: %v", err)
}
// This is not meant to test the mail service, but rather the general capability of the Send() function to catch
// errors.
n.UseServices(mail.New("", ""))
if err := n.Send(ctx, "subject", "message"); err == nil {
t.Errorf("Send() invalid mail returned no error: %v", err)
}
// After disabling the Notifier, Send() should return silently.
n.WithOptions(Disable)
if err := n.Send(ctx, "subject", "message"); err != nil {
t.Errorf("Send() of disabled Notifier returned error: %v", err)
}
n.WithOptions(Enable)
// Smuggle in a nil service. This usually never happens, since UseServices filters out nil services. But, it's good
// to test anyway.
n.notifiers = make([]Notifier, 0)
n.notifiers = append(n.notifiers, nil)
if err := n.Send(ctx, "subject", "message"); err != nil {
t.Errorf("Send() of disabled Notifier returned no error: %v", err)
}
if r := recover(); r != nil {
t.Errorf("Send() with nil service panicked: %v", r)
}
}
func TestSendMany(t *testing.T) {
t.Parallel()
n := New()
if n == nil {
t.Fatal("New() returned nil")
}
var services []Notifier
for range 10 {
services = append(services, mail.New("", ""))
}
n.UseServices(services...)
if err := n.Send(context.Background(), "subject", "message"); err == nil {
t.Errorf("Send() invalid mail returned no error: %v", err)
}
}