-
Notifications
You must be signed in to change notification settings - Fork 14
/
otel_producer_test.go
82 lines (68 loc) · 1.9 KB
/
otel_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
package kafka
import (
"context"
"errors"
"testing"
"github.com/segmentio/kafka-go"
)
func Test_otelProducer_WriteMessages(t *testing.T) {
t.Run("Return_Success_When_Single_Message_Producing", func(t *testing.T) {
// Given
o := otelProducer{w: mockOtelKafkaKonsumerWriter{wantErr: false}}
// When
err := o.WriteMessages(context.TODO(), kafka.Message{})
// Then
if err != nil {
t.Fatalf("Error when single producing %v", err)
}
})
t.Run("Return_Error_When_Single_Message_Producing", func(t *testing.T) {
// Given
o := otelProducer{w: mockOtelKafkaKonsumerWriter{wantErr: true}}
// When
err := o.WriteMessages(context.TODO(), kafka.Message{})
// Then
if err == nil {
t.Fatalf("Success when single producing %v", err)
}
})
t.Run("Return_Success_When_Batch_Producing", func(t *testing.T) {
// Given
o := otelProducer{w: mockOtelKafkaKonsumerWriter{wantErr: false}}
// When
err := o.WriteMessages(context.TODO(), kafka.Message{}, kafka.Message{})
// Then
if err != nil {
t.Fatalf("Error when batch producing %v", err)
}
})
t.Run("Return_Error_When_Batch_Producing", func(t *testing.T) {
// Given
o := otelProducer{w: mockOtelKafkaKonsumerWriter{wantErr: true}}
// When
err := o.WriteMessages(context.TODO(), kafka.Message{}, kafka.Message{})
// Then
if err == nil {
t.Fatalf("Success when single producing %v", err)
}
})
}
type mockOtelKafkaKonsumerWriter struct {
wantErr bool
}
var _ OtelKafkaKonsumerWriter = (*mockOtelKafkaKonsumerWriter)(nil)
func (m mockOtelKafkaKonsumerWriter) WriteMessage(_ context.Context, _ kafka.Message) error {
if m.wantErr {
return errors.New("err occurred")
}
return nil
}
func (m mockOtelKafkaKonsumerWriter) WriteMessages(_ context.Context, _ []kafka.Message) error {
if m.wantErr {
return errors.New("err occurred")
}
return nil
}
func (m mockOtelKafkaKonsumerWriter) Close() error {
panic("implement me")
}