| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xscraper |
| 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/pprofile" |
| 18 |
) |
| 19 |
|
| 20 |
func TestNewProfiles(t *testing.T) { |
| 21 |
mp, err := NewProfiles(newTestScrapeProfilesFunc(nil)) |
| 22 |
require.NoError(t, err) |
| 23 |
|
| 24 |
require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost())) |
| 25 |
md, err := mp.ScrapeProfiles(context.Background()) |
| 26 |
require.NoError(t, err) |
| 27 |
assert.Equal(t, pprofile.NewProfiles(), md) |
| 28 |
require.NoError(t, mp.Shutdown(context.Background())) |
| 29 |
} |
| 30 |
|
| 31 |
func TestNewProfiles_WithOptions(t *testing.T) { |
| 32 |
want := errors.New("my_error") |
| 33 |
mp, err := NewProfiles(newTestScrapeProfilesFunc(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 TestNewProfiles_NilRequiredFields(t *testing.T) { |
| 43 |
_, err := NewProfiles(nil) |
| 44 |
require.Error(t, err) |
| 45 |
} |
| 46 |
|
| 47 |
func TestNewProfiles_ProcessProfilesError(t *testing.T) { |
| 48 |
want := errors.New("my_error") |
| 49 |
mp, err := NewProfiles(newTestScrapeProfilesFunc(want)) |
| 50 |
require.NoError(t, err) |
| 51 |
_, err = mp.ScrapeProfiles(context.Background()) |
| 52 |
require.ErrorIs(t, err, want) |
| 53 |
} |
| 54 |
|
| 55 |
func TestProfilesConcurrency(t *testing.T) { |
| 56 |
mp, err := NewProfiles(newTestScrapeProfilesFunc(nil)) |
| 57 |
require.NoError(t, err) |
| 58 |
require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost())) |
| 59 |
|
| 60 |
var wg sync.WaitGroup |
| 61 |
for range 10 { |
| 62 |
wg.Add(1) |
| 63 |
go func() { |
| 64 |
defer wg.Done() |
| 65 |
for range 10000 { |
| 66 |
_, errScrape := mp.ScrapeProfiles(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 newTestScrapeProfilesFunc(retError error) ScrapeProfilesFunc { |
| 76 |
return func(_ context.Context) (pprofile.Profiles, error) { |
| 77 |
return pprofile.NewProfiles(), retError |
| 78 |
} |
| 79 |
} |