Debian Codegraph @

exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest/client.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/otlptrace/otlptracetest/client.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package otlptracetest // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest"
8
9 import (
10 "context"
11 "errors"
12 "sync"
13 "testing"
14 "time"
15
16 "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
17 )
18
19 func RunExporterShutdownTest(t *testing.T, factory func() otlptrace.Client) {
20 t.Run("testClientStopHonorsTimeout", func(t *testing.T) {
21 testClientStopHonorsTimeout(t, factory())
22 })
23
24 t.Run("testClientStopHonorsCancel", func(t *testing.T) {
25 testClientStopHonorsCancel(t, factory())
26 })
27
28 t.Run("testClientStopNoError", func(t *testing.T) {
29 testClientStopNoError(t, factory())
30 })
31
32 t.Run("testClientStopManyTimes", func(t *testing.T) {
33 testClientStopManyTimes(t, factory())
34 })
35 }
36
37 func initializeExporter(t *testing.T, client otlptrace.Client) *otlptrace.Exporter {
38 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
39 defer cancel()
40
41 e, err := otlptrace.New(ctx, client)
42 if err != nil {
43 t.Fatalf("failed to create exporter")
44 }
45
46 return e
47 }
48
49 func testClientStopHonorsTimeout(t *testing.T, client otlptrace.Client) {
50 t.Cleanup(func() {
51 // The test is looking for a failed shut down. Call Stop a second time
52 // with an un-expired context to give the client a second chance at
53 // cleaning up. There is not guarantee from the Client interface this
54 // will succeed, therefore, no need to check the error (just give it a
55 // best try).
56 _ = client.Stop(context.Background())
57 })
58 e := initializeExporter(t, client)
59
60 ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
61 defer cancel()
62 <-ctx.Done()
63
64 if err := e.Shutdown(ctx); !errors.Is(err, context.DeadlineExceeded) {
65 t.Errorf("expected context DeadlineExceeded error, got %v", err)
66 }
67 }
68
69 func testClientStopHonorsCancel(t *testing.T, client otlptrace.Client) {
70 t.Cleanup(func() {
71 // The test is looking for a failed shut down. Call Stop a second time
72 // with an un-expired context to give the client a second chance at
73 // cleaning up. There is not guarantee from the Client interface this
74 // will succeed, therefore, no need to check the error (just give it a
75 // best try).
76 _ = client.Stop(context.Background())
77 })
78 e := initializeExporter(t, client)
79
80 ctx, cancel := context.WithCancel(context.Background())
81 cancel()
82
83 if err := e.Shutdown(ctx); !errors.Is(err, context.Canceled) {
84 t.Errorf("expected context canceled error, got %v", err)
85 }
86 }
87
88 func testClientStopNoError(t *testing.T, client otlptrace.Client) {
89 e := initializeExporter(t, client)
90
91 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
92 defer cancel()
93
94 if err := e.Shutdown(ctx); err != nil {
95 t.Errorf("shutdown errored: expected nil, got %v", err)
96 }
97 }
98
99 func testClientStopManyTimes(t *testing.T, client otlptrace.Client) {
100 e := initializeExporter(t, client)
101
102 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
103 defer cancel()
104
105 ch := make(chan struct{})
106 wg := sync.WaitGroup{}
107 const num int = 20
108 wg.Add(num)
109 errs := make([]error, num)
110 for i := range num {
111 go func(idx int) {
112 defer wg.Done()
113 <-ch
114 errs[idx] = e.Shutdown(ctx)
115 }(i)
116 }
117 close(ch)
118 wg.Wait()
119 for _, err := range errs {
120 if err != nil {
121 t.Errorf("failed to shutdown exporter: %v", err)
122 return
123 }
124 }
125 }