-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_select_test.go
275 lines (256 loc) · 7.77 KB
/
priority_select_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package priority_channels_test
import (
"context"
"testing"
"github.com/dimag-jfrog/priority-channels"
"github.com/dimag-jfrog/priority-channels/channels"
)
func TestSelect(t *testing.T) {
urgentC := make(chan string, 1)
normalC := make(chan string, 1)
lowPriorityC := make(chan string, 1)
urgentC <- "Urgent message"
normalC <- "Normal message"
lowPriorityC <- "Low priority message"
channelsWithPriority := []channels.ChannelWithPriority[string]{
channels.NewChannelWithPriority(
"Low Priority Messages",
lowPriorityC,
1),
channels.NewChannelWithPriority(
"Urgent Messages",
urgentC,
10),
channels.NewChannelWithPriority(
"Normal Messages",
normalC,
5),
}
ctx := context.Background()
msg, channelName, status, err := priority_channels.Select(ctx, channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if channelName != "Urgent Messages" {
t.Errorf("Expected channel name %s, but got %s", "Urgent Messages", channelName)
}
if status != priority_channels.ReceiveSuccess {
t.Errorf("Expected ok to be true, but got false")
}
if msg != "Urgent message" {
t.Errorf("Expected 'Urgent message' message, but got %s", msg)
}
msg, channelName, status, err = priority_channels.Select(ctx, channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if channelName != "Normal Messages" {
t.Errorf("Expected channel name %s, but got %s", "Normal Messages", channelName)
}
if status != priority_channels.ReceiveSuccess {
t.Errorf("Expected ok to be true, but got false")
}
if msg != "Normal message" {
t.Errorf("Expected 'Normal message' message, but got %s", msg)
}
msg, channelName, status, err = priority_channels.Select(ctx, channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if channelName != "Low Priority Messages" {
t.Errorf("Expected channel name %s, but got %s", "Low Priority Messages", channelName)
}
if status != priority_channels.ReceiveSuccess {
t.Errorf("Expected ok to be true, but got false")
}
if msg != "Low priority message" {
t.Errorf("Expected 'Low priority message' message, but got %s", msg)
}
}
func TestSelectWithDefaultUseCase(t *testing.T) {
urgentC := make(chan string, 1)
normalC := make(chan string, 1)
lowPriorityC := make(chan string, 1)
channelsWithPriority := []channels.ChannelWithPriority[string]{
channels.NewChannelWithPriority(
"Low Priority Messages",
lowPriorityC,
1),
channels.NewChannelWithPriority(
"Urgent Messages",
urgentC,
10),
channels.NewChannelWithPriority(
"Normal Messages",
normalC,
5),
}
msg, channelName, status, err := priority_channels.SelectWithDefaultCase(channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if status != priority_channels.ReceiveDefaultCase {
t.Errorf("Expected status default-select-case, but got %d", status)
}
if channelName != "" {
t.Errorf("Expected empty channel name, but got %s", channelName)
}
if msg != "" {
t.Errorf("Expected empty message, but got %s", msg)
}
urgentC <- "Urgent message"
normalC <- "Normal message"
lowPriorityC <- "Low priority message"
msg, channelName, status, err = priority_channels.SelectWithDefaultCase(channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if channelName != "Urgent Messages" {
t.Errorf("Expected channel name %s, but got %s", "Urgent Messages", channelName)
}
if status != priority_channels.ReceiveSuccess {
t.Errorf("Expected ok to be true, but got false")
}
if msg != "Urgent message" {
t.Errorf("Expected 'Urgent message' message, but got %s", msg)
}
msg, channelName, status, err = priority_channels.SelectWithDefaultCase(channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if channelName != "Normal Messages" {
t.Errorf("Expected channel name %s, but got %s", "Normal Messages", channelName)
}
if status != priority_channels.ReceiveSuccess {
t.Errorf("Expected ok to be true, but got false")
}
if msg != "Normal message" {
t.Errorf("Expected 'Normal message' message, but got %s", msg)
}
msg, channelName, status, err = priority_channels.SelectWithDefaultCase(channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if channelName != "Low Priority Messages" {
t.Errorf("Expected channel name %s, but got %s", "Low Priority Messages", channelName)
}
if status != priority_channels.ReceiveSuccess {
t.Errorf("Expected ok to be true, but got false")
}
if msg != "Low priority message" {
t.Errorf("Expected 'Low priority message' message, but got %s", msg)
}
msg, channelName, status, err = priority_channels.SelectWithDefaultCase(channelsWithPriority)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if status != priority_channels.ReceiveDefaultCase {
t.Errorf("Expected status default-select-case, but got %d", status)
}
if channelName != "" {
t.Errorf("Expected empty channel name, but got %s", channelName)
}
if msg != "" {
t.Errorf("Expected empty message, but got %s", msg)
}
}
func TestPrioritySelectValidation(t *testing.T) {
var testCases = []struct {
Name string
ChannelsWithPriorities []channels.ChannelWithPriority[string]
ExpectedErrorMessage string
}{
{
Name: "No channels",
ChannelsWithPriorities: []channels.ChannelWithPriority[string]{},
ExpectedErrorMessage: priority_channels.ErrNoChannels.Error(),
},
{
Name: "Empty channel name",
ChannelsWithPriorities: []channels.ChannelWithPriority[string]{
channels.NewChannelWithPriority(
"Urgent Messages",
make(chan string),
10),
channels.NewChannelWithPriority(
"Normal Messages",
make(chan string),
5),
channels.NewChannelWithPriority(
"",
make(chan string),
1),
},
ExpectedErrorMessage: priority_channels.ErrEmptyChannelName.Error(),
},
{
Name: "Negative priority value",
ChannelsWithPriorities: []channels.ChannelWithPriority[string]{
channels.NewChannelWithPriority(
"Urgent Messages",
make(chan string),
10),
channels.NewChannelWithPriority(
"Normal Messages",
make(chan string),
-5),
channels.NewChannelWithPriority(
"Low Priority Messages",
make(chan string),
1),
},
ExpectedErrorMessage: "channel 'Normal Messages': priority cannot be negative",
},
{
Name: "Duplicate channel name",
ChannelsWithPriorities: []channels.ChannelWithPriority[string]{
channels.NewChannelWithPriority(
"Urgent Messages",
make(chan string),
10),
channels.NewChannelWithPriority(
"Normal Messages",
make(chan string),
5),
channels.NewChannelWithPriority(
"Urgent Messages",
make(chan string),
1),
},
ExpectedErrorMessage: "channel name 'Urgent Messages' is used more than once",
},
}
for _, tc := range testCases {
t.Run(tc.Name+"- Select", func(t *testing.T) {
ctx := context.Background()
_, _, _, err := priority_channels.Select(ctx, tc.ChannelsWithPriorities)
if tc.ExpectedErrorMessage == "" {
if err != nil {
t.Fatalf("Unexpected validation error: %v", err)
}
return
}
if err == nil {
t.Fatalf("Expected validation error")
}
if err.Error() != tc.ExpectedErrorMessage {
t.Errorf("Expected error %s, but got: %v", tc.ExpectedErrorMessage, err)
}
})
t.Run(tc.Name+"- SelectWithDefaultCase", func(t *testing.T) {
_, _, _, err := priority_channels.SelectWithDefaultCase(tc.ChannelsWithPriorities)
if tc.ExpectedErrorMessage == "" {
if err != nil {
t.Fatalf("Unexpected validation error: %v", err)
}
return
}
if err == nil {
t.Fatalf("Expected validation error")
}
if err.Error() != tc.ExpectedErrorMessage {
t.Errorf("Expected error %s, but got: %v", tc.ExpectedErrorMessage, err)
}
})
}
}