Skip to content

Commit

Permalink
Refactor some new factory func under component/componenttest (#4940)
Browse files Browse the repository at this point in the history
* refactor new factory func

* Update service_test.go

Co-authored-by: Bogdan Drutu <[email protected]>
  • Loading branch information
Haleygo and Bogdan Drutu authored Mar 14, 2022
1 parent 248afd9 commit 317c08e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 77 deletions.
22 changes: 10 additions & 12 deletions component/componenttest/nop_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ type nopExporterConfig struct {
config.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
}

var nopExporterFactory = component.NewExporterFactory(
"nop",
func() config.Exporter {
return &nopExporterConfig{
ExporterSettings: config.NewExporterSettings(config.NewComponentID("nop")),
}
},
component.WithTracesExporter(createTracesExporter),
component.WithMetricsExporter(createMetricsExporter),
component.WithLogsExporter(createLogsExporter))

// NewNopExporterFactory returns a component.ExporterFactory that constructs nop exporters.
func NewNopExporterFactory() component.ExporterFactory {
return nopExporterFactory
return component.NewExporterFactory(
"nop",
func() config.Exporter {
return &nopExporterConfig{
ExporterSettings: config.NewExporterSettings(config.NewComponentID("nop")),
}
},
component.WithTracesExporter(createTracesExporter),
component.WithMetricsExporter(createMetricsExporter),
component.WithLogsExporter(createLogsExporter))
}

func createTracesExporter(context.Context, component.ExporterCreateSettings, config.Exporter) (component.TracesExporter, error) {
Expand Down
22 changes: 10 additions & 12 deletions component/componenttest/nop_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ type nopExtensionConfig struct {
config.ExtensionSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
}

var nopExtensionFactoryInstance = component.NewExtensionFactory(
"nop",
func() config.Extension {
return &nopExtensionConfig{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID("nop")),
}
},
func(context.Context, component.ExtensionCreateSettings, config.Extension) (component.Extension, error) {
return nopExtensionInstance, nil
})

// NewNopExtensionFactory returns a component.ExtensionFactory that constructs nop extensions.
func NewNopExtensionFactory() component.ExtensionFactory {
return nopExtensionFactoryInstance
return component.NewExtensionFactory(
"nop",
func() config.Extension {
return &nopExtensionConfig{
ExtensionSettings: config.NewExtensionSettings(config.NewComponentID("nop")),
}
},
func(context.Context, component.ExtensionCreateSettings, config.Extension) (component.Extension, error) {
return nopExtensionInstance, nil
})
}

var nopExtensionInstance = &nopExtension{}
Expand Down
4 changes: 1 addition & 3 deletions component/componenttest/nop_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import (
// nopHost mocks a receiver.ReceiverHost for test purposes.
type nopHost struct{}

var nopHostInstance component.Host = &nopHost{}

// NewNopHost returns a new instance of nopHost with proper defaults for most tests.
func NewNopHost() component.Host {
return nopHostInstance
return &nopHost{}
}

func (nh *nopHost) ReportFatalError(_ error) {}
Expand Down
24 changes: 11 additions & 13 deletions component/componenttest/nop_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,19 @@ type nopProcessorConfig struct {
config.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
}

var nopProcessorFactory = component.NewProcessorFactory(
"nop",
func() config.Processor {
return &nopProcessorConfig{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID("nop")),
}
},
component.WithTracesProcessor(createTracesProcessor),
component.WithMetricsProcessor(createMetricsProcessor),
component.WithLogsProcessor(createLogsProcessor),
)

// NewNopProcessorFactory returns a component.ProcessorFactory that constructs nop processors.
func NewNopProcessorFactory() component.ProcessorFactory {
return nopProcessorFactory
return component.NewProcessorFactory(
"nop",
func() config.Processor {
return &nopProcessorConfig{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID("nop")),
}
},
component.WithTracesProcessor(createTracesProcessor),
component.WithMetricsProcessor(createMetricsProcessor),
component.WithLogsProcessor(createLogsProcessor),
)
}

func createTracesProcessor(context.Context, component.ProcessorCreateSettings, config.Processor, consumer.Traces) (component.TracesProcessor, error) {
Expand Down
22 changes: 10 additions & 12 deletions component/componenttest/nop_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ type nopReceiverConfig struct {
config.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
}

var nopReceiverFactory = component.NewReceiverFactory(
"nop",
func() config.Receiver {
return &nopReceiverConfig{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID("nop")),
}
},
component.WithTracesReceiver(createTracesReceiver),
component.WithMetricsReceiver(createMetricsReceiver),
component.WithLogsReceiver(createLogsReceiver))

