Path: blob/main/component/otelcol/internal/scheduler/scheduler_test.go
4096 views
package scheduler_test12import (3"context"4"testing"5"time"67"github.com/grafana/agent/component/otelcol/internal/scheduler"8"github.com/grafana/agent/pkg/flow/componenttest"9"github.com/grafana/agent/pkg/util"10"github.com/stretchr/testify/require"11otelcomponent "go.opentelemetry.io/collector/component"12)1314func TestScheduler(t *testing.T) {15t.Run("Scheduled components get started", func(t *testing.T) {16var (17l = util.TestLogger(t)18cs = scheduler.New(l)19h = scheduler.NewHost(l)20)2122// Run our scheduler in the background.23go func() {24err := cs.Run(componenttest.TestContext(t))25require.NoError(t, err)26}()2728// Schedule our component, which should notify the started trigger once it is29// running.30component, started, _ := newTriggerComponent()31cs.Schedule(h, component)32require.NoError(t, started.Wait(5*time.Second), "component did not start")33})3435t.Run("Unscheduled components get stopped", func(t *testing.T) {36var (37l = util.TestLogger(t)38cs = scheduler.New(l)39h = scheduler.NewHost(l)40)4142// Run our scheduler in the background.43go func() {44err := cs.Run(componenttest.TestContext(t))45require.NoError(t, err)46}()4748// Schedule our component, which should notify the started and stopped49// trigger once it starts and stops respectively.50component, started, stopped := newTriggerComponent()51cs.Schedule(h, component)5253// Wait for the component to start, and then unschedule all components, which54// should cause our running component to terminate.55require.NoError(t, started.Wait(5*time.Second), "component did not start")56cs.Schedule(h)57require.NoError(t, stopped.Wait(5*time.Second), "component did not shutdown")58})5960t.Run("Running components get stopped on shutdown", func(t *testing.T) {61var (62l = util.TestLogger(t)63cs = scheduler.New(l)64h = scheduler.NewHost(l)65)6667ctx, cancel := context.WithCancel(componenttest.TestContext(t))68defer cancel()6970// Run our scheduler in the background.71go func() {72err := cs.Run(ctx)73require.NoError(t, err)74}()7576// Schedule our component which will notify our trigger when Shutdown gets77// called.78component, started, stopped := newTriggerComponent()79cs.Schedule(h, component)8081// Wait for the component to start, and then stop our scheduler, which82// should cause our running component to terminate.83require.NoError(t, started.Wait(5*time.Second), "component did not start")84cancel()85require.NoError(t, stopped.Wait(5*time.Second), "component did not shutdown")86})87}8889func newTriggerComponent() (component otelcomponent.Component, started, stopped *util.WaitTrigger) {90started = util.NewWaitTrigger()91stopped = util.NewWaitTrigger()9293component = &fakeComponent{94StartFunc: func(_ context.Context, _ otelcomponent.Host) error {95started.Trigger()96return nil97},98ShutdownFunc: func(_ context.Context) error {99stopped.Trigger()100return nil101},102}103104return105}106107type fakeComponent struct {108StartFunc func(ctx context.Context, host otelcomponent.Host) error109ShutdownFunc func(ctx context.Context) error110}111112var _ otelcomponent.Component = (*fakeComponent)(nil)113114func (fc *fakeComponent) Start(ctx context.Context, host otelcomponent.Host) error {115if fc.StartFunc != nil {116fc.StartFunc(ctx, host)117}118return nil119}120121func (fc *fakeComponent) Shutdown(ctx context.Context) error {122if fc.ShutdownFunc != nil {123return fc.ShutdownFunc(ctx)124}125return nil126}127128129