replay_timer_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package filereceiver
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/pdata/pcommon"
  11. )
  12. func TestReplayTimer(t *testing.T) {
  13. s := &fakeSleeper{}
  14. timer := &replayTimer{
  15. throttle: 0.5,
  16. sleepFunc: s.fakeSleep,
  17. }
  18. firstMetricTime := time.Date(2020, time.January, 1, 1, 0, 0, 0, time.UTC)
  19. err := timer.wait(context.Background(), pcommon.NewTimestampFromTime(firstMetricTime))
  20. require.NoError(t, err)
  21. secondMetricTime := firstMetricTime.Add(time.Second * 10)
  22. err = timer.wait(context.Background(), pcommon.NewTimestampFromTime(secondMetricTime))
  23. require.NoError(t, err)
  24. err = timer.wait(context.Background(), 0)
  25. require.NoError(t, err)
  26. assert.Equal(t, s.durations, []time.Duration{0, time.Second * 5})
  27. }
  28. type fakeSleeper struct {
  29. durations []time.Duration
  30. }
  31. func (t *fakeSleeper) fakeSleep(_ context.Context, d time.Duration) error {
  32. t.durations = append(t.durations, d)
  33. return nil
  34. }