diff --git a/receiver/solacereceiver/factory_test.go b/receiver/solacereceiver/factory_test.go index af6c09527a2a..402cf436319a 100644 --- a/receiver/solacereceiver/factory_test.go +++ b/receiver/solacereceiver/factory_test.go @@ -30,9 +30,11 @@ func TestCreateTracesReceiver(t *testing.T) { require.NoError(t, err) require.NoError(t, component.UnmarshalConfig(sub, cfg)) + set := receivertest.NewNopCreateSettings() + set.ID = component.NewIDWithName("solace", "factory") receiver, err := factory.CreateTracesReceiver( context.Background(), - receivertest.NewNopCreateSettings(), + set, cfg, consumertest.NewNop(), ) @@ -92,10 +94,11 @@ func TestCreateTracesReceiverBadMetrics(t *testing.T) { sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "primary").String()) require.NoError(t, err) require.NoError(t, component.UnmarshalConfig(sub, cfg)) - + set := receivertest.NewNopCreateSettings() + set.ID = component.NewIDWithName("solace", "factory") receiver, err := factory.CreateTracesReceiver( context.Background(), - receivertest.NewNopCreateSettings(), + set, cfg, consumertest.NewNop(), ) diff --git a/receiver/solacereceiver/generated_component_test.go b/receiver/solacereceiver/generated_component_test.go new file mode 100644 index 000000000000..0c78d44b8d74 --- /dev/null +++ b/receiver/solacereceiver/generated_component_test.go @@ -0,0 +1,89 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package solacereceiver + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/component/componenttest" + + "go.opentelemetry.io/collector/consumer/consumertest" + "go.opentelemetry.io/collector/receiver" + "go.opentelemetry.io/collector/receiver/receivertest" + + "go.opentelemetry.io/collector/confmap/confmaptest" +) + +// assertNoErrorHost implements a component.Host that asserts that there were no errors. +type assertNoErrorHost struct { + component.Host + *testing.T +} + +var _ component.Host = (*assertNoErrorHost)(nil) + +// newAssertNoErrorHost returns a new instance of assertNoErrorHost. +func newAssertNoErrorHost(t *testing.T) component.Host { + return &assertNoErrorHost{ + componenttest.NewNopHost(), + t, + } +} + +func (aneh *assertNoErrorHost) ReportFatalError(err error) { + assert.NoError(aneh, err) +} + +func TestComponentLifecycle(t *testing.T) { + factory := NewFactory() + + tests := []struct { + name string + createFn func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) + }{ + + { + name: "traces", + createFn: func(ctx context.Context, set receiver.CreateSettings, cfg component.Config) (component.Component, error) { + return factory.CreateTracesReceiver(ctx, set, cfg, consumertest.NewNop()) + }, + }, + } + + cm, err := confmaptest.LoadConf("metadata.yaml") + require.NoError(t, err) + cfg := factory.CreateDefaultConfig() + sub, err := cm.Sub("tests::config") + require.NoError(t, err) + require.NoError(t, component.UnmarshalConfig(sub, cfg)) + + for _, test := range tests { + t.Run(test.name+"-shutdown", func(t *testing.T) { + c, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + err = c.Shutdown(context.Background()) + require.NoError(t, err) + }) + + t.Run(test.name+"-lifecycle", func(t *testing.T) { + + // TODO support lifecycle + t.SkipNow() + + firstRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + host := newAssertNoErrorHost(t) + require.NoError(t, err) + require.NoError(t, firstRcvr.Start(context.Background(), host)) + require.NoError(t, firstRcvr.Shutdown(context.Background())) + secondRcvr, err := test.createFn(context.Background(), receivertest.NewNopCreateSettings(), cfg) + require.NoError(t, err) + require.NoError(t, secondRcvr.Start(context.Background(), host)) + require.NoError(t, secondRcvr.Shutdown(context.Background())) + }) + } +} diff --git a/receiver/solacereceiver/metadata.yaml b/receiver/solacereceiver/metadata.yaml index 5b858e00752b..d724eacd2b06 100644 --- a/receiver/solacereceiver/metadata.yaml +++ b/receiver/solacereceiver/metadata.yaml @@ -7,3 +7,11 @@ status: distributions: [contrib, sumo, splunk] codeowners: active: [djaglowski, mcardy] + +tests: + config: + auth: + sasl_plain: + username: username + password: passwd + skip_lifecycle: true \ No newline at end of file diff --git a/receiver/solacereceiver/receiver.go b/receiver/solacereceiver/receiver.go index fc51b0673912..70ad59598371 100644 --- a/receiver/solacereceiver/receiver.go +++ b/receiver/solacereceiver/receiver.go @@ -89,10 +89,13 @@ func (s *solaceTracesReceiver) Start(_ context.Context, _ component.Host) error // Shutdown implements component.Receiver::Shutdown func (s *solaceTracesReceiver) Shutdown(_ context.Context) error { + if s.cancel == nil { + return nil + } s.terminating.Store(true) s.metrics.recordReceiverStatus(receiverStateTerminating) s.settings.Logger.Info("Shutdown waiting for all components to complete") - s.cancel() // cancels the context passed to the reconneciton loop + s.cancel() // cancels the context passed to the reconnection loop s.shutdownWaitGroup.Wait() s.settings.Logger.Info("Receiver shutdown successfully") s.metrics.recordReceiverStatus(receiverStateTerminated)