-
Notifications
You must be signed in to change notification settings - Fork 2
/
consume_worker_test.go
208 lines (184 loc) · 5.1 KB
/
consume_worker_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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"net"
"os"
"strconv"
"testing"
"time"
"github.com/segmentio/kafka-go"
"github.com/test-go/testify/assert"
"github.com/databendcloud/bend-ingest-kafka/config"
)
type consumeWorkerTest struct {
databendDSN string
kafkaBrokers []string
}
func prepareConsumeWorkerTest(topic string, partition int) *consumeWorkerTest {
testDatabendDSN := os.Getenv("TEST_DATABEND_DSN")
if testDatabendDSN == "" {
testDatabendDSN = "http://databend:databend@localhost:8000?presigned_url_disabled=true"
}
testKafkaBroker := os.Getenv("TEST_KAFKA_BROKER")
if testKafkaBroker == "" {
testKafkaBroker = "127.0.0.1:9092"
}
tt := &consumeWorkerTest{
databendDSN: testDatabendDSN,
kafkaBrokers: []string{testKafkaBroker},
}
tt.setupKafkaTopic(topic, partition)
return tt
}
func (tt *consumeWorkerTest) setupKafkaTopic(topic string, partition int) {
conn, err := kafka.Dial("tcp", tt.kafkaBrokers[0])
if err != nil {
panic(err)
}
controller, err := conn.Controller()
if err != nil {
panic(err)
}
controllerConn, err := kafka.Dial("tcp", net.JoinHostPort("localhost", strconv.Itoa(controller.Port)))
if err != nil {
panic(err.Error())
}
defer controllerConn.Close()
topicConfigs := []kafka.TopicConfig{{Topic: topic, NumPartitions: partition, ReplicationFactor: 1}}
err = controllerConn.CreateTopics(topicConfigs...)
if err != nil {
log.Printf(err.Error())
}
}
func TestProduceMessage(t *testing.T) {
produceMessage("produce_test", 1)
}
func produceMessage(topic string, partition int) {
tt := prepareConsumeWorkerTest(topic, partition)
// Set up a context
ctx := context.Background()
// Set up Kafka writer configuration
writerConfig := kafka.WriterConfig{
Brokers: tt.kafkaBrokers,
Topic: topic,
}
// Create a Kafka writer
writer := kafka.NewWriter(writerConfig)
// Send a message to Kafka
for i := 0; i < 3; i++ {
message := kafka.Message{
Key: []byte("name"),
Value: []byte("{\"i64\": 10,\"u64\": 30,\"f64\": 20,\"s\": \"hao\",\"s2\": \"hello\",\"a16\":[1],\"a8\":[2],\"d\": \"2011-03-06\",\"t\": \"2016-04-04 11:30:00\"}"),
}
err := writer.WriteMessages(ctx, message)
if err != nil {
log.Fatal("Failed to send message:", err)
}
}
// Close the Kafka writer
err := writer.Close()
if err != nil {
log.Fatal("Failed to close writer:", err)
}
}
func TestConsumeKafka(t *testing.T) {
consumeTopic := "consume_test"
consumePartition := 2
tt := prepareConsumeWorkerTest(consumeTopic, consumePartition)
db, err := sql.Open("databend", tt.databendDSN)
assert.NoError(t, err)
execute(db, `CREATE OR REPLACE TABLE test_ingest (
i64 Int64,
u64 UInt64,
f64 Float64,
s String,
s2 String,
a16 Array(Int16),
a8 Array(UInt8),
d Date,
t DateTime)`)
defer execute(db, "drop table if exists test_ingest;")
produceMessage(consumeTopic, consumePartition)
fmt.Println("start consuming data")
cfg := &config.Config{
DatabendDSN: tt.databendDSN,
DatabendTable: "test_ingest",
KafkaTopic: consumeTopic,
KafkaBootstrapServers: tt.kafkaBrokers[0],
IsJsonTransform: true,
KafkaConsumerGroup: "test",
BatchSize: 10,
Workers: 1,
DataFormat: "json",
BatchMaxInterval: 10,
DisableVariantCheck: false,
UserStage: "~",
}
ig := NewDatabendIngester(cfg)
w := NewConsumeWorker(cfg, "worker1", ig)
log.Printf("start consume")
w.stepBatch()
result, err := db.Query("select * from test_ingest")
assert.NoError(t, err)
count := 0
for result.Next() {
count += 1
var i64 int64
var u64 uint64
var f64 float64
var s string
var s2 string
var a16 []int16
var a8 []uint8
var d time.Time
var t time.Time
err = result.Scan(&i64, &u64, &f64, &s, &s2, &a16, &a8, &d, &t)
fmt.Println(i64, u64, f64, s, s2, a16, a8, d, t)
}
assert.NotEqual(t, 0, count)
}
func TestConsumerWithoutTransform(t *testing.T) {
consumeRawTopic := "consume_raw_test"
consumeRawPartition := 3
tt := prepareConsumeWorkerTest(consumeRawTopic, consumeRawPartition)
db, err := sql.Open("databend", tt.databendDSN)
assert.NoError(t, err)
defer execute(db, "drop table if exists test_ingest_raw;")
produceMessage(consumeRawTopic, consumeRawPartition)
fmt.Println("start consuming data")
cfg := &config.Config{
DatabendDSN: tt.databendDSN,
DatabendTable: "test_ingest_raw",
KafkaTopic: consumeRawTopic,
KafkaBootstrapServers: tt.kafkaBrokers[0],
IsJsonTransform: false,
KafkaConsumerGroup: "test",
BatchSize: 10,
Workers: 1,
DataFormat: "json",
BatchMaxInterval: 10,
DisableVariantCheck: true,
UserStage: "~",
}
ig := NewDatabendIngester(cfg)
if !cfg.IsJsonTransform {
err := ig.CreateRawTargetTable()
if err != nil {
panic(err)
}
}
w := NewConsumeWorker(cfg, "worker1", ig)
log.Printf("start consume")
err = w.stepBatch()
assert.NoError(t, err)
result, err := db.Query("select * from test_ingest_raw")
assert.NoError(t, err)
count := 0
for result.Next() {
count += 1
}
assert.NotEqual(t, 0, count)
}