Debian Codegraph @

internal/sharedcomponent/sharedcomponent_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package sharedcomponent
5
6 import (
7 "context"
8 "errors"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 "go.opentelemetry.io/collector/component"
15 "go.opentelemetry.io/collector/component/componentstatus"
16 "go.opentelemetry.io/collector/component/componenttest"
17 )
18
19 var id = component.MustNewID("test")
20
21 type baseComponent struct {
22 component.StartFunc
23 component.ShutdownFunc
24 }
25
26 func TestNewMap(t *testing.T) {
27 comps := NewMap[component.ID, *baseComponent]()
28 assert.Empty(t, comps.components)
29 }
30
31 func TestNewSharedComponentsCreateError(t *testing.T) {
32 comps := NewMap[component.ID, *baseComponent]()
33 assert.Empty(t, comps.components)
34 myErr := errors.New("my error")
35 _, err := comps.LoadOrStore(
36 id,
37 func() (*baseComponent, error) { return nil, myErr },
38 )
39 require.ErrorIs(t, err, myErr)
40 assert.Empty(t, comps.components)
41 }
42
43 func TestSharedComponentsLoadOrStore(t *testing.T) {
44 nop := &baseComponent{}
45
46 comps := NewMap[component.ID, *baseComponent]()
47 got, err := comps.LoadOrStore(
48 id,
49 func() (*baseComponent, error) { return nop, nil },
50 )
51 require.NoError(t, err)
52 assert.Len(t, comps.components, 1)
53 assert.Same(t, nop, got.Unwrap())
54 gotSecond, err := comps.LoadOrStore(
55 id,
56 func() (*baseComponent, error) { panic("should not be called") },
57 )
58
59 require.NoError(t, err)
60 assert.Same(t, got, gotSecond)
61
62 // Shutdown nop will remove
63 require.NoError(t, got.Shutdown(context.Background()))
64 assert.Empty(t, comps.components)
65 gotThird, err := comps.LoadOrStore(
66 id,
67 func() (*baseComponent, error) { return nop, nil },
68 )
69 require.NoError(t, err)
70 assert.NotSame(t, got, gotThird)
71 }
72
73 func TestSharedComponent(t *testing.T) {
74 wantErr := errors.New("my error")
75 calledStart := 0
76 calledStop := 0
77 comp := &baseComponent{
78 StartFunc: func(context.Context, component.Host) error {
79 calledStart++
80 return wantErr
81 },
82 ShutdownFunc: func(context.Context) error {
83 calledStop++
84 return wantErr
85 },
86 }
87
88 comps := NewMap[component.ID, *baseComponent]()
89 got, err := comps.LoadOrStore(
90 id,
91 func() (*baseComponent, error) { return comp, nil },
92 )
93 require.NoError(t, err)
94 assert.Equal(t, wantErr, got.Start(context.Background(), componenttest.NewNopHost()))
95 assert.Equal(t, 1, calledStart)
96 // Second time is not called anymore.
97 require.NoError(t, got.Start(context.Background(), componenttest.NewNopHost()))
98 assert.Equal(t, 1, calledStart)
99 // first time, shutdown is called.
100 assert.Equal(t, wantErr, got.Shutdown(context.Background()))
101 assert.Equal(t, 1, calledStop)
102 // Second time is not called anymore.
103 require.NoError(t, got.Shutdown(context.Background()))
104 assert.Equal(t, 1, calledStop)
105 }
106
107 func TestReportStatusOnStartShutdown(t *testing.T) {
108 for _, tc := range []struct {
109 name string
110 startErr error
111 shutdownErr error
112 expectedStatuses []componentstatus.Status
113 expectedNumReporterInstances int
114 }{
115 {
116 name: "successful start/stop",
117 startErr: nil,
118 shutdownErr: nil,
119 expectedStatuses: []componentstatus.Status{
120 componentstatus.StatusStarting,
121 componentstatus.StatusOK,
122 componentstatus.StatusStopping,
123 componentstatus.StatusStopped,
124 },
125 expectedNumReporterInstances: 3,
126 },
127 {
128 name: "start error",
129 startErr: assert.AnError,
130 shutdownErr: nil,
131 expectedStatuses: []componentstatus.Status{
132 componentstatus.StatusStarting,
133 componentstatus.StatusPermanentError,
134 },
135 expectedNumReporterInstances: 1,
136 },
137 {
138 name: "shutdown error",
139 shutdownErr: assert.AnError,
140 expectedStatuses: []componentstatus.Status{
141 componentstatus.StatusStarting,
142 componentstatus.StatusOK,
143 componentstatus.StatusStopping,
144 componentstatus.StatusPermanentError,
145 },
146 expectedNumReporterInstances: 3,
147 },
148 } {
149 t.Run(tc.name, func(t *testing.T) {
150 reportedStatuses := make(map[*componentstatus.InstanceID][]componentstatus.Status)
151 newStatusFunc := func(id *componentstatus.InstanceID, ev *componentstatus.Event) {
152 reportedStatuses[id] = append(reportedStatuses[id], ev.Status())
153 }
154 base := &baseComponent{}
155 if tc.startErr != nil {
156 base.StartFunc = func(context.Context, component.Host) error {
157 return tc.startErr
158 }
159 }
160 if tc.shutdownErr != nil {
161 base.ShutdownFunc = func(context.Context) error {
162 return tc.shutdownErr
163 }
164 }
165 comps := NewMap[component.ID, *baseComponent]()
166 var comp *Component[*baseComponent]
167 var err error
168 for range 3 {
169 comp, err = comps.LoadOrStore(
170 id,
171 func() (*baseComponent, error) { return base, nil },
172 )
173 require.NoError(t, err)
174 }
175
176 baseHost := componenttest.NewNopHost()
177 for range 3 {
178 err = comp.Start(context.Background(), &testHost{Host: baseHost, InstanceID: &componentstatus.InstanceID{}, newStatusFunc: newStatusFunc})
179 if err != nil {
180 break
181 }
182 }
183
184 require.Equal(t, tc.startErr, err)
185
186 if tc.startErr == nil {
187 comp.hostWrapper.Report(componentstatus.NewEvent(componentstatus.StatusOK))
188
189 err = comp.Shutdown(context.Background())
190 require.Equal(t, tc.shutdownErr, err)
191 }
192
193 require.Len(t, reportedStatuses, tc.expectedNumReporterInstances)
194
195 for _, actualStatuses := range reportedStatuses {
196 require.Equal(t, tc.expectedStatuses, actualStatuses)
197 }
198 })
199 }
200 }
201
202 var (
203 _ component.Host = (*testHost)(nil)
204 _ componentstatus.Reporter = (*testHost)(nil)
205 )
206
207 type testHost struct {
208 component.Host
209 *componentstatus.InstanceID
210 newStatusFunc func(id *componentstatus.InstanceID, ev *componentstatus.Event)
211 }
212
213 func (h *testHost) Report(e *componentstatus.Event) {
214 h.newStatusFunc(h.InstanceID, e)
215 }