-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
98 lines (84 loc) · 2.63 KB
/
client_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
package otfranz
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/DoNewsCode/core/config"
"github.com/go-kit/log"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
"github.com/twmb/franz-go/pkg/kgo"
)
func TestClient_ProduceWithTracing(t *testing.T) {
if os.Getenv("KAFKA_ADDR") == "" {
t.Skip("set KAFKA_ADDR to run TestProvideFactory")
return
}
addrs := strings.Split(os.Getenv("KAFKA_ADDR"), ",")
factory, cleanup := provideFactory(factoryIn{
Logger: log.NewNopLogger(),
Conf: config.MapAdapter{"kafka": map[string]interface{}{
"default": map[string]interface{}{
"seed_brokers": addrs,
"default_produce_topic": "tracing",
},
}},
}, func(name string, config *Config) {})
defer cleanup()
cli, err := factory.Make("default")
assert.NoError(t, err)
assert.NotNil(t, cli)
tracer := mocktracer.New()
clientWithTrace := NewClient(cli, tracer)
span, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "test")
record := &kgo.Record{Value: []byte("bar")}
clientWithTrace.ProduceWithTracing(ctx, record, func(r *kgo.Record, err error) {
if err != nil {
t.Fatalf("produce error: %v\n", err)
}
})
time.Sleep(time.Second)
if err := clientWithTrace.ProduceSyncWithTracing(ctx, record).FirstErr(); err != nil {
t.Fatalf("produce sync error: %v\n", err)
}
assert.Len(t, tracer.FinishedSpans(), 2)
span.Finish()
}
func TestClient_ProduceWithOutTracing(t *testing.T) {
if os.Getenv("KAFKA_ADDR") == "" {
t.Skip("set KAFKA_ADDR to run TestProvideFactory")
return
}
addrs := strings.Split(os.Getenv("KAFKA_ADDR"), ",")
factory, cleanup := provideFactory(factoryIn{
Logger: log.NewNopLogger(),
Conf: config.MapAdapter{"kafka": map[string]interface{}{
"default": map[string]interface{}{
"seed_brokers": addrs,
"default_produce_topic": "tracing",
},
}},
}, func(name string, config *Config) {})
defer cleanup()
cli, err := factory.Make("default")
assert.NoError(t, err)
assert.NotNil(t, cli)
clientWithTrace := NewClient(cli, nil)
record := &kgo.Record{Value: []byte("bar")}
tracer := mocktracer.New()
span, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "test")
clientWithTrace.ProduceWithTracing(ctx, record, func(r *kgo.Record, err error) {
if err != nil {
t.Fatalf("produce error: %v\n", err)
}
})
time.Sleep(time.Second)
if err := clientWithTrace.ProduceSyncWithTracing(ctx, record).FirstErr(); err != nil {
t.Fatalf("produce sync error: %v\n", err)
}
assert.Len(t, tracer.FinishedSpans(), 0)
span.Finish()
}