Skip to content

Commit

Permalink
Added a test to verify the reusablility of telemetryInitializer in se…
Browse files Browse the repository at this point in the history
…rvices

Signed-off-by: Corbin Phelps <[email protected]>
  • Loading branch information
Corbin Phelps committed Oct 25, 2022
1 parent 7eadbad commit 212548c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,56 @@ func TestServiceTelemetryCleanupOnError(t *testing.T) {

}

// TestServiceTelemetryReusable tests that a single telemetryInitializer can be reused in multiple services
func TestServiceTelemetryReusable(t *testing.T) {
factories, err := componenttest.NopFactories()
require.NoError(t, err)

// Read valid yaml config from file
validProvider, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
require.NoError(t, err)
validCfg, err := validProvider.Get(context.Background(), factories)
require.NoError(t, err)

// Create a service
telemetry := newColTelemetry(featuregate.NewRegistry())
srvOne, err := newService(&settings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Config: validCfg,
telemetry: telemetry,
})
require.NoError(t, err)

// Start the service
require.NoError(t, srvOne.Start(context.Background()))

// Shutdown the service
require.NoError(t, srvOne.Shutdown(context.Background()))

// Create a new service with the same telemetry
srvTwo, err := newService(&settings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
Config: validCfg,
telemetry: telemetry,
})
require.NoError(t, err)

// For safety ensure everything is cleaned up
t.Cleanup(func() {
assert.NoError(t, telemetry.shutdown())
assert.NoError(t, srvOne.Shutdown(context.Background()))
assert.NoError(t, srvTwo.Shutdown(context.Background()))
})

// Start the new service
require.NoError(t, srvTwo.Start(context.Background()))

// Shutdown the new service
require.NoError(t, srvTwo.Shutdown(context.Background()))
}

func createExampleService(t *testing.T, factories component.Factories) *service {
// Read yaml config from file
prov, err := NewConfigProvider(newDefaultConfigProviderSettings([]string{filepath.Join("testdata", "otelcol-nop.yaml")}))
Expand Down

0 comments on commit 212548c

Please sign in to comment.