-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendermap_test.go
214 lines (192 loc) · 4.75 KB
/
sendermap_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrpnng
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/wrp-go/v3"
"github.com/xmidt-org/wrpnng/internal/sender"
)
type mockSender struct {
processErr error
processCount int
dialErr error
}
func (m *mockSender) ProcessWRP(_ context.Context, _ wrp.Message) error {
m.processCount++
return m.processErr
}
func (m *mockSender) Close() error {
return nil
}
func (m *mockSender) Dial() error {
return m.dialErr
}
func TestSenderMap_ProcessWRP(t *testing.T) {
randomErr := errors.New("random error")
tests := []struct {
name string
senders map[string]*mockSender
msg wrp.Message
expect map[string]*mockSender
expectedErr error
}{
{
name: "ServiceAliveMessageType",
senders: map[string]*mockSender{
"service_1": {},
"service_2": {},
},
msg: wrp.Message{Type: wrp.ServiceAliveMessageType},
expect: map[string]*mockSender{
"service_1": {processCount: 1},
"service_2": {processCount: 1},
},
}, {
name: "Valid Destination",
senders: map[string]*mockSender{
"service_1": {},
"service_2": {},
},
msg: wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Destination: "mac:112233445566/service_1/ignored",
},
expect: map[string]*mockSender{
"service_1": {processCount: 1},
"service_2": {},
},
}, {
name: "Invalid Destination",
senders: map[string]*mockSender{
"service_1": {},
},
msg: wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Destination: "mac:112233445566/invalid/ignored",
},
expectedErr: wrp.ErrNotHandled,
expect: map[string]*mockSender{
"service_1": {},
},
}, {
name: "Sender Error",
senders: map[string]*mockSender{
"service_1": {processErr: randomErr},
},
msg: wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Destination: "mac:112233445566/service_1/ignored",
},
expectedErr: randomErr,
}, {
name: "Invalid locator",
msg: wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Destination: "service_1/ignored",
},
expectedErr: wrp.ErrorInvalidLocator,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := &senderMap{
senders: make(map[string]limitedSender),
}
for k, v := range tt.senders {
sm.senders[k] = v
}
err := sm.ProcessWRP(context.Background(), tt.msg)
if tt.expectedErr != nil {
assert.ErrorIs(t, err, tt.expectedErr)
} else {
assert.NoError(t, err)
}
if tt.expect != nil {
for k, v := range tt.expect {
require.NotNil(t, sm.senders[k])
assert.Equal(t, v.processCount, sm.senders[k].(*mockSender).processCount)
}
}
})
}
}
func TestSenderMap_upsert(t *testing.T) {
factory := func(opts ...sender.Option) (limitedSender, error) {
return &mockSender{}, nil
}
tests := []struct {
name string
initialSenders map[string]limitedSender
upsertName string
factory limitedSenderFactory
opts []sender.Option
expectError bool
}{
{
name: "Upsert new sender",
upsertName: "service_1",
}, {
name: "Upsert existing sender",
initialSenders: map[string]limitedSender{
"service_1": new(mockSender),
},
upsertName: "service_1",
}, {
name: "Cause the factory to fail",
upsertName: "service_1",
factory: func(opts ...sender.Option) (limitedSender, error) {
return nil, errors.New("factory error")
},
expectError: true,
}, {
name: "using a faulty dialer",
upsertName: "service_1",
factory: func(opts ...sender.Option) (limitedSender, error) {
return &mockSender{
dialErr: errors.New("dial error"),
}, nil
},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := &senderMap{
senders: tt.initialSenders,
}
if tt.factory == nil {
tt.factory = factory
}
err := sm.upsert(tt.upsertName, tt.opts, tt.factory)
if tt.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, sm.senders[tt.upsertName])
}
})
}
}
func TestSenderMap_Remove(t *testing.T) {
sm := &senderMap{
senders: make(map[string]limitedSender),
}
sm.senders["service1"] = &mockSender{}
err := sm.Remove("service1")
assert.NoError(t, err)
assert.Nil(t, sm.senders["service1"])
}
func TestSenderMap_Close(t *testing.T) {
sm := &senderMap{
senders: make(map[string]limitedSender),
}
sm.senders["service1"] = &mockSender{}
sm.senders["service2"] = &mockSender{}
err := sm.Close()
assert.NoError(t, err)
assert.Nil(t, sm.senders)
}