-
Notifications
You must be signed in to change notification settings - Fork 7
/
publication_test.go
314 lines (246 loc) · 7.59 KB
/
publication_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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bahamut
import (
"testing"
"time"
// nolint:revive // Allow dot imports for readability in tests
. "github.com/smartystreets/goconvey/convey"
"go.aporeto.io/elemental"
testmodel "go.aporeto.io/elemental/test/model"
)
func TestPublication_NewPublication(t *testing.T) {
Convey("Given I create a new Publication", t, func() {
publication := NewPublication("topic")
Convey("Then the publication should be correctly initialized", func() {
So(publication.Topic, ShouldEqual, "topic")
})
})
}
func TestPublication_EncodeDecode(t *testing.T) {
Convey("Given I create a new Publication", t, func() {
tracer := &mockTracer{}
publication := NewPublication("topic")
publication.StartTracing(tracer, "test")
Convey("When I encode some object using JSON encoding", func() {
list := testmodel.NewList()
list.Name = "l1"
list.ID = "xxx"
err := publication.EncodeWithEncoding(list, elemental.EncodingTypeJSON)
Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then the publication contains the correct data", func() {
d, _ := elemental.Encode(elemental.EncodingTypeJSON, list)
So(publication.Data, ShouldResemble, d)
})
Convey("When I decode the object", func() {
l2 := testmodel.NewList()
err := publication.Decode(l2)
Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then l2 should resemble to l1", func() {
So(l2, ShouldResemble, list)
})
})
})
Convey("When I encode some object using MSGPACK encoding", func() {
list := testmodel.NewList()
list.Name = "l1"
list.ID = "xxx"
err := publication.EncodeWithEncoding(list, elemental.EncodingTypeMSGPACK)
Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then the publication contains the correct data", func() {
d, _ := elemental.Encode(elemental.EncodingTypeMSGPACK, list)
So(publication.Data, ShouldResemble, d)
})
Convey("When I decode the object", func() {
l2 := testmodel.NewList()
err := publication.Decode(l2)
Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then l2 should resemble to l1", func() {
So(l2, ShouldResemble, list)
})
})
})
Convey("When I encode some unencodable object", func() {
list := testmodel.NewUnmarshalableList()
list.Name = "l1"
list.ID = "xxx"
err := publication.EncodeWithEncoding(list, elemental.EncodingTypeJSON)
Convey("Then err should not be nil", func() {
So(err, ShouldNotBeNil)
})
Convey("Then the publication contains the correct data", func() {
So(string(publication.Data), ShouldEqual, "")
})
Convey("When I decode the non existing object", func() {
l2 := testmodel.NewList()
err := publication.Decode(l2)
Convey("Then err should not be nil", func() {
So(err, ShouldNotBeNil)
})
})
})
})
}
func TestPublicationTracing(t *testing.T) {
Convey("Given I have no tracer", t, func() {
publication := NewPublication("topic")
Convey("When I call StartTracing", func() {
publication.StartTracing(nil, "test")
Convey("Then the span should be correct", func() {
So(publication.Span(), ShouldBeNil)
})
})
Convey("When I call StartTracingFromSpan", func() {
span := &mockSpan{}
err := publication.StartTracingFromSpan(span, "test")
Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then the span should be correct", func() {
So(publication.Span(), ShouldBeNil)
})
})
})
Convey("Given I have a tracer", t, func() {
tracer := &mockTracer{}
publication := NewPublication("topic")
Convey("When I call StartTracing", func() {
publication.StartTracing(tracer, "test")
Convey("Then the span should be correct", func() {
So(publication.Span(), ShouldNotBeNil)
})
})
Convey("When I call StartTracingFromSpan", func() {
span := newMockSpan(tracer)
err := publication.StartTracingFromSpan(span, "test")
Convey("Then err should be nil", func() {
So(err, ShouldBeNil)
})
Convey("Then the span should be correct", func() {
So(publication.Span(), ShouldNotBeNil)
})
})
})
}
func TestPublication_Duplicate(t *testing.T) {
Convey("Given I have a publication", t, func() {
pub := NewPublication("topic")
pub.Data = []byte("data")
pub.Partition = 12
pub.TrackingName = "TrackingName"
Convey("When I call duplicate", func() {
dup := pub.Duplicate()
Convey("Then the copy should be correct", func() {
So(dup, ShouldNotEqual, pub)
So(dup.Data, ShouldResemble, pub.Data)
So(dup.Partition, ShouldEqual, pub.Partition)
So(dup.TrackingName, ShouldEqual, pub.TrackingName)
So(dup.Topic, ShouldEqual, pub.Topic)
So(dup.Encoding, ShouldEqual, pub.Encoding)
})
})
})
}
func TestReply(t *testing.T) {
threshold := 100 * time.Millisecond
testCases := []struct {
setup func() *Publication
response *Publication
description string
expectingReply bool
shouldError bool
}{
{
description: "should send publication response to reply channel",
setup: func() *Publication {
return &Publication{
replyCh: make(chan *Publication),
}
},
response: NewPublication("test topic"),
expectingReply: true,
shouldError: false,
},
{
description: "should return an error if no reply channel is configured for publication",
setup: func() *Publication {
return NewPublication("")
},
response: NewPublication("test topic"),
shouldError: true,
},
{
description: "should return an error if passed in a nil publication",
setup: func() *Publication {
return &Publication{
replyCh: make(chan *Publication),
}
},
response: nil,
shouldError: true,
},
{
description: "should return an error if publication has already been responded to",
setup: func() *Publication {
return &Publication{
replyCh: make(chan *Publication),
replied: true,
}
},
response: NewPublication("test topic"),
shouldError: true,
},
{
description: "should return an error if caller took too long to respond to publication",
setup: func() *Publication {
return &Publication{
replyCh: make(chan *Publication),
timedOut: true,
}
},
response: NewPublication("test topic"),
shouldError: true,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
done := make(chan struct{})
pub := tc.setup()
go func() {
if pub.replyCh != nil {
select {
case response := <-pub.replyCh:
if !tc.expectingReply {
t.Errorf("was NOT expecting to get a response in reply channel, but got: %+v", response)
}
case <-time.After(threshold):
if tc.expectingReply {
t.Errorf("expected to get a response in reply channel, but got nothing.")
}
}
}
close(done)
}()
if err := pub.Reply(tc.response); !tc.shouldError && err != nil {
t.Errorf("error returned when none was expected - error: %+v", err)
}
<-done
})
}
}