-
Notifications
You must be signed in to change notification settings - Fork 0
/
producerdlq_test.go
95 lines (85 loc) · 2.15 KB
/
producerdlq_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
package wkafka
import (
"context"
"errors"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/twmb/franz-go/pkg/kgo"
)
func Test_producerDLQ(t *testing.T) {
clientID := []byte("test-client")
topic := "test-topic"
errDLQ := &DLQError{
Err: errors.New("test error"),
Indexes: map[int]error{
0: errors.New("indexed error"),
2: nil,
},
}
orgRecords := []*kgo.Record{
{
Topic: "original-topic",
Key: []byte("key"),
Value: []byte("value"),
Headers: []kgo.RecordHeader{},
Offset: 123,
Partition: 1,
Timestamp: time.Now(),
},
{
// This record should be skipped
Topic: "original-topic",
Key: []byte("key"),
Value: []byte("value-1"),
Headers: []kgo.RecordHeader{},
Offset: 124,
Partition: 1,
Timestamp: time.Now(),
},
{
Topic: "original-topic",
Key: []byte("key"),
Value: []byte("value-2"),
Headers: []kgo.RecordHeader{},
Offset: 125,
Partition: 2,
Timestamp: time.Now(),
},
}
// Define a mock function to be wrapped by producerDLQ
mockFn := func(ctx context.Context, records []*kgo.Record) error {
for i := range records {
var orgRecord *kgo.Record
errDLQStr := errDLQ.Err.Error()
switch i {
case 0:
orgRecord = orgRecords[0]
errDLQStr = "indexed error"
case 1:
orgRecord = orgRecords[2]
}
for _, header := range records[i].Headers {
switch header.Key {
case HeaderServiceKey:
assert.Equal(t, clientID, header.Value)
case "error":
assert.Equal(t, errDLQStr, string(header.Value))
case "offset":
assert.Equal(t, strconv.FormatInt(orgRecord.Offset, 10), string(header.Value))
case "partition":
assert.Equal(t, strconv.FormatInt(int64(orgRecord.Partition), 10), string(header.Value))
case "topic":
assert.Equal(t, orgRecord.Topic, string(header.Value))
case "timestamp":
assert.Equal(t, orgRecord.Timestamp.Format(time.RFC3339), string(header.Value))
}
}
}
return nil
}
dlqProducer := producerDLQ(topic, clientID, mockFn)
err := dlqProducer(context.Background(), errDLQ, orgRecords)
assert.NoError(t, err)
}