-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer_publish_tracer_test.go
73 lines (60 loc) · 1.73 KB
/
consumer_publish_tracer_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
package tests
import (
"context"
"testing"
"time"
"github.com/google/uuid"
"github.com/pmorelli92/bunnify/bunnify"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.uber.org/goleak"
)
func TestConsumerPublisherTracing(t *testing.T) {
// Setup tracing
otel.SetTracerProvider(tracesdk.NewTracerProvider())
otel.SetTextMapPropagator(propagation.TraceContext{})
// Setup amqp
queueName := uuid.NewString()
exchangeName := uuid.NewString()
routingKey := uuid.NewString()
connection := bunnify.NewConnection()
if err := connection.Start(); err != nil {
t.Fatal(err)
}
// Exercise consuming
var actualTraceID trace.TraceID
eventHandler := func(ctx context.Context, _ bunnify.ConsumableEvent[any]) error {
actualTraceID = trace.SpanFromContext(ctx).SpanContext().TraceID()
return nil
}
consumer := connection.NewConsumer(
queueName,
bunnify.WithBindingToExchange(exchangeName),
bunnify.WithHandler(routingKey, eventHandler))
if err := consumer.Consume(); err != nil {
t.Fatal(err)
}
// Exercise publishing
publisher := connection.NewPublisher()
publishingContext, _ := otel.Tracer("amqp").Start(context.Background(), "publish-test")
err := publisher.Publish(
publishingContext,
exchangeName,
routingKey,
bunnify.NewPublishableEvent(struct{}{}))
if err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
if err := connection.Close(); err != nil {
t.Fatal(err)
}
// Assert
publishingTraceID := trace.SpanFromContext(publishingContext).SpanContext().TraceID()
if actualTraceID != publishingTraceID {
t.Fatalf("expected traceID %s, got %s", publishingTraceID, actualTraceID)
}
goleak.VerifyNone(t)
}