Skip to content

Commit a4b03b2

Browse files
committed
kgo: add test around purging then adding direct partitions
1 parent a7cede1 commit a4b03b2

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

Diff for: pkg/kgo/consumer_direct_test.go

+53-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestIssue337(t *testing.T) {
4545
t.Fatal(err)
4646
}
4747

48-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
48+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
4949
defer cancel()
5050
for {
5151
fs := cl.PollFetches(ctx)
@@ -60,3 +60,55 @@ func TestIssue337(t *testing.T) {
6060
}
6161
}
6262
}
63+
64+
func TestDirectPartitionPurge(t *testing.T) {
65+
topic, cleanup := tmpTopicPartitions(t, 2)
66+
defer cleanup()
67+
68+
cl, _ := NewClient(
69+
DefaultProduceTopic(topic),
70+
RecordPartitioner(ManualPartitioner()),
71+
ConsumePartitions(map[string]map[int32]Offset{
72+
topic: {0: NewOffset().At(0)},
73+
}),
74+
)
75+
76+
if err := cl.ProduceSync(context.Background(),
77+
&Record{Partition: 0, Value: []byte("foo")},
78+
&Record{Partition: 1, Value: []byte("bar")},
79+
).FirstErr(); err != nil {
80+
t.Fatal(err)
81+
}
82+
cl.PurgeTopicsFromClient(topic)
83+
84+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
85+
fs := cl.PollFetches(ctx)
86+
cancel()
87+
if err := fs.Err0(); err != context.DeadlineExceeded {
88+
t.Fatal("unexpected success when expecting context.DeadlineExceeded")
89+
}
90+
91+
cl.AddConsumeTopics(topic)
92+
ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
93+
94+
exp := map[string]bool{
95+
"foo": true,
96+
"bar": true,
97+
}
98+
for {
99+
fs := cl.PollFetches(ctx)
100+
if err := fs.Err0(); err == context.DeadlineExceeded {
101+
break
102+
}
103+
fs.EachRecord(func(r *Record) {
104+
v := string(r.Value)
105+
if !exp[v] {
106+
t.Errorf("saw unexpected value %v", v)
107+
}
108+
delete(exp, v)
109+
})
110+
}
111+
if len(exp) > 0 {
112+
t.Errorf("did not see expected values %v", exp)
113+
}
114+
}

0 commit comments

Comments
 (0)