-
Notifications
You must be signed in to change notification settings - Fork 16
/
options_test.go
170 lines (141 loc) · 4.05 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package semaphore
import (
"errors"
"testing"
"github.com/jexia/semaphore/v2/pkg/broker"
"github.com/jexia/semaphore/v2/pkg/broker/logger"
"github.com/jexia/semaphore/v2/pkg/codec/json"
"github.com/jexia/semaphore/v2/pkg/functions"
"github.com/jexia/semaphore/v2/pkg/specs"
"github.com/jexia/semaphore/v2/pkg/transport/http"
"go.uber.org/zap/zapcore"
)
func TestWithFlowsOption(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
resolver := func(*broker.Context) (specs.FlowListInterface, error) { return nil, nil }
result, err := NewOptions(ctx, WithFlows(resolver))
if err != nil {
t.Fatal(err)
}
if len(result.FlowResolvers) != 1 {
t.Fatal("unexpected result expected flow resolver to be set")
}
}
func TestWithMultipleFlowsOption(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
resolver := func(*broker.Context) (specs.FlowListInterface, error) { return nil, nil }
result, err := NewOptions(ctx, WithFlows(resolver), WithFlows(resolver))
if err != nil {
t.Fatal(err)
}
if len(result.FlowResolvers) != 2 {
t.Fatal("unexpected result expected multiple flow resolvers to be set")
}
}
func TestWithLogLevel(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
_, err := NewOptions(ctx, WithLogLevel("*", "debug"))
if err != nil {
t.Fatal(err)
}
}
func TestWithFunctions(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
options, err := NewOptions(ctx, WithFunctions(functions.Custom{"mock": nil}))
if err != nil {
t.Fatal(err)
}
if options.Functions == nil {
t.Fatal("functions not set")
}
_, has := options.Functions["mock"]
if !has {
t.Fatal("mock function does not exist")
}
}
func TestWithCaller(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
options, err := NewOptions(ctx, WithCaller(http.NewCaller()))
if err != nil {
t.Fatal(err)
}
if options.Callers == nil {
t.Fatal("callers not set")
}
caller := options.Callers.Get("http")
if caller == nil {
t.Fatal("HTTP caller does not exist")
}
}
func TestWithCodec(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
options, err := NewOptions(ctx, WithCodec(json.NewConstructor()))
if err != nil {
t.Fatal(err)
}
if options.Codec == nil {
t.Fatal("codecs not set")
}
codec := options.Codec.Get("json")
if codec == nil {
t.Fatal("JSON codec does not exist")
}
}
func TestWithInvalidLogLevel(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
_, err := NewOptions(ctx, WithLogLevel("*", "unkown"))
if err != nil {
t.Fatal(err)
}
if ctx.Atom.Level() != zapcore.ErrorLevel {
t.Fatalf("unexpected atom level %s, expected %s", ctx.Atom.Level(), zapcore.ErrorLevel)
}
}
func TestWithInvalidLogLevelPattern(t *testing.T) {
ctx := logger.WithLogger(broker.WithModule(broker.NewBackground(), "x"))
_, err := NewOptions(ctx, WithLogLevel("[x-]", "error"))
if err != nil {
t.Fatal("unexpected pass")
}
if ctx.Atom.Level() != zapcore.ErrorLevel {
t.Fatalf("unexpected atom level %s, expected %s", ctx.Atom.Level(), zapcore.ErrorLevel)
}
}
func TestNewOptions(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
_, err := NewOptions(ctx)
if err != nil {
t.Fatal(err)
}
}
func TestNewOptionsNil(t *testing.T) {
ctx := logger.WithLogger(broker.NewBackground())
_, err := NewOptions(ctx, nil)
if err != nil {
t.Fatal(err)
}
}
func TestNewOptionsMiddleware(t *testing.T) {
middleware := MiddlewareFunc(func(*broker.Context) ([]Option, error) {
options := []Option{
WithLogLevel("*", "warn"),
}
return options, nil
})
ctx := logger.WithLogger(broker.NewBackground())
_, err := NewOptions(ctx, WithMiddleware(middleware))
if err != nil {
t.Fatal(err)
}
}
func TestNewOptionsMiddlewareErr(t *testing.T) {
expected := errors.New("unexpected err")
middleware := MiddlewareFunc(func(*broker.Context) ([]Option, error) {
return nil, expected
})
ctx := logger.WithLogger(broker.NewBackground())
_, err := NewOptions(ctx, WithMiddleware(middleware))
if err != expected {
t.Fatalf("unexpected err (%+v), expected (%+v)", err, expected)
}
}