mirror of
https://github.com/hanzoai/otel-collector.git
synced 2026-10-30 18:26:25 +00:00
### Summary - Cloning Azure Event Hub receiver as from upstream: version 0.144.0 - Changing component name to `signozazureeventhub` from `azureeventhub` in metadata.yaml - Regenerated metadata generated files
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sharedcomponent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.opentelemetry.io/collector/component"
|
|
"go.opentelemetry.io/collector/component/componenttest"
|
|
)
|
|
|
|
var id = component.MustNewID("test")
|
|
|
|
func TestNewSharedComponents(t *testing.T) {
|
|
comps := NewSharedComponents()
|
|
assert.Empty(t, comps.comps)
|
|
}
|
|
|
|
type mockComponent struct {
|
|
component.StartFunc
|
|
component.ShutdownFunc
|
|
}
|
|
|
|
func TestSharedComponents_GetOrAdd(t *testing.T) {
|
|
nop := &mockComponent{}
|
|
createNop := func() component.Component { return nop }
|
|
|
|
comps := NewSharedComponents()
|
|
got := comps.GetOrAdd(id, createNop)
|
|
assert.Len(t, comps.comps, 1)
|
|
assert.Same(t, nop, got.Unwrap())
|
|
assert.Same(t, got, comps.GetOrAdd(id, createNop))
|
|
|
|
// Shutdown nop will remove
|
|
assert.NoError(t, got.Shutdown(t.Context()))
|
|
assert.Empty(t, comps.comps)
|
|
assert.NotSame(t, got, comps.GetOrAdd(id, createNop))
|
|
}
|
|
|
|
func TestSharedComponent(t *testing.T) {
|
|
wantErr := errors.New("my error")
|
|
calledStart := 0
|
|
calledStop := 0
|
|
comp := &mockComponent{
|
|
StartFunc: func(_ context.Context, _ component.Host) error {
|
|
calledStart++
|
|
return wantErr
|
|
},
|
|
ShutdownFunc: func(_ context.Context) error {
|
|
calledStop++
|
|
return wantErr
|
|
},
|
|
}
|
|
createComp := func() component.Component { return comp }
|
|
|
|
comps := NewSharedComponents()
|
|
got := comps.GetOrAdd(id, createComp)
|
|
assert.Equal(t, wantErr, got.Start(t.Context(), componenttest.NewNopHost()))
|
|
assert.Equal(t, 1, calledStart)
|
|
// Second time is not called anymore.
|
|
assert.NoError(t, got.Start(t.Context(), componenttest.NewNopHost()))
|
|
assert.Equal(t, 1, calledStart)
|
|
assert.Equal(t, wantErr, got.Shutdown(t.Context()))
|
|
assert.Equal(t, 1, calledStop)
|
|
// Second time is not called anymore.
|
|
assert.NoError(t, got.Shutdown(t.Context()))
|
|
assert.Equal(t, 1, calledStop)
|
|
}
|