79 lines
3.0 KiB
Go
79 lines
3.0 KiB
Go
package kafka
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/twmb/franz-go/pkg/kgo"
|
|
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
func TestEventSourceCommitAcceptsOnlyAPrefixOfTheLastPollPerPartition(t *testing.T) {
|
|
newSource := func() *EventSource {
|
|
source := &EventSource{
|
|
epochs: make(map[recordPosition]int32),
|
|
polled: make(map[topicPartition][]int64),
|
|
pending: make(map[string]map[int32]kgo.EpochOffset),
|
|
}
|
|
// The last poll returned offsets 4, 5, 6 of partition 0 and 9 of
|
|
// partition 1.
|
|
for partition, offsets := range map[int32][]int64{0: {4, 5, 6}, 1: {9}} {
|
|
for _, offset := range offsets {
|
|
source.epochs[recordPosition{topic: testTopic, partition: partition, offset: offset}] = 1
|
|
key := topicPartition{topic: testTopic, partition: partition}
|
|
source.polled[key] = append(source.polled[key], offset)
|
|
}
|
|
}
|
|
return source
|
|
}
|
|
record := func(partition int32, offset int64) outbound.ConsumedRecord {
|
|
return outbound.ConsumedRecord{Topic: testTopic, Partition: partition, Offset: offset}
|
|
}
|
|
rejected := map[string][]outbound.ConsumedRecord{
|
|
"not from the last poll": {record(0, 7)},
|
|
"gap": {record(0, 4), record(0, 6)},
|
|
"skips the first record": {record(0, 5)},
|
|
"out of order": {record(0, 5), record(0, 4)},
|
|
"repeated record": {record(0, 4), record(0, 4)},
|
|
"another topic": {{Topic: "other", Partition: 0, Offset: 4}},
|
|
}
|
|
for name, finished := range rejected {
|
|
t.Run(name, func(t *testing.T) {
|
|
source := newSource()
|
|
if err := source.Commit(t.Context(), finished); err == nil {
|
|
t.Fatal("Commit() accepted records that are not a prefix of the last poll")
|
|
}
|
|
if len(source.pending) != 0 {
|
|
t.Fatalf("Commit() kept a pending position for rejected records: %v", source.pending)
|
|
}
|
|
})
|
|
}
|
|
// Interleaved partitions are fine as long as each one is a prefix.
|
|
source := newSource()
|
|
if err := source.checkPrefix([]outbound.ConsumedRecord{record(0, 4), record(1, 9), record(0, 5)}); err != nil {
|
|
t.Fatalf("checkPrefix() rejected a valid prefix: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPendingPositionsAreLimitedAndForgottenPerPartition(t *testing.T) {
|
|
source := &EventSource{pending: map[string]map[int32]kgo.EpochOffset{
|
|
testTopic: {0: {Epoch: 1, Offset: 10}, 1: {Epoch: 1, Offset: 20}},
|
|
}}
|
|
revoked := clonePending(source.pending, map[string][]int32{testTopic: {1}})
|
|
if len(revoked[testTopic]) != 1 || revoked[testTopic][1].Offset != 20 {
|
|
t.Fatalf("clonePending() = %v, want only partition 1", revoked)
|
|
}
|
|
|
|
committed := clonePending(source.pending, nil)
|
|
// Partition 0 advanced while the commit was in flight, so it stays.
|
|
source.pending[testTopic][0] = kgo.EpochOffset{Epoch: 1, Offset: 11}
|
|
source.forgetCommitted(committed)
|
|
if len(source.pending[testTopic]) != 1 || source.pending[testTopic][0].Offset != 11 {
|
|
t.Fatalf("pending after forgetCommitted() = %v, want only the advanced partition 0", source.pending)
|
|
}
|
|
source.dropPending(map[string][]int32{testTopic: {0}})
|
|
if len(source.pending) != 0 {
|
|
t.Fatalf("pending after dropPending() = %v, want none", source.pending)
|
|
}
|
|
}
|