forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialer_test.go
69 lines (58 loc) · 1.57 KB
/
dialer_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
package kafka
import (
"context"
"reflect"
"sort"
"testing"
"time"
)
func TestDialer(t *testing.T) {
tests := []struct {
scenario string
function func(*testing.T, context.Context, *Dialer)
}{
{
scenario: "looking up partitions returns the list of available partitions for a topic",
function: testDialerLookupPartitions,
},
}
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()
testFunc(t, ctx, &Dialer{})
})
}
}
func testDialerLookupPartitions(t *testing.T, ctx context.Context, d *Dialer) {
// Write a message to ensure the partition gets created.
w := NewWriter(WriterConfig{
Brokers: []string{"localhost:9092"},
Topic: "test-dialer-LookupPartitions",
})
w.WriteMessages(ctx, Message{})
w.Close()
// for some reason the partition isn't available right away.
time.Sleep(1 * time.Second)
partitions, err := d.LookupPartitions(ctx, "tcp", "localhost:9092", "test-dialer-LookupPartitions")
if err != nil {
t.Error(err)
return
}
sort.Slice(partitions, func(i int, j int) bool {
return partitions[i].ID < partitions[j].ID
})
if !reflect.DeepEqual(partitions, []Partition{
{
Topic: "test-dialer-LookupPartitions",
Leader: Broker{Host: "localhost", Port: 9092, ID: 1001},
Replicas: []Broker{{Host: "localhost", Port: 9092, ID: 1001}},
Isr: []Broker{{Host: "localhost", Port: 9092, ID: 1001}},
ID: 0,
},
}) {
t.Error("bad partitions:", partitions)
}
}