-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_handler_test.go
More file actions
71 lines (55 loc) · 1.19 KB
/
email_handler_test.go
File metadata and controls
71 lines (55 loc) · 1.19 KB
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
package multislog
import (
"context"
"errors"
"log/slog"
"testing"
)
var errSMTPFailure = errors.New("smtp failure")
type mockEmailHandler struct {
closed bool
fail bool
}
func (m *mockEmailHandler) Enabled(context.Context, slog.Level) bool {
return true
}
func (m *mockEmailHandler) Handle(context.Context, slog.Record) error {
return nil
}
func (m *mockEmailHandler) WithAttrs([]slog.Attr) slog.Handler {
return m
}
func (m *mockEmailHandler) WithGroup(string) slog.Handler {
return m
}
func (m *mockEmailHandler) Close() error {
m.closed = true
if m.fail {
return errSMTPFailure
}
return nil
}
func TestClose_EmailHandler_CloseCalled(t *testing.T) {
h := &mockEmailHandler{}
ms := &Multislog{
Logger: slog.New(h),
handlers: []slog.Handler{h},
}
ms.Close()
if !h.closed {
t.Fatal("expected email handler Close() to be called")
}
}
func TestClose_EmailHandler_CloseErrorIgnored(t *testing.T) {
h1 := &mockEmailHandler{fail: true}
h2 := &mockEmailHandler{}
ms := &Multislog{
Logger: slog.New(h1),
handlers: []slog.Handler{h1, h2},
}
// Must not panic
ms.Close()
if !h1.closed || !h2.closed {
t.Fatal("expected all handlers to be closed even after error")
}
}