| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package memorylimiterprocessor |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/component/componenttest" |
| 15 |
"go.opentelemetry.io/collector/consumer/consumertest" |
| 16 |
"go.opentelemetry.io/collector/internal/telemetry" |
| 17 |
"go.opentelemetry.io/collector/internal/telemetry/telemetrytest" |
| 18 |
"go.opentelemetry.io/collector/processor/processortest" |
| 19 |
) |
| 20 |
|
| 21 |
func TestCreateDefaultConfig(t *testing.T) { |
| 22 |
factory := NewFactory() |
| 23 |
require.NotNil(t, factory) |
| 24 |
|
| 25 |
cfg := factory.CreateDefaultConfig() |
| 26 |
assert.NotNil(t, cfg, "failed to create default config") |
| 27 |
assert.NoError(t, componenttest.CheckConfigStruct(cfg)) |
| 28 |
} |
| 29 |
|
| 30 |
func TestCreateProcessor(t *testing.T) { |
| 31 |
factory := NewFactory() |
| 32 |
require.NotNil(t, factory) |
| 33 |
|
| 34 |
cfg := factory.CreateDefaultConfig() |
| 35 |
|
| 36 |
// Create processor with a valid config. |
| 37 |
pCfg := cfg.(*Config) |
| 38 |
pCfg.MemoryLimitMiB = 5722 |
| 39 |
pCfg.MemorySpikeLimitMiB = 1907 |
| 40 |
pCfg.CheckInterval = 100 * time.Millisecond |
| 41 |
|
| 42 |
set := processortest.NewNopSettings(factory.Type()) |
| 43 |
var droppedAttrs []string |
| 44 |
set.Logger = telemetrytest.MockInjectorLogger(set.Logger, &droppedAttrs) |
| 45 |
|
| 46 |
tp, err := factory.CreateTraces(context.Background(), set, cfg, consumertest.NewNop()) |
| 47 |
require.NoError(t, err) |
| 48 |
assert.NotNil(t, tp) |
| 49 |
// test if we can shutdown a monitoring routine that has not started |
| 50 |
require.NoError(t, tp.Shutdown(context.Background())) |
| 51 |
require.NoError(t, tp.Start(context.Background(), componenttest.NewNopHost())) |
| 52 |
|
| 53 |
mp, err := factory.CreateMetrics(context.Background(), set, cfg, consumertest.NewNop()) |
| 54 |
require.NoError(t, err) |
| 55 |
assert.NotNil(t, mp) |
| 56 |
require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost())) |
| 57 |
|
| 58 |
lp, err := factory.CreateLogs(context.Background(), set, cfg, consumertest.NewNop()) |
| 59 |
require.NoError(t, err) |
| 60 |
assert.NotNil(t, lp) |
| 61 |
require.NoError(t, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 62 |
|
| 63 |
pp, err := factory.CreateProfiles(context.Background(), set, cfg, consumertest.NewNop()) |
| 64 |
require.NoError(t, err) |
| 65 |
assert.NotNil(t, pp) |
| 66 |
require.NoError(t, pp.Start(context.Background(), componenttest.NewNopHost())) |
| 67 |
|
| 68 |
// Test that we've dropped the relevant injected attributes exactly once |
| 69 |
assert.ElementsMatch(t, droppedAttrs, []string{ |
| 70 |
telemetry.SignalKey, telemetry.ComponentIDKey, telemetry.PipelineIDKey, |
| 71 |
}) |
| 72 |
|
| 73 |
assert.NoError(t, lp.Shutdown(context.Background())) |
| 74 |
assert.NoError(t, tp.Shutdown(context.Background())) |
| 75 |
assert.NoError(t, mp.Shutdown(context.Background())) |
| 76 |
assert.NoError(t, pp.Shutdown(context.Background())) |
| 77 |
// verify that no monitoring routine is running |
| 78 |
require.NoError(t, tp.Shutdown(context.Background())) |
| 79 |
|
| 80 |
// start and shutdown a new monitoring routine |
| 81 |
assert.NoError(t, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 82 |
assert.NoError(t, lp.Shutdown(context.Background())) |
| 83 |
// calling it again should throw no error |
| 84 |
require.NoError(t, lp.Shutdown(context.Background())) |
| 85 |
} |