-
Notifications
You must be signed in to change notification settings - Fork 48
/
topic_metadata_test.go
210 lines (179 loc) · 5.8 KB
/
topic_metadata_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package kazoo
import (
"sort"
"testing"
)
func TestPartition(t *testing.T) {
topic := &Topic{Name: "test"}
partition := topic.Partition(1, []int32{1, 2, 3})
if key := partition.Key(); key != "test/1" {
t.Error("Unexpected partition key", key)
}
if partition.Topic() != topic {
t.Error("Expected Topic() to return the topic the partition was created from.")
}
if pr := partition.PreferredReplica(); pr != 1 {
t.Error("Expected 1 to be the preferred replica, but found", pr)
}
partitionWithoutReplicas := topic.Partition(1, nil)
if pr := partitionWithoutReplicas.PreferredReplica(); pr != -1 {
t.Error("Expected -1 to be returned if the partition does not have replicas, but found", pr)
}
}
func TestTopicList(t *testing.T) {
topics := TopicList{
&Topic{Name: "foo"},
&Topic{Name: "bar"},
&Topic{Name: "baz"},
}
sort.Sort(topics)
if topics[0].Name != "bar" || topics[1].Name != "baz" || topics[2].Name != "foo" {
t.Error("Unexpected order after sorting topic list", topics)
}
topic := topics.Find("foo")
if topic != topics[2] {
t.Error("Should have found foo topic from the list")
}
}
func TestPartitionList(t *testing.T) {
var (
topic1 = &Topic{Name: "1"}
topic2 = &Topic{Name: "2"}
)
var (
partition21 = topic2.Partition(1, nil)
partition12 = topic1.Partition(2, nil)
partition11 = topic1.Partition(1, nil)
)
partitions := PartitionList{partition21, partition12, partition11}
sort.Sort(partitions)
if partitions[0] != partition11 || partitions[1] != partition12 || partitions[2] != partition21 {
t.Error("Unexpected order after sorting topic list", partitions)
}
}
func TestGeneratePartitionAssignments(t *testing.T) {
// check for errors
tests := []struct {
brokers []int32
partitionCount int
replicationFactor int
err error
}{
{[]int32{1, 2}, -1, 1, ErrInvalidPartitionCount},
{[]int32{1, 2}, 0, 1, ErrInvalidPartitionCount},
{[]int32{}, 1, 1, ErrInvalidReplicationFactor},
{[]int32{1, 2}, 1, -1, ErrInvalidReplicationFactor},
{[]int32{1, 2}, 2, 0, ErrInvalidReplicationFactor},
{[]int32{1, 2}, 3, 3, ErrInvalidReplicationFactor},
{[]int32{1, 2}, 2, 1, nil},
{[]int32{1, 2}, 10, 2, nil},
{[]int32{1}, 10, 1, nil},
{[]int32{1, 2, 3, 4, 5}, 1, 1, nil},
{[]int32{1, 2, 3, 4, 5}, 1, 3, nil},
{[]int32{1, 2, 3, 4, 5}, 10, 2, nil},
}
for testIdx, test := range tests {
topic := &Topic{Name: "t"}
res, err := topic.generatePartitionAssignments(test.brokers, test.partitionCount, test.replicationFactor)
if err != test.err {
t.Errorf("Incorrect error for test %d. Expected (%v) got (%v)", testIdx, test.err, err)
} else if err == nil {
// proper number of paritions
if len(res) != test.partitionCount {
t.Errorf("Wrong number of partitions assigned in test %d. Expected %d got %d", testIdx, test.partitionCount, len(res))
}
// ensure all petitions are assigned and that they have
// the right number of non-overlapping brokers
for i, part := range res {
if part == nil {
t.Errorf("Partition %d is nil in test %d", i, testIdx)
continue
}
if len(part.Replicas) != test.replicationFactor {
t.Errorf("Partition %d does not have the correct number of brokers in test %d. Expected %d got %d", i, testIdx, test.replicationFactor, len(part.Replicas))
}
replicaMap := make(map[int32]bool, test.replicationFactor)
for _, r := range part.Replicas {
// ensure broker is in initial broker list
found := false
for _, broker := range test.brokers {
if broker == r {
found = true
break
}
}
if !found {
t.Errorf("Partition %d has an invalid broker id %d in test %d", i, r, testIdx)
}
replicaMap[r] = true
}
if len(replicaMap) != len(part.Replicas) {
t.Errorf("Partition %d has overlapping broker assignments (%v) in test %d", i, part.Replicas, testIdx)
}
}
}
}
}
func TestValidatePartitionAssignments(t *testing.T) {
// check for errors
tests := []struct {
brokers []int32
partitions PartitionList
err error
}{
{[]int32{1}, PartitionList{}, ErrInvalidPartitionCount},
{[]int32{1}, PartitionList{
{ID: 0, Replicas: []int32{}},
}, ErrInvalidReplicationFactor},
{[]int32{1, 2}, PartitionList{
{ID: 0, Replicas: []int32{1}},
{ID: 1, Replicas: []int32{1, 2}},
}, ErrInvalidReplicaCount},
{[]int32{1, 2}, PartitionList{
{ID: 0, Replicas: []int32{1, 2}},
{ID: 1, Replicas: []int32{1}},
}, ErrInvalidReplicaCount},
{[]int32{1, 2}, PartitionList{
{ID: 0, Replicas: []int32{1, 2}},
{ID: 1, Replicas: []int32{2, 2}},
}, ErrReplicaBrokerOverlap},
{[]int32{1, 2}, PartitionList{
{ID: 0, Replicas: []int32{1, 3}},
{ID: 1, Replicas: []int32{2, 1}},
}, ErrInvalidBroker},
{[]int32{1, 2, 3}, PartitionList{
{ID: 1, Replicas: []int32{1, 3}},
{ID: 2, Replicas: []int32{2, 1}},
}, ErrMissingPartitionID},
{[]int32{1, 2, 3}, PartitionList{
{ID: 0, Replicas: []int32{1, 3}},
{ID: 2, Replicas: []int32{2, 1}},
}, ErrMissingPartitionID},
{[]int32{1, 2, 3}, PartitionList{
{ID: 0, Replicas: []int32{1, 3}},
{ID: 0, Replicas: []int32{1, 3}},
{ID: 2, Replicas: []int32{2, 1}},
}, ErrDuplicatePartitionID},
{[]int32{1}, PartitionList{
{ID: 0, Replicas: []int32{1}},
}, nil},
{[]int32{1}, PartitionList{
{ID: 0, Replicas: []int32{1}},
{ID: 1, Replicas: []int32{1}},
{ID: 2, Replicas: []int32{1}},
}, nil},
{[]int32{1, 2, 3}, PartitionList{
{ID: 0, Replicas: []int32{1, 2}},
{ID: 1, Replicas: []int32{2, 3}},
{ID: 2, Replicas: []int32{3, 1}},
}, nil},
}
for testIdx, test := range tests {
topic := &Topic{Name: "t"}
err := topic.validatePartitionAssignments(test.brokers, test.partitions)
if err != test.err {
t.Errorf("Incorrect error for test %d. Expected (%v) got (%v)", testIdx, test.err, err)
} else if err == nil {
}
}
}