-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
153 lines (120 loc) · 3.54 KB
/
main_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
package main
import (
"context"
"encoding/base64"
"net"
"os"
"testing"
"time"
"github.com/jinzhu/copier"
"github.com/kelseyhightower/envconfig"
kafka "github.com/segmentio/kafka-go"
yaml "gopkg.in/yaml.v2"
)
type TestMessage struct {
Message string `yaml:"message"`
ExpectedTopic string `yaml:"expected_topic"`
}
type TestSplit struct {
InputTopic string
Extractor Extractor `yaml:"extractor"`
OutputTopic string `yaml:"output_topic"`
Action string `yaml:"action"`
}
type TestSpliter struct {
TestSplits []TestSplit `yaml:"splits"`
InputTopic string `yaml:"input_topic"`
Actions map[string]string `yaml:"actions"`
TestMessages []TestMessage `yaml:"test_messages"`
}
type TestSpliterCollection struct {
Spliters []TestSpliter `yaml:"spliters_templates"`
}
func TestMessageRouting(t *testing.T) {
spliters := TestSpliterCollection{}
splitConf := os.Getenv("SPLIT_CONF")
logger := GetLogger()
data, err := base64.StdEncoding.DecodeString(splitConf)
if err != nil {
logger.Printf("Error: %s", err)
return
}
err = yaml.Unmarshal([]byte(data), &spliters)
if err != nil {
logger.Fatalf("Problem with file %s: %s", splitConf, err)
}
templateReaderConfig := ReaderConfig{}
templateWriterConfig := WriterConfig{}
err = envconfig.Process("pannet-kafka-streamer", &templateReaderConfig)
if err != nil {
logger.Fatal(err.Error())
}
err = envconfig.Process("pannet-kafka-streamer", &templateWriterConfig)
if err != nil {
logger.Fatal(err.Error())
}
for _, spliter := range spliters.Spliters {
for _, testMsg := range spliter.TestMessages {
dialer := &kafka.Dialer{
Timeout: 10 * time.Second,
}
readerConfig := templateReaderConfig
readerConfig.Topic = testMsg.ExpectedTopic
readerConfig.GroupID = spliter.InputTopic
readerConfig.Dialer = dialer
readerConfig.ErrorLogger = logger
writerConfig := templateWriterConfig
writerConfig.Topic = spliter.InputTopic
writerConfig.Dialer = dialer
writerConfig.ErrorLogger = logger
writerKafkaConfig := &kafka.WriterConfig{}
readerKafkaConfig := &kafka.ReaderConfig{}
copier.Copy(&writerKafkaConfig, &writerConfig)
copier.Copy(&readerKafkaConfig, &readerConfig)
for index := 0; index < 20; index++ {
conn, err := (&kafka.Dialer{
Resolver: &net.Resolver{},
}).DialLeader(context.Background(), "tcp", writerConfig.Brokers[0], spliter.InputTopic, 0)
if err != nil {
logger.Printf("Failed to open a new kafka connection: %s", err)
defer conn.Close()
time.Sleep(3 * time.Second)
} else {
break
}
}
w := kafka.NewWriter(*writerKafkaConfig)
defer w.Close()
newMsg := kafka.Message{
Value: []byte(testMsg.Message),
}
logger.Printf("%v", newMsg)
err = w.WriteMessages(context.Background(),
newMsg,
)
if err != nil {
logger.Fatalf("%s", err)
}
r := kafka.NewReader(*readerKafkaConfig)
defer r.Close()
var m kafka.Message
for index := 0; index < 10; index++ {
logger.Print("Fetching")
m, err = r.ReadMessage(context.Background())
logger.Printf("Read message %s", m.Value)
if err != nil {
logger.Fatalf("%s", err)
}
if string(m.Value) != testMsg.Message {
logger.Printf("Message: %s is not in expected topic: %s! %s aaa", testMsg.Message, testMsg.ExpectedTopic, m.Value)
time.Sleep(3 * time.Second)
} else {
break
}
}
if string(m.Value) != testMsg.Message {
t.Fatalf("Message: %s is not in expected topic: %s!", testMsg.Message, testMsg.ExpectedTopic)
}
}
}
}