forked from mantzas/patron
-
Notifications
You must be signed in to change notification settings - Fork 67
/
options_test.go
89 lines (74 loc) · 2.06 KB
/
options_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
81
82
83
84
85
86
87
88
89
package patron
import (
"errors"
"log/slog"
"testing"
"github.com/beatlabs/patron/observability"
"github.com/beatlabs/patron/observability/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLogFields(t *testing.T) {
attrs := []slog.Attr{slog.String("key", "value")}
attrs1 := []slog.Attr{slog.String("name1", "version1")}
expectedSuccess := observability.Config{LogConfig: log.Config{
Attributes: attrs,
}}
expectedNoOverwrite := observability.Config{LogConfig: log.Config{
Attributes: []slog.Attr{slog.String("name1", "version2")},
}}
type args struct {
fields []slog.Attr
}
tests := map[string]struct {
args args
want observability.Config
expectedErr string
}{
"empty attributes": {args: args{fields: nil}, expectedErr: "attributes are empty"},
"success": {args: args{fields: attrs}, want: expectedSuccess},
"no overwrite": {args: args{fields: attrs1}, want: expectedNoOverwrite},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
svc := &Service{
observabilityCfg: observability.Config{},
}
err := WithLogFields(tt.args.fields...)(svc)
if tt.expectedErr == "" {
require.NoError(t, err)
assert.Equal(t, tt.want, svc.observabilityCfg)
} else {
require.EqualError(t, err, tt.expectedErr)
}
})
}
}
func TestSIGHUP(t *testing.T) {
t.Parallel()
t.Run("empty value for sighup handler", func(t *testing.T) {
t.Parallel()
svc := &Service{}
err := WithSIGHUP(nil)(svc)
assert.Equal(t, errors.New("provided WithSIGHUP handler was nil"), err)
assert.Nil(t, svc.sighupHandler)
})
t.Run("non empty value for sighup handler", func(t *testing.T) {
t.Parallel()
svc := &Service{}
comp := &testSighupAlterable{}
err := WithSIGHUP(testSighupHandle(comp))(svc)
require.NoError(t, err)
assert.NotNil(t, svc.sighupHandler)
svc.sighupHandler()
assert.Equal(t, 1, comp.value)
})
}
type testSighupAlterable struct {
value int
}
func testSighupHandle(value *testSighupAlterable) func() {
return func() {
value.value = 1
}
}