1// Code created by gotmpl. DO NOT MODIFY.
2// source: internal/shared/otlp/otlptrace/otlptracetest/client.go.tmpl
3
4// Copyright The OpenTelemetry Authors
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18package otlptracetest
19
20import (
21 "context"
22 "errors"
23 "sync"
24 "testing"
25 "time"
26
27 "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
28)
29
30func RunExporterShutdownTest(t *testing.T, factory func() otlptrace.Client) {
31 t.Run("testClientStopHonorsTimeout", func(t *testing.T) {
32 testClientStopHonorsTimeout(t, factory())
33 })
34
35 t.Run("testClientStopHonorsCancel", func(t *testing.T) {
36 testClientStopHonorsCancel(t, factory())
37 })
38
39 t.Run("testClientStopNoError", func(t *testing.T) {
40 testClientStopNoError(t, factory())
41 })
42
43 t.Run("testClientStopManyTimes", func(t *testing.T) {
44 testClientStopManyTimes(t, factory())
45 })
46}
47
48func initializeExporter(t *testing.T, client otlptrace.Client) *otlptrace.Exporter {
49 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
50 defer cancel()
51
52 e, err := otlptrace.New(ctx, client)
53 if err != nil {
54 t.Fatalf("failed to create exporter")
55 }
56
57 return e
58}
59
60func testClientStopHonorsTimeout(t *testing.T, client otlptrace.Client) {
61 t.Cleanup(func() {
62 // The test is looking for a failed shut down. Call Stop a second time
63 // with an un-expired context to give the client a second chance at
64 // cleaning up. There is not guarantee from the Client interface this
65 // will succeed, therefore, no need to check the error (just give it a
66 // best try).
67 _ = client.Stop(context.Background())
68 })
69 e := initializeExporter(t, client)
70
71 ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
72 defer cancel()
73 <-ctx.Done()
74
75 if err := e.Shutdown(ctx); !errors.Is(err, context.DeadlineExceeded) {
76 t.Errorf("expected context DeadlineExceeded error, got %v", err)
77 }
78}
79
80func testClientStopHonorsCancel(t *testing.T, client otlptrace.Client) {
81 t.Cleanup(func() {
82 // The test is looking for a failed shut down. Call Stop a second time
83 // with an un-expired context to give the client a second chance at
84 // cleaning up. There is not guarantee from the Client interface this
85 // will succeed, therefore, no need to check the error (just give it a
86 // best try).
87 _ = client.Stop(context.Background())
88 })
89 e := initializeExporter(t, client)
90
91 ctx, cancel := context.WithCancel(context.Background())
92 cancel()
93
94 if err := e.Shutdown(ctx); !errors.Is(err, context.Canceled) {
95 t.Errorf("expected context canceled error, got %v", err)
96 }
97}
98
99func testClientStopNoError(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 if err := e.Shutdown(ctx); err != nil {
106 t.Errorf("shutdown errored: expected nil, got %v", err)
107 }
108}
109
110func testClientStopManyTimes(t *testing.T, client otlptrace.Client) {
111 e := initializeExporter(t, client)
112
113 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
114 defer cancel()
115
116 ch := make(chan struct{})
117 wg := sync.WaitGroup{}
118 const num int = 20
119 wg.Add(num)
120 errs := make([]error, num)
121 for i := 0; i < num; i++ {
122 go func(idx int) {
123 defer wg.Done()
124 <-ch
125 errs[idx] = e.Shutdown(ctx)
126 }(i)
127 }
128 close(ch)
129 wg.Wait()
130 for _, err := range errs {
131 if err != nil {
132 t.Errorf("failed to shutdown exporter: %v", err)
133 return
134 }
135 }
136}
View as plain text