-
Notifications
You must be signed in to change notification settings - Fork 17
/
message_test.go
486 lines (451 loc) · 12.5 KB
/
message_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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package uof
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMessageParseRoutingKeys(t *testing.T) {
data := []struct {
key string
rm Message
}{
{
key: "hi.-.live.bet_cancel.21.sr:match.13073610.-",
rm: Message{
Header: Header{
Type: MessageTypeBetCancel,
Scope: MessageScopeLive,
Priority: MessagePriorityHigh,
SportID: 21,
EventURN: "sr:match:13073610",
EventID: 13073610,
},
},
},
{
key: "hi.pre.-.odds_change.1.sr:match.1234.-",
rm: Message{
Header: Header{
Type: MessageTypeOddsChange,
Scope: MessageScopePrematch,
Priority: MessagePriorityHigh,
SportID: 1,
EventURN: "sr:match:1234",
EventID: 1234,
},
},
},
{
key: "hi.virt.-.odds_change.7.vf:match.12345.-",
rm: Message{
Header: Header{
Type: MessageTypeOddsChange,
Scope: MessageScopeVirtuals,
Priority: MessagePriorityHigh,
SportID: 7,
EventURN: "vf:match:12345",
EventID: -3160336,
},
},
},
{
key: "-.-.-.alive.-.-.-.-",
rm: Message{
Header: Header{
Type: MessageTypeAlive,
Scope: MessageScopeSystem,
Priority: MessagePriorityLow,
},
},
},
{
key: "-.-.-.snapshot_complete.-.-.-",
rm: Message{
Header: Header{
Type: MessageTypeSnapshotComplete,
Scope: MessageScopeSystem,
Priority: MessagePriorityLow,
},
},
},
{
key: "hi.-.live.odds_change.4.sr:match.11784628",
rm: Message{
Header: Header{
Type: MessageTypeOddsChange,
Scope: MessageScopeLive,
Priority: MessagePriorityHigh,
SportID: 4,
EventURN: "sr:match:11784628",
EventID: 11784628,
},
},
},
{
key: "lo.pre.live.bet_settlement.8.sr:match.12.-",
rm: Message{
Header: Header{
Type: MessageTypeBetSettlement,
Scope: MessageScopePrematchAndLive,
Priority: MessagePriorityLow,
SportID: 8,
EventURN: "sr:match:12",
EventID: 12,
},
},
},
}
for _, d := range data {
rm, err := NewQueueMessage(d.key, nil)
assert.Nil(t, err)
assert.Equal(t, d.rm.Scope, rm.Scope)
assert.Equal(t, d.rm.Type, rm.Type)
assert.Equal(t, d.rm.Priority, rm.Priority)
assert.Equal(t, d.rm.SportID, rm.SportID)
assert.Equal(t, d.rm.EventURN, rm.EventURN)
assert.Equal(t, d.rm.EventID, rm.EventID)
assert.Equal(t, LangNone, rm.Lang)
}
_, err := NewQueueMessage("...", nil)
assert.Error(t, err)
_, err = NewQueueMessage("hi.-.live.unknown.4.sr:match.11784628", nil)
assert.Error(t, err)
_, err = NewQueueMessage("hi.-.live.odds_change.4.sr:match.pero", nil)
assert.Error(t, err)
}
func TestMessageTypeParse(t *testing.T) {
var y MessageType
y.Parse("alive")
assert.Equal(t, MessageTypeAlive, y)
}
func TestMessageWithRawMarshal(t *testing.T) {
m := &Message{
Header: Header{Type: MessageTypeAlive, Scope: MessageScopeSystem, Priority: MessagePriorityLow, ReceivedAt: 12345, Producer: ProducerPrematch, Timestamp: 12340},
Raw: []byte(`<alive product="3" timestamp="12340" subscribed="1"/>`),
Body: Body{
Alive: &Alive{
Producer: ProducerPrematch,
Timestamp: 12340,
Subscribed: 1,
},
},
}
buf := m.Marshal()
expected := []byte(`{"type":64,"scope":4,"receivedAt":12345,"producer":3,"timestamp":12340}
<alive product="3" timestamp="12340" subscribed="1"/>`)
assert.Equal(t, expected, buf)
m2 := &Message{}
err := m2.Unmarshal(buf)
assert.NoError(t, err)
assert.Equal(t, m, m2)
}
func TestMessageWithoutRaw(t *testing.T) {
m := &Message{
Header: Header{Type: MessageTypeConnection, Scope: MessageScopeSystem, Priority: MessagePriorityLow, ReceivedAt: 12345},
Body: Body{
Connection: &Connection{
Status: ConnectionStatusDown,
},
},
}
buf := m.Marshal()
expected := []byte(`{"type":66,"scope":4,"receivedAt":12345,"connection":{"status":1}}`)
assert.Equal(t, expected, buf)
m2 := &Message{}
err := m2.Unmarshal(buf)
assert.NoError(t, err)
assert.Equal(t, m, m2)
}
func TestUID(t *testing.T) {
data := []struct {
m Message
uid int
}{
{
Message{
Header: Header{
Type: MessageTypePlayer,
Lang: LangHR,
},
Body: Body{
Player: &Player{ID: 0x12345},
},
},
0x1234509,
},
{
Message{
Header: Header{
Type: MessageTypePlayer,
Lang: LangTR,
},
Body: Body{
Player: &Player{ID: 0x007fffffffffffff},
},
},
0x7fffffffffffff29,
},
{
Message{
Header: Header{
Type: MessageTypeFixture,
Lang: LangIT,
},
Body: Body{
Fixture: &Fixture{ID: -0x123},
},
},
-0x1232c,
},
{
Message{
Header: Header{
Type: MessageTypeAlive,
},
},
0,
},
}
for i, d := range data {
assert.Equal(t, d.uid, d.m.UID(),
"case: %d, actual %x", i, d.m.UID())
}
}
func TestUIDWithLang(t *testing.T) {
data := []struct {
id int
lang Lang
uid int
}{
{0x01, LangHR, 0x0109},
{0x007fffffffffffff, LangTR, 0x7fffffffffffff29},
{-0x123, LangIT, -0x1232c},
}
for _, d := range data {
assert.Equal(t, d.uid, UIDWithLang(d.id, d.lang))
}
}
func TestNewMessage(t *testing.T) {
m := NewSimpleConnnectionMessage(ConnectionStatusUp)
assert.True(t, m.Is(MessageTypeConnection))
m = NewPlayerMessage(LangEN, nil, 0)
assert.True(t, m.Is(MessageTypePlayer))
m = NewMarketsMessage(LangEN, nil, 0)
assert.True(t, m.Is(MessageTypeMarkets))
m = NewProducersChangeMessage(nil)
assert.True(t, m.Is(MessageTypeProducersChange))
m = NewFixtureMessage(LangEN, Fixture{}, 0)
assert.True(t, m.Is(MessageTypeFixture))
m, err := NewFixtureMessageFromBuf(LangEN, nil, 0)
assert.NoError(t, err)
assert.True(t, m.Is(MessageTypeFixture))
m.NewFixtureMessage(LangEN, Fixture{})
assert.True(t, m.Is(MessageTypeFixture))
}
func TestNewMessageFromBufFail(t *testing.T) {
failing := []byte{}
expectErr := fmt.Errorf("NOTICE uof error op: message.unpack, inner: EOF")
m, err := NewFixtureMessageFromBuf(LangEN, failing, 0)
assert.Nil(t, m)
assert.Error(t, err)
assert.EqualError(t, err, expectErr.Error())
}
func TestUnpackFail(t *testing.T) {
// void_reason below whould be int value
buf := []byte(`<bet_cancel end_time="1564598513000" event_id="sr:match:18941600" product="1" start_time="1564597838000" timestamp="1564602448841">
<market name="1st half - 1st goal" id="62" void_reason="int"/>
</bet_cancel>`)
_, err := NewQueueMessage("hi.pre.-.bet_cancel.1.sr:match.1234.-", buf)
assert.Error(t, err)
assert.Equal(t, `NOTICE uof error op: message.unpack, inner: strconv.ParseInt: parsing "int": invalid syntax`, err.Error())
// height should be int
buf = []byte(`
<player_profile>
<player height="int" />
</player_profile>
`)
_, err = NewAPIMessage(LangEN, MessageTypePlayer, buf)
assert.Error(t, err)
assert.Equal(t, `NOTICE uof error op: message.unpack, inner: strconv.ParseInt: parsing "int": invalid syntax`, err.Error())
var m Message
err = m.Unmarshal(nil)
assert.Error(t, err)
m.Raw = []byte{}
m.Type = -1
err = m.unpack()
assert.Error(t, err)
assert.Equal(t, `NOTICE uof error op: message.unpack, inner: unknown message type -1`, err.Error())
}
func TestEnrichHeaderAfterUnpack(t *testing.T) {
// check if Producer and Timestamp get copied to Header after Unmarshal
// all messages have the same producer and the same timestamp in ms (1234578910111)
buf := []struct {
key string
raw []byte
}{
// snapshot_complete
struct {
key string
raw []byte
}{
key: "-.-.-.snapshot_complete.-.-.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<snapshot_complete request_id="1234" timestamp="1234578910111" product="1"/>`),
},
// alive
struct {
key string
raw []byte
}{
key: "-.-.-.alive.-.-.-.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<alive product="1" timestamp="1234578910111" subscribed="1"/>`),
},
// fixture_change
struct {
key string
raw []byte
}{
key: "hi.pre.live.fixture_change.1.sr:match.18001015.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<fixture_change start_time="1557255600000" product="1" event_id="sr:match:18001015" timestamp="1234578910111"/>`),
},
// bet_stop
struct {
key string
raw []byte
}{
key: "hi.-.live.bet_stop.1.sr:match.18001015.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bet_stop groups="all" market_status="0" product="1" event_id="sr:match:18001015" timestamp="1234578910111"/>`),
},
// odds_change
struct {
key string
raw []byte
}{
key: "hi.-.live.odds_change.1.sr:match.18001015.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<odds_change product="1" event_id="sr:match:18001015" timestamp="1234578910111">
<sport_event_status status="0" match_status="0"/>
<odds>
<market status="1" id="1">
<outcome id="1" odds="2.08" probabilities="0.450633" active="1"/>
<outcome id="2" odds="3.31" probabilities="0.277172" active="1"/>
<outcome id="3" odds="3.37" probabilities="0.272194" active="1"/>
</market>
</odds>
</odds_change>`),
},
// bet_settlement
struct {
key string
raw []byte
}{
key: "hi.-.live.bet_settlement.1.sr:match.18001015.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bet_settlement certainty="1" product="1" event_id="sr:match:13369905" timestamp="1234578910111">
<outcomes>
<market id="6">
<outcome id="4" result="1"/>
<outcome id="5" result="0"/>
</market>
</outcomes>
</bet_settlement>`),
},
// rollback_bet_settlement
struct {
key string
raw []byte
}{
key: "hi.-.live.rollback_bet_settlement.1.sr:match.18001015.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rollback_bet_settlement product="1" event_id="sr:match:18001015" timestamp="1234578910111">
<market id="38" specifiers="goalnr=1|type=live"/>
</rollback_bet_settlement>`),
},
// bet_cancel
struct {
key string
raw []byte
}{
key: "hi.-.live.bet_cancel.1.sr:match.18001015.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bet_cancel end_time="1564598513000" event_id="sr:match:18001015" product="1" start_time="1564597838000" timestamp="1234578910111">
<market name="1st half - 1st goal" id="62" specifier="goalnr=1" void_reason="12"/>
</bet_cancel>`),
},
// rollback_bet_cancel
struct {
key string
raw []byte
}{
key: "hi.-.live.rollback_bet_cancel.1.sr:match.18001015.-",
raw: []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rollback_bet_cancel event_id="sr:match:4444" product="1" timestamp="1234578910111">
<market id="48" specifiers="score=41.5"/>
</rollback_bet_cancel>`),
},
}
for _, m := range buf {
qm, err := NewQueueMessage(m.key, m.raw)
assert.Nil(t, err)
if err != nil {
t.Logf(err.Error())
}
assert.NotNil(t, qm)
assert.Equal(t, ProducerLiveOdds, qm.Producer)
assert.Equal(t, 1234578910111, qm.Timestamp)
assert.Equal(t, getTsFromMsg(qm), qm.Timestamp)
assert.Equal(t, getProducerFromMsg(qm), qm.Producer)
t.Logf("prod: %s ts: %d msg type: %s \n", qm.Producer, qm.Timestamp, qm.Type.String())
}
}
// get timestamp from embedded message type
func getTsFromMsg(msg *Message) int {
switch msg.Type {
case MessageTypeOddsChange:
return msg.OddsChange.Timestamp
case MessageTypeFixtureChange:
return msg.FixtureChange.Timestamp
case MessageTypeBetStop:
return msg.BetStop.Timestamp
case MessageTypeBetSettlement:
return msg.BetSettlement.Timestamp
case MessageTypeRollbackBetSettlement:
return msg.RollbackBetSettlement.Timestamp
case MessageTypeBetCancel:
return msg.BetCancel.Timestamp
case MessageTypeRollbackBetCancel:
return msg.RollbackBetCancel.Timestamp
case MessageTypeSnapshotComplete:
return msg.SnapshotComplete.Timestamp
case MessageTypeAlive:
return msg.Alive.Timestamp
}
return 0
}
// get producer from embedded message type
func getProducerFromMsg(msg *Message) Producer {
switch msg.Type {
case MessageTypeOddsChange:
return msg.OddsChange.Producer
case MessageTypeFixtureChange:
return msg.FixtureChange.Producer
case MessageTypeBetStop:
return msg.BetStop.Producer
case MessageTypeBetSettlement:
return msg.BetSettlement.Producer
case MessageTypeRollbackBetSettlement:
return msg.RollbackBetSettlement.Producer
case MessageTypeBetCancel:
return msg.BetCancel.Producer
case MessageTypeRollbackBetCancel:
return msg.RollbackBetCancel.Producer
case MessageTypeSnapshotComplete:
return msg.SnapshotComplete.Producer
case MessageTypeAlive:
return msg.Alive.Producer
}
return ProducerUnknown
}