1// Copyright The OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package trace
16
17import (
18	"context"
19	"errors"
20	"testing"
21
22	"github.com/stretchr/testify/assert"
23)
24
25type basicSpanProcesor struct {
26	running             bool
27	injectShutdownError error
28}
29
30func (t *basicSpanProcesor) Shutdown(context.Context) error {
31	t.running = false
32	return t.injectShutdownError
33}
34
35func (t *basicSpanProcesor) OnStart(context.Context, ReadWriteSpan) {}
36func (t *basicSpanProcesor) OnEnd(ReadOnlySpan)                     {}
37func (t *basicSpanProcesor) ForceFlush(context.Context) error {
38	return nil
39}
40
41func TestShutdownTraceProvider(t *testing.T) {
42	stp := NewTracerProvider()
43	sp := &basicSpanProcesor{}
44	stp.RegisterSpanProcessor(sp)
45
46	sp.running = true
47
48	_ = stp.Shutdown(context.Background())
49
50	if sp.running != false {
51		t.Errorf("Error shutdown basicSpanProcesor\n")
52	}
53}
54
55func TestFailedProcessorShutdown(t *testing.T) {
56	stp := NewTracerProvider()
57	spErr := errors.New("basic span processor shutdown failure")
58	sp := &basicSpanProcesor{
59		running:             true,
60		injectShutdownError: spErr,
61	}
62	stp.RegisterSpanProcessor(sp)
63
64	err := stp.Shutdown(context.Background())
65	assert.Error(t, err)
66	assert.Equal(t, err, spErr)
67}
68
69func TestFailedProcessorShutdownInUnregister(t *testing.T) {
70	handler.Reset()
71	stp := NewTracerProvider()
72	spErr := errors.New("basic span processor shutdown failure")
73	sp := &basicSpanProcesor{
74		running:             true,
75		injectShutdownError: spErr,
76	}
77	stp.RegisterSpanProcessor(sp)
78	stp.UnregisterSpanProcessor(sp)
79
80	assert.Contains(t, handler.errs, spErr)
81
82	err := stp.Shutdown(context.Background())
83	assert.NoError(t, err)
84}
85