-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkakfa_test.go
173 lines (134 loc) · 4.41 KB
/
kakfa_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
package main
import (
"testing"
"github.com/Shopify/sarama/mocks"
"github.com/Shopify/sarama"
"time"
"encoding/json"
"github.com/stretchr/testify/assert"
)
func TestWriteSingleMessage(t *testing.T) {
producer := NewProducer(t)
expectedTime := time.Now()
expectedSource := "containerABC"
expectedLine := "I am a log message"
msg := newMessage(expectedTime, expectedSource, expectedLine)
producer.ExpectInputAndSucceed()
WriteMessage("topic1", msg, expectedSource, KEY_BY_TIMESTAMP, producer)
writtenMsg := <-producer.Successes()
assertLineMatch(t, expectedLine, writtenMsg)
}
func TestWriteMultipleMessagesToSameTopic(t *testing.T) {
producer := NewProducer(t)
msg1 := newMessage(time.Now(), "1", "a")
msg2 := newMessage(time.Now(), "2", "b")
msg3 := newMessage(time.Now(), "3", "c")
// Need to call this three times to expect three messages
producer.ExpectInputAndSucceed()
producer.ExpectInputAndSucceed()
producer.ExpectInputAndSucceed()
WriteMessage("topic1", msg1, msg1.Source, KEY_BY_TIMESTAMP, producer)
WriteMessage("topic1", msg2, msg2.Source, KEY_BY_TIMESTAMP, producer)
WriteMessage("topic1", msg3, msg3.Source, KEY_BY_TIMESTAMP, producer)
out1 := <-producer.Successes()
out2 := <-producer.Successes()
out3 := <-producer.Successes()
assert.NotNil(t, out1)
assertLineMatch(t,"a", out1)
assertTopic(t, "topic1", out1)
assert.NotNil(t, out2)
assertLineMatch(t,"b", out2)
assertTopic(t, "topic1", out1)
assert.NotNil(t, out3)
assertLineMatch(t,"c", out3)
assertTopic(t, "topic1", out1)
}
func TestWriteMultipleMessagesToDifferentTopics(t *testing.T) {
producer := NewProducer(t)
msg1 := newMessage(time.Now(), "1", "a")
msg2 := newMessage(time.Now(), "2", "b")
msg3 := newMessage(time.Now(), "3", "c")
// Need to call this three times to expect three messages
producer.ExpectInputAndSucceed()
producer.ExpectInputAndSucceed()
producer.ExpectInputAndSucceed()
WriteMessage("topic1", msg1, msg1.Source, KEY_BY_TIMESTAMP, producer)
WriteMessage("topic2", msg2, msg2.Source, KEY_BY_TIMESTAMP, producer)
WriteMessage("topic3", msg3, msg3.Source, KEY_BY_TIMESTAMP, producer)
out1 := <-producer.Successes()
out2 := <-producer.Successes()
out3 := <-producer.Successes()
assert.NotNil(t, out1)
assertLineMatch(t,"a", out1)
assertTopic(t, "topic1", out1)
assert.NotNil(t, out2)
assertLineMatch(t,"b", out2)
assertTopic(t, "topic2", out2)
assert.NotNil(t, out3)
assertLineMatch(t,"c", out3)
assertTopic(t, "topic3", out3)
}
func TestKeyByTimestamp(t *testing.T) {
producer := NewProducer(t)
expectedTime := time.Now()
expectedTimeAsUnixTime := expectedTime.Unix()
msg1 := newMessage(expectedTime, "1", "a")
producer.ExpectInputAndSucceed()
WriteMessage("topic1", msg1, msg1.Source, KEY_BY_TIMESTAMP, producer)
out1 := <-producer.Successes()
keyBytes,err := out1.Key.Encode()
if err != nil {
t.Error(err)
t.Fail()
}
assert.Equal(t, string(expectedTimeAsUnixTime), string(keyBytes))
}
func TestKeyByContainerId(t *testing.T) {
producer := NewProducer(t)
expectedContainerId := "containerABC"
msg1 := newMessage(time.Now(), "1", "a")
producer.ExpectInputAndSucceed()
WriteMessage("topic1", msg1, expectedContainerId, KEY_BY_CONTAINER_ID, producer)
out1 := <-producer.Successes()
keyBytes,err := out1.Key.Encode()
if err != nil {
t.Error(err)
t.Fail()
}
assert.Equal(t, expectedContainerId, string(keyBytes))
}
func assertTopic(t *testing.T, expectedTopic string, message *sarama.ProducerMessage) {
assert.Equal(t, expectedTopic, message.Topic)
}
func assertLineMatch(t *testing.T, expectedLine string, message *sarama.ProducerMessage) {
outputJson := unmarshallMessage(message, t)
assert.Equal(t, expectedLine, outputJson.Line)
}
func unmarshallMessage(message *sarama.ProducerMessage, t *testing.T) (LogMessage) {
msgContentBytes, err := message.Value.Encode()
if err != nil {
t.Error(err)
t.FailNow()
}
var outputJson LogMessage
jErr := json.Unmarshal(msgContentBytes, &outputJson)
if jErr != nil {
t.Error(jErr)
t.FailNow()
}
return outputJson
}
func newMessage(expectedTime time.Time, expectedSource string, expectedLine string) (LogMessage) {
var msg LogMessage
msg.Timestamp = expectedTime
msg.Source = expectedSource
msg.Line = expectedLine
msg.Partial = false
return msg
}
func NewProducer(t *testing.T) *mocks.AsyncProducer {
config := sarama.NewConfig()
config.Producer.Return.Successes = true
producer := mocks.NewAsyncProducer(t, config)
return producer
}