forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_test.go
66 lines (54 loc) · 1.15 KB
/
conn_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
package kafka
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
)
func TestConn(t *testing.T) {
tests := []struct {
scenario string
function func(*testing.T, *Conn)
}{
{
scenario: "close right away",
function: testConnClose,
},
{
scenario: "write messages to kafka",
function: testConnWrite,
},
}
id := int32(0)
for _, test := range tests {
testFunc := test.function
t.Run(test.scenario, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
topic := fmt.Sprintf("kafka-go-%02d", atomic.AddInt32(&id, 1))
conn, err := (&Dialer{}).DialLeader(ctx, "tcp", "localhost:9092", topic, 0)
if err != nil {
t.Fatal("failed to open a new kafka connection:", err)
}
defer conn.Close()
testFunc(t, conn)
})
}
}
func testConnClose(t *testing.T, conn *Conn) {
if err := conn.Close(); err != nil {
t.Error(err)
}
}
func testConnWrite(t *testing.T, conn *Conn) {
b := []byte("Hello World!")
n, err := conn.Write(b)
if err != nil {
t.Error(err)
}
if n != len(b) {
t.Error("bad length returned by (*Conn).Write:", n)
}
}