-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_test.go
154 lines (141 loc) · 3.3 KB
/
producer_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
package wkafka
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/twmb/franz-go/pkg/kgo"
)
type testData struct {
Name string
Topic string
Details map[string]interface{}
}
func produceHook(d *testData, r *kgo.Record) error {
r.Value = []byte(d.Name)
r.Headers = append(r.Headers, Header{
Key: "name",
Value: []byte(d.Name),
})
r.Key = []byte(d.Name)
r.Topic = d.Topic
return nil
}
func Test_produce_Produce(t *testing.T) {
type fields[T any] struct {
Config producerConfig[T]
ProduceRaw func(t *testing.T) func(ctx context.Context, records []*kgo.Record) error
}
type args struct {
ctx context.Context
data []*testData
}
type testCase[T any] struct {
name string
fields fields[T]
args args
wantErr bool
}
tests := []testCase[*testData]{
{
name: "test",
fields: fields[*testData]{
Config: producerConfig[*testData]{
Topic: "test",
Headers: []Header{
{
Key: "server",
Value: []byte("test"),
},
},
Hook: produceHook,
Encode: codecJSON[*testData]{}.Encode,
},
ProduceRaw: func(t *testing.T) func(ctx context.Context, records []*kgo.Record) error {
return func(ctx context.Context, records []*kgo.Record) error {
t.Helper()
if len(records) != 1 {
t.Errorf("produce.Produce() len(records) = %v, want %v", len(records), 3)
}
for i := range records {
switch i {
case 0:
assert.Equal(t, "test", records[i].Topic)
assert.Equal(t, "test", string(records[i].Key))
// assert.Equal(t, `{"Name":"test","Topic":"test","Details":{"key":1234}}`, string(records[i].Value))
assert.Equal(t, `test`, string(records[i].Value))
assert.Equal(t, 2, len(records[i].Headers))
assert.Equal(t, "server", records[i].Headers[0].Key)
assert.Equal(t, "test", string(records[i].Headers[0].Value))
assert.Equal(t, "name", records[i].Headers[1].Key)
assert.Equal(t, "test", string(records[i].Headers[1].Value))
}
}
return nil
}
},
},
args: args{
ctx: context.Background(),
data: []*testData{
{
Name: "test",
Topic: "test",
Details: map[string]interface{}{
"key": 1234,
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Producer[*testData]{
config: tt.fields.Config,
produceRaw: tt.fields.ProduceRaw(t),
}
if err := p.Produce(tt.args.ctx, tt.args.data...); (err != nil) != tt.wantErr {
t.Errorf("produce.Produce() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func BenchmarkProduce(b *testing.B) {
p := &Producer[*testData]{
config: producerConfig[*testData]{
Topic: "test",
Headers: []Header{
{
Key: "server",
Value: []byte("test"),
},
},
},
produceRaw: func(ctx context.Context, records []*kgo.Record) error {
return nil
},
}
datas := []*testData{
nil,
// []byte("test"),
{
Name: "test",
Topic: "test",
Details: map[string]interface{}{
"key": 1234,
},
},
{
Name: "test",
Topic: "test",
Details: map[string]interface{}{
"key": 1234,
},
},
}
for i := 0; i < b.N; i++ {
if err := p.Produce(context.Background(), datas...); err != nil {
b.Error(err)
}
}
}