forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_test.go
195 lines (167 loc) · 6.65 KB
/
producer_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
package kafka
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestProduce tests the produce function
func TestProduce(t *testing.T) {
test := GetTestModuleInstance(t)
writer, err := test.module.Kafka.Writer(
[]string{"localhost:9092"}, "test-topic", SASLConfig{}, TLSConfig{}, "")
assert.Nil(t, err)
assert.NotNil(t, writer)
defer writer.Close()
// Produce a message in the init context
err = test.module.Kafka.Produce(writer, []map[string]interface{}{
{
"key": "key1",
"value": "value1",
},
{
"key": "key2",
"value": "value2",
},
}, "", "", false)
assert.NotNil(t, err)
assert.Equal(t, ErrorForbiddenInInitContext, err)
// Create a topic before producing messages, otherwise tests will fail.
err = test.module.CreateTopic(
"localhost:9092", "test-topic", 1, 1, "", SASLConfig{}, TLSConfig{})
assert.Nil(t, err)
require.NoError(t, test.moveToVUCode())
// Produce two messages in the VU function
err = test.module.Kafka.Produce(writer, []map[string]interface{}{
{
"key": "key1",
"value": "value1",
},
{
"key": "key2",
"value": "value2",
},
}, "", "", false)
assert.Nil(t, err)
// Check if two message were produced
metricsValues := test.GetCounterMetricsValues()
assert.Equal(t, 2.0, metricsValues[test.module.metrics.WriterWrites.Name])
assert.Equal(t, 2.0, metricsValues[test.module.metrics.WriterMessages.Name])
assert.Equal(t, 64.0, metricsValues[test.module.metrics.WriterBytes.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterErrors.Name])
assert.GreaterOrEqual(t, 1.0, metricsValues[test.module.metrics.WriterWriteTime.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterWaitTime.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterRetries.Name])
assert.Equal(t, 1.0, metricsValues[test.module.metrics.WriterBatchSize.Name])
assert.Equal(t, 32.0, metricsValues[test.module.metrics.WriterBatchBytes.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterMaxAttempts.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterMaxBatchSize.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterBatchTimeout.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterReadTimeout.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterWriteTimeout.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterRequiredAcks.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterAsync.Name])
}
// TestProduceWithoutKey tests the produce function without a key
func TestProduceWithoutKey(t *testing.T) {
test := GetTestModuleInstance(t)
writer, err := test.module.Kafka.Writer(
[]string{"localhost:9092"}, "", SASLConfig{}, TLSConfig{}, "")
assert.Nil(t, err)
assert.NotNil(t, writer)
defer writer.Close()
// Create a topic before producing messages, otherwise tests will fail.
err = test.module.CreateTopic(
"localhost:9092", "test-topic", 1, 1, "", SASLConfig{}, TLSConfig{})
assert.Nil(t, err)
require.NoError(t, test.moveToVUCode())
// Produce two messages in the VU function
err = test.module.Kafka.Produce(writer, []map[string]interface{}{
{
"value": "value1",
// The topic should be set either on the writer or on individual messages
"topic": "test-topic",
"offset": int64(0),
"time": time.Now().UnixMilli(),
},
{
"value": "value2",
"topic": "test-topic",
},
}, "", "", false)
assert.Nil(t, err)
// Check if two message were produced
metricsValues := test.GetCounterMetricsValues()
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterErrors.Name])
// Notice the smaller size because the key is not present (64 -> 56)
assert.Equal(t, 56.0, metricsValues[test.module.metrics.WriterBytes.Name])
assert.Equal(t, 2.0, metricsValues[test.module.metrics.WriterMessages.Name])
assert.Equal(t, 2.0, metricsValues[test.module.metrics.WriterWrites.Name])
}
// TestProducerContextCancelled tests the produce function with a cancelled context
func TestProducerContextCancelled(t *testing.T) {
test := GetTestModuleInstance(t)
writer, err := test.module.Kafka.Writer(
[]string{"localhost:9092"}, "test-topic", SASLConfig{}, TLSConfig{}, "")
assert.Nil(t, err)
assert.NotNil(t, writer)
defer writer.Close()
// Create a topic before producing messages, otherwise tests will fail.
err = test.module.CreateTopic(
"localhost:9092", "test-topic", 1, 1, "", SASLConfig{}, TLSConfig{})
assert.Nil(t, err)
require.NoError(t, test.moveToVUCode())
// This will cancel the context, so the produce will fail
test.cancelContext()
// Produce two messages in the VU function
err = test.module.Kafka.Produce(writer, []map[string]interface{}{
{
"key": "key1",
"value": "value1",
},
{
"key": "key2",
"value": "value2",
},
}, "", "", false)
assert.NotNil(t, err)
assert.Equal(t, "Context cancelled.", err.Message)
assert.Equal(t, context.Canceled, err.Unwrap())
// Cancelled context is immediately reflected in metrics, because
// we need the context object to update the metrics.
metricsValues := test.GetCounterMetricsValues()
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterErrors.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterBytes.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterMessages.Name])
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterWrites.Name])
}
// TestProduceJSON tests the produce function with a JSON value
func TestProduceJSON(t *testing.T) {
// TODO: change this once the interfaces accept JSON
test := GetTestModuleInstance(t)
writer, err := test.module.Kafka.Writer(
[]string{"localhost:9092"}, "test-topic", SASLConfig{}, TLSConfig{}, "")
assert.Nil(t, err)
// Create a topic before producing messages, otherwise tests will fail.
err = test.module.CreateTopic(
"localhost:9092", "test-topic", 1, 1, "", SASLConfig{}, TLSConfig{})
assert.Nil(t, err)
require.NoError(t, test.moveToVUCode())
serialized, jsonErr := json.Marshal(map[string]interface{}{"field": "value"})
assert.Nil(t, jsonErr)
// Produce a message in the VU function
err = test.module.Kafka.Produce(writer, []map[string]interface{}{
{
"value": string(serialized),
},
}, "", "", false)
assert.Nil(t, err)
// Check if one message was produced
metricsValues := test.GetCounterMetricsValues()
assert.Equal(t, 0.0, metricsValues[test.module.metrics.WriterErrors.Name])
assert.Equal(t, 39, int(metricsValues[test.module.metrics.WriterBytes.Name]))
assert.Equal(t, 1.0, metricsValues[test.module.metrics.WriterMessages.Name])
assert.Equal(t, 1.0, metricsValues[test.module.metrics.WriterWrites.Name])
}