-
Notifications
You must be signed in to change notification settings - Fork 87
/
message_test.go
92 lines (74 loc) · 2.21 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
package analytics
import (
"reflect"
"testing"
)
func TestMessageIdDefault(t *testing.T) {
if id := makeMessageId("", "42"); id != "42" {
t.Error("invalid default message id:", id)
}
}
func TestMessageIdNonDefault(t *testing.T) {
if id := makeMessageId("A", "42"); id != "A" {
t.Error("invalid non-default message id:", id)
}
}
func TestMessageQueuePushMaxBatchSize(t *testing.T) {
m0, _ := makeMessage(Track{
UserId: "1",
Event: "A",
}, maxMessageBytes)
m1, _ := makeMessage(Track{
UserId: "2",
Event: "A",
}, maxMessageBytes)
q := messageQueue{
maxBatchSize: 2,
maxBatchBytes: maxBatchBytes,
}
if msgs := q.push(m0); msgs != nil {
t.Error("unexpected message batch returned after pushing only one message")
}
if msgs := q.push(m1); !reflect.DeepEqual(msgs, []message{m0, m1}) {
t.Error("invalid message batch returned after pushing two messages:", msgs)
}
}
func TestMessageQueuePushMaxBatchBytes(t *testing.T) {
m0, _ := makeMessage(Track{
UserId: "1",
Event: "A",
}, maxMessageBytes)
m1, _ := makeMessage(Track{
UserId: "2",
Event: "A",
}, maxMessageBytes)
q := messageQueue{
maxBatchSize: 100,
maxBatchBytes: len(m0.json) + 1,
}
if msgs := q.push(m0); msgs != nil {
t.Error("unexpected message batch returned after pushing only one message")
}
if msgs := q.push(m1); !reflect.DeepEqual(msgs, []message{m0}) {
t.Error("invalid message batch returned after pushing two messages:", msgs)
}
if !reflect.DeepEqual(q.pending, []message{m1}) {
t.Error("invalid state of the message queue after pushing two messages:", q.pending)
}
}
func TestMakeMessage(t *testing.T) {
track := Track{UserId: "1"}
if msg, err := makeMessage(track, maxMessageBytes); err != nil {
t.Error("failed to make message from track message:", err)
} else if !reflect.DeepEqual(msg, message{
msg: track,
json: []byte(`{"userId":"1","event":"","timestamp":"0001-01-01T00:00:00Z"}`),
}) {
t.Error("invalid message generated from track message:", msg.msg, string(msg.json))
}
}
func TestMakeMessageTooBig(t *testing.T) {
if _, err := makeMessage(Track{UserId: "1"}, 1); err != ErrMessageTooBig {
t.Error("invalid error returned when creating a message bigger than the limit:", err)
}
}