-
Notifications
You must be signed in to change notification settings - Fork 15
/
zk_group_storage_test.go
139 lines (113 loc) · 3.4 KB
/
zk_group_storage_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
package consumergroup
import (
"fmt"
"math/rand"
"testing"
"time"
)
const (
testValue = "go_test_value"
testTopic = "go_test_topic"
testGroup = "go_test_group"
testConsumerID = "go_test_consumer_id"
)
func TestZKGroupStorageClaimAndGetAndReleasePartition(t *testing.T) {
zk := newZKGroupStorage([]string{"127.0.0.1:2181"}, 6*time.Second)
err := zk.claimPartition(testGroup, testTopic, 0, testConsumerID)
if err != nil {
t.Error(err)
}
err = zk.releasePartition(testGroup, testTopic, 0)
if err != nil {
t.Error(err)
}
zk.claimPartition(testGroup, testTopic, 0, testConsumerID)
err = zk.claimPartition(testGroup, testTopic, 0, testConsumerID)
if err == nil {
zk.releasePartition(testGroup, testTopic, 0)
t.Error("Expected it can't claim a partition twice, but it did")
}
cid, err := zk.getPartitionOwner(testGroup, testTopic, 0)
if err != nil {
zk.releasePartition(testGroup, testTopic, 0)
t.Error("get partition owner failed, because: ", err)
}
if cid != testConsumerID {
zk.releasePartition(testGroup, testTopic, 0)
t.Error("partition owner get from zookeeper isn't unexpected")
}
zk.releasePartition(testGroup, testTopic, 0)
}
func TestZKGroupStorageRegisterAndGetAndDeleteConsumer(t *testing.T) {
zk := newZKGroupStorage([]string{"127.0.0.1:2181"}, 6*time.Second)
err := zk.registerConsumer(testGroup, testConsumerID, nil)
if err != nil {
t.Fatal(err)
}
err = zk.deleteConsumer(testGroup, testConsumerID)
if err != nil {
t.Fatal(err)
}
zk.registerConsumer(testGroup, testConsumerID, nil)
err = zk.registerConsumer(testGroup, testConsumerID, nil)
if err == nil {
zk.deleteConsumer(testGroup, testConsumerID)
t.Fatal("Expected it can't register consumer twice, but it did")
}
consumerList, err := zk.getConsumerList(testGroup)
if err != nil {
t.Fatal(err)
}
if consumerList[0] != testConsumerID {
zk.deleteConsumer(testGroup, testConsumerID)
t.Fatal("consumer id get from zookeeper isn't expected")
}
zk.deleteConsumer(testGroup, testConsumerID)
}
func TestZKGroupWatchConsumerList(t *testing.T) {
zk := newZKGroupStorage([]string{"127.0.0.1:2181"}, 6*time.Second)
consumer1 := fmt.Sprintf("%s-%d", testConsumerID, rand.Int())
consumer2 := fmt.Sprintf("%s-%d", testConsumerID, rand.Int())
consumer3 := fmt.Sprintf("%s-%d", testConsumerID, rand.Int())
consumerList := []string{consumer1, consumer2, consumer3}
for _, consumer := range consumerList {
zk.registerConsumer(testGroup, consumer, nil)
}
watcher, err := zk.watchConsumerList(testGroup)
if err != nil {
t.Error(err)
}
select {
case <-watcher.EvCh:
t.Error("channel receive message before consumer list change")
default:
}
zk.deleteConsumer(testGroup, consumer1)
select {
case <-watcher.EvCh:
default:
t.Error("channel can't receive message after consumer list change")
}
for _, consumer := range consumerList {
zk.deleteConsumer(testGroup, consumer)
}
}
func TestZKGroupStorageCommitAndGetOffset(t *testing.T) {
zk := newZKGroupStorage([]string{"127.0.0.1:2181"}, 6*time.Second)
testOffset := rand.Int63()
err := zk.commitOffset(testGroup, testTopic, 0, testOffset)
if err != nil {
t.Error(err)
}
offset, err := zk.getOffset(testGroup, testTopic, 0)
if err != nil {
t.Error(err)
}
if offset != testOffset {
t.Error("offset get from zookeeper isn't unexpected")
}
err = zk.commitOffset(testGroup, testTopic, 0, testOffset+1)
if err != nil {
t.Error(err)
}
}