| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package memorylimiterextension |
| 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" |
| 15 |
"go.opentelemetry.io/collector/component/componenttest" |
| 16 |
"go.opentelemetry.io/collector/extension/extensiontest" |
| 17 |
) |
| 18 |
|
| 19 |
func TestCreateDefaultConfig(t *testing.T) { |
| 20 |
factory := NewFactory() |
| 21 |
require.NotNil(t, factory) |
| 22 |
|
| 23 |
cfg := factory.CreateDefaultConfig() |
| 24 |
assert.NotNil(t, cfg, "failed to create default config") |
| 25 |
assert.NoError(t, componenttest.CheckConfigStruct(cfg)) |
| 26 |
} |
| 27 |
|
| 28 |
func TestCreate(t *testing.T) { |
| 29 |
factory := NewFactory() |
| 30 |
require.NotNil(t, factory) |
| 31 |
|
| 32 |
cfg := factory.CreateDefaultConfig() |
| 33 |
|
| 34 |
// Create extension with a valid config. |
| 35 |
pCfg := cfg.(*Config) |
| 36 |
pCfg.MemoryLimitMiB = 5722 |
| 37 |
pCfg.MemorySpikeLimitMiB = 1907 |
| 38 |
pCfg.CheckInterval = 100 * time.Millisecond |
| 39 |
|
| 40 |
set := extensiontest.NewNopSettings(factory.Type()) |
| 41 |
set.ID = component.NewID(factory.Type()) |
| 42 |
|
| 43 |
tp, err := factory.Create(context.Background(), set, cfg) |
| 44 |
require.NoError(t, err) |
| 45 |
assert.NotNil(t, tp) |
| 46 |
// test if we can shutdown a monitoring routine that has not started |
| 47 |
require.NoError(t, tp.Shutdown(context.Background())) |
| 48 |
assert.NoError(t, tp.Start(context.Background(), componenttest.NewNopHost())) |
| 49 |
|
| 50 |
assert.NoError(t, tp.Shutdown(context.Background())) |
| 51 |
// verify that shutdown twice works: |
| 52 |
assert.NoError(t, tp.Shutdown(context.Background())) |
| 53 |
} |