-
Notifications
You must be signed in to change notification settings - Fork 9
/
server_test.go
172 lines (141 loc) · 3.85 KB
/
server_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
171
172
// Copyright 2024 SeatGeek, Inc.
//
// Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
package mailroom
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/seatgeek/mailroom/pkg/common"
"github.com/seatgeek/mailroom/pkg/event"
"github.com/seatgeek/mailroom/pkg/handler"
"github.com/seatgeek/mailroom/pkg/identifier"
"github.com/seatgeek/mailroom/pkg/user"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
s := New()
assert.NotNil(t, s)
assert.Equal(t, "0.0.0.0:8000", s.listenAddr)
}
func TestWithHandlers(t *testing.T) {
src1 := handler.NewMockHandler(t)
src1.EXPECT().Key().Return("foo").Maybe()
src2 := handler.NewMockHandler(t)
src2.EXPECT().Key().Return("bar").Maybe()
s := New(WithHandlers(src1, src2))
assert.NotNil(t, s)
assert.Contains(t, s.handlers, src1)
assert.Contains(t, s.handlers, src2)
}
func TestRun(t *testing.T) {
t.Parallel()
errValidationFailed := errors.New("some validation failed error")
tests := []struct {
name string
opts []Opt
wantErr error
}{
{
name: "starts and shuts down",
opts: []Opt{
WithListenAddr(":0"),
},
wantErr: nil,
},
{
name: "returns error if a handler fails to validate",
opts: []Opt{
WithListenAddr(":0"),
WithHandlers(&handlerThatFailsToValidate{err: errValidationFailed}),
},
wantErr: errValidationFailed,
},
{
name: "returns error if a transport fails to validate",
opts: []Opt{
WithListenAddr(":0"),
WithTransports(&transportThatFailsToValidate{
err: errValidationFailed,
}),
},
wantErr: errValidationFailed,
},
{
name: "returns error if a user store fails to validate",
opts: []Opt{
WithListenAddr(":0"),
WithUserStore(&userStoreThatFailsToValidate{
err: errValidationFailed,
}),
},
wantErr: errValidationFailed,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
s := New(tt.opts...)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(500 * time.Millisecond)
cancel()
}()
err := s.Run(ctx)
if tt.wantErr == nil {
assert.NoError(t, err)
} else {
assert.ErrorIs(t, err, tt.wantErr)
}
})
}
}
type handlerThatFailsToValidate struct {
err error
}
var _ handler.Handler = handlerThatFailsToValidate{}
var _ common.Validator = handlerThatFailsToValidate{}
func (s handlerThatFailsToValidate) Validate(_ context.Context) error {
return s.err
}
func (s handlerThatFailsToValidate) Key() string {
return "some-handler"
}
func (s handlerThatFailsToValidate) Process(_ *http.Request) ([]common.Notification, error) {
panic("not implemented")
}
func (s handlerThatFailsToValidate) EventTypes() []event.TypeDescriptor {
panic("not implemented")
}
type transportThatFailsToValidate struct {
err error
}
func (t transportThatFailsToValidate) Push(_ context.Context, _ common.Notification) error {
panic("not called in our tests")
}
func (t transportThatFailsToValidate) Key() common.TransportKey {
return "test"
}
func (t transportThatFailsToValidate) Validate(_ context.Context) error {
return t.err
}
type userStoreThatFailsToValidate struct {
err error
}
func (s userStoreThatFailsToValidate) Get(_ context.Context, _ string) (*user.User, error) {
panic("not called in our tests")
}
func (s userStoreThatFailsToValidate) GetByIdentifier(_ context.Context, identifier identifier.Identifier) (*user.User, error) {
panic("not called in our tests")
}
func (s userStoreThatFailsToValidate) Find(_ context.Context, _ identifier.Set) (*user.User, error) {
panic("not called in our tests")
}
func (s userStoreThatFailsToValidate) SetPreferences(_ context.Context, _ string, _ user.Preferences) error {
panic("not called in our tests")
}
func (s userStoreThatFailsToValidate) Validate(_ context.Context) error {
return s.err
}