Raj Kamal Singh 0ad5d67140
QS: Collector simulator (#3656)
* feat: get collectorsimulator started and add inmemoryreceiver

* feat: add collectorsimulator/inmemoryexporter

* feat: add collectorsimulator.SimulateLogsProcessing

* chore: clean up collector simulator code a little

* chore: update go.sum entries for cors

* chore: add collectorsimulator tests to make cmd

* chore: move to latest dependency version for collectorsimulator

* chore: revert to dependency versions matching signoz-otel-col

* chore: cleanup: reorganize collectorsimulator logic

* chore: some more cleanup

* chore: some more cleanup

* chore: some more cleanup

* chore: redo go.mod
2023-10-05 14:27:41 +05:30

69 lines
2.5 KiB
Go

package inmemoryreceiver
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver"
)
func TestReceiverLifecycle(t *testing.T) {
require := require.New(t)
testReceiverId := uuid.NewString()
// Should be able to get a hold of the receiver after starting it.
require.Nil(GetReceiverInstance(testReceiverId), "receiver instance should not exist before Start()")
constructed, err := makeTestLogReceiver(testReceiverId)
require.Nil(err, "could not make test receiver")
err = constructed.Start(context.Background(), componenttest.NewNopHost())
require.Nil(err, "could not start test receiver")
testReceiver := GetReceiverInstance(testReceiverId)
require.NotNil(testReceiver, "could not get receiver instance by Id")
// Should not be able to start 2 receivers with the same id
constructed2, err := makeTestLogReceiver(testReceiverId)
require.Nil(err, "could not create second receiver with same id")
err = constructed2.Start(context.Background(), componenttest.NewNopHost())
require.NotNil(err, "should not be able to start another receiver with same id before shutting down the previous one")
// Should not be able to get a hold of an receiver after shutdown
testReceiver.Shutdown(context.Background())
require.Nil(GetReceiverInstance(testReceiverId), "should not be able to find inmemory receiver after shutdown")
// Should be able to start a new receiver with same id after shutting down
constructed3, err := makeTestLogReceiver(testReceiverId)
require.Nil(err, "could not make receiver with same Id after shutting down old one")
err = constructed3.Start(context.Background(), componenttest.NewNopHost())
require.Nil(err, "should be able to start another receiver with same id after shutting down the previous one")
testReceiver3 := GetReceiverInstance(testReceiverId)
require.NotNil(testReceiver3, "could not get receiver instance by Id")
testReceiver3.Shutdown(context.Background())
require.Nil(GetReceiverInstance(testReceiverId))
}
func makeTestLogReceiver(receiverId string) (receiver.Logs, error) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
component.UnmarshalConfig(confmap.NewFromStringMap(
map[string]interface{}{"id": receiverId}), cfg,
)
return factory.CreateLogsReceiver(
context.Background(), receiver.CreateSettings{}, cfg, consumertest.NewNop(),
)
}