-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcomponent_test.go
71 lines (65 loc) · 1.63 KB
/
component_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
package ekafka
import (
"context"
"fmt"
"log"
"strings"
"testing"
"time"
"github.com/BurntSushi/toml"
"github.com/gotomicro/ego/core/econf"
"github.com/stretchr/testify/assert"
)
func produce(w *Producer) {
err := w.WriteMessages(context.Background(),
&Message{Key: []byte("Key-A"), Value: []byte("Hello World!")},
&Message{Key: []byte("Key-B"), Value: []byte("One!")},
&Message{Key: []byte("Key-C"), Value: []byte("Two!")},
)
if err != nil {
log.Fatal("failed to write messages:", err)
}
if err := w.Close(); err != nil {
log.Fatal("failed to close writer:", err)
}
fmt.Println(`produce message succ--------------->`)
}
func consume(r *Consumer) {
ctx := context.Background()
for {
// the `ReadMessage` method blocks until we receive the next event
msg, _, err := r.ReadMessage(ctx)
if err != nil {
panic("could not read message " + err.Error())
}
// after receiving the message, log its value
fmt.Println("received: ", string(msg.Value))
err = r.CommitMessages(ctx, &msg)
if err != nil {
log.Printf("fail to commit msg:%v", err)
}
}
}
func TestProduceConsume(t *testing.T) {
conf := `
[kafka]
debug=true
brokers=["localhost:9091","localhost:9092","localhost:9093"]
[kafka.client]
timeout="3s"
[kafka.producers.p1]
topic="sre-infra-test"
[kafka.consumers.c1]
topic="sre-infra-test"
groupID="group-1"
[kafka.consumers.c2]
topic="sre-infra-test"
groupID="group-2"
`
err := econf.LoadFromReader(strings.NewReader(conf), toml.Unmarshal)
assert.NoError(t, err)
cmp := Load("kafka").Build()
go produce(cmp.Producer("p1"))
consume(cmp.Consumer("c1"))
time.Sleep(60 * time.Second)
}