// NewNopReceiverFactory returns a component.ReceiverFactory that constructs nop receivers.
func NewNopReceiverFactory() component.ReceiverFactory {
return nopReceiverFactory
return component.NewReceiverFactory(
"nop",
func() config.Receiver {
return &nopReceiverConfig{
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID("nop")),
}
},
component.WithTracesReceiver(createTracesReceiver),
component.WithMetricsReceiver(createMetricsReceiver),
component.WithLogsReceiver(createLogsReceiver))
}

func createTracesReceiver(context.Context, component.ReceiverCreateSettings, config.Receiver, consumer.Traces) (component.TracesReceiver, error) {
Expand Down
42 changes: 17 additions & 25 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,33 @@ import (
func TestService_GetFactory(t *testing.T) {
factories, err := componenttest.NopFactories()
require.NoError(t, err)

srv := createExampleService(t)
srv := createExampleService(t, factories)

assert.NoError(t, srv.Start(context.Background()))
t.Cleanup(func() {
assert.NoError(t, srv.Shutdown(context.Background()))
})

factory := srv.GetFactory(component.KindReceiver, "nop")
assert.EqualValues(t, factories.Receivers["nop"], factory)
factory = srv.GetFactory(component.KindReceiver, "wrongtype")
assert.EqualValues(t, nil, factory)
assert.Nil(t, srv.GetFactory(component.KindReceiver, "wrongtype"))
assert.Equal(t, factories.Receivers["nop"], srv.GetFactory(component.KindReceiver, "nop"))

factory = srv.GetFactory(component.KindProcessor, "nop")
assert.EqualValues(t, factories.Processors["nop"], factory)
factory = srv.GetFactory(component.KindProcessor, "wrongtype")
assert.EqualValues(t, nil, factory)
assert.Nil(t, srv.GetFactory(component.KindProcessor, "wrongtype"))
assert.Equal(t, factories.Processors["nop"], srv.GetFactory(component.KindProcessor, "nop"))

factory = srv.GetFactory(component.KindExporter, "nop")
assert.EqualValues(t, factories.Exporters["nop"], factory)
factory = srv.GetFactory(component.KindExporter, "wrongtype")
assert.EqualValues(t, nil, factory)
assert.Nil(t, srv.GetFactory(component.KindExporter, "wrongtype"))
assert.Equal(t, factories.Exporters["nop"], srv.GetFactory(component.KindExporter, "nop"))

factory = srv.GetFactory(component.KindExtension, "nop")
assert.EqualValues(t, factories.Extensions["nop"], factory)
factory = srv.GetFactory(component.KindExtension, "wrongtype")
assert.EqualValues(t, nil, factory)
assert.Nil(t, srv.GetFactory(component.KindExtension, "wrongtype"))
assert.Equal(t, factories.Extensions["nop"], srv.GetFactory(component.KindExtension, "nop"))

// Try retrieve non existing component.Kind.
factory = srv.GetFactory(42, "nop")
assert.EqualValues(t, nil, factory)
assert.Nil(t, srv.GetFactory(42, "nop"))
}

func TestService_GetExtensions(t *testing.T) {
srv := createExampleService(t)
factories, err := componenttest.NopFactories()
require.NoError(t, err)
srv := createExampleService(t, factories)

assert.NoError(t, srv.Start(context.Background()))
t.Cleanup(func() {
Expand All @@ -79,7 +71,9 @@ func TestService_GetExtensions(t *testing.T) {
}

func TestService_GetExporters(t *testing.T) {
srv := createExampleService(t)
factories, err := componenttest.NopFactories()
require.NoError(t, err)
srv := createExampleService(t, factories)

assert.NoError(t, srv.Start(context.Background()))
t.Cleanup(func() {
Expand All @@ -96,10 +90,8 @@ func TestService_GetExporters(t *testing.T) {
assert.Contains(t, expMap[config.LogsDataType], config.NewComponentID("nop"))
}

func createExampleService(t *testing.T) *service {
func createExampleService(t *testing.T, factories component.Factories) *service {
// Create some factories.
factories, err := componenttest.NopFactories()
require.NoError(t, err)
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "otelcol-nop.yaml"), factories)
require.NoError(t, err)

Expand Down

0 comments on commit 317c08e

Please sign in to comment.