|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package scraper |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "go.opentelemetry.io/collector/component" |
| 16 | + "go.opentelemetry.io/collector/component/componenttest" |
| 17 | + "go.opentelemetry.io/collector/pdata/plog" |
| 18 | +) |
| 19 | + |
| 20 | +func TestNewLogs(t *testing.T) { |
| 21 | + mp, err := NewLogs(newTestScrapeLogsFunc(nil)) |
| 22 | + require.NoError(t, err) |
| 23 | + |
| 24 | + require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost())) |
| 25 | + md, err := mp.ScrapeLogs(context.Background()) |
| 26 | + require.NoError(t, err) |
| 27 | + assert.Equal(t, plog.NewLogs(), md) |
| 28 | + require.NoError(t, mp.Shutdown(context.Background())) |
| 29 | +} |
| 30 | + |
| 31 | +func TestNewLogs_WithOptions(t *testing.T) { |
| 32 | + want := errors.New("my_error") |
| 33 | + mp, err := NewLogs(newTestScrapeLogsFunc(nil), |
| 34 | + WithStart(func(context.Context, component.Host) error { return want }), |
| 35 | + WithShutdown(func(context.Context) error { return want })) |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + assert.Equal(t, want, mp.Start(context.Background(), componenttest.NewNopHost())) |
| 39 | + assert.Equal(t, want, mp.Shutdown(context.Background())) |
| 40 | +} |
| 41 | + |
| 42 | +func TestNewLogs_NilRequiredFields(t *testing.T) { |
| 43 | + _, err := NewLogs(nil) |
| 44 | + require.Error(t, err) |
| 45 | +} |
| 46 | + |
| 47 | +func TestNewLogs_ProcessLogsError(t *testing.T) { |
| 48 | + want := errors.New("my_error") |
| 49 | + mp, err := NewLogs(newTestScrapeLogsFunc(want)) |
| 50 | + require.NoError(t, err) |
| 51 | + _, err = mp.ScrapeLogs(context.Background()) |
| 52 | + require.ErrorIs(t, err, want) |
| 53 | +} |
| 54 | + |
| 55 | +func TestLogsConcurrency(t *testing.T) { |
| 56 | + mp, err := NewLogs(newTestScrapeLogsFunc(nil)) |
| 57 | + require.NoError(t, err) |
| 58 | + require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost())) |
| 59 | + |
| 60 | + var wg sync.WaitGroup |
| 61 | + for i := 0; i < 10; i++ { |
| 62 | + wg.Add(1) |
| 63 | + go func() { |
| 64 | + defer wg.Done() |
| 65 | + for j := 0; j < 10000; j++ { |
| 66 | + _, errScrape := mp.ScrapeLogs(context.Background()) |
| 67 | + assert.NoError(t, errScrape) |
| 68 | + } |
| 69 | + }() |
| 70 | + } |
| 71 | + wg.Wait() |
| 72 | + require.NoError(t, mp.Shutdown(context.Background())) |
| 73 | +} |
| 74 | + |
| 75 | +func newTestScrapeLogsFunc(retError error) ScrapeLogsFunc { |
| 76 | + return func(_ context.Context) (plog.Logs, error) { |
| 77 | + return plog.NewLogs(), retError |
| 78 | + } |
| 79 | +} |
0 commit comments