forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay_timer_test.go
40 lines (34 loc) · 1.05 KB
/
replay_timer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package filereceiver
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
)
func TestReplayTimer(t *testing.T) {
s := &fakeSleeper{}
timer := &replayTimer{
throttle: 0.5,
sleepFunc: s.fakeSleep,
}
firstMetricTime := time.Date(2020, time.January, 1, 1, 0, 0, 0, time.UTC)
err := timer.wait(context.Background(), pcommon.NewTimestampFromTime(firstMetricTime))
require.NoError(t, err)
secondMetricTime := firstMetricTime.Add(time.Second * 10)
err = timer.wait(context.Background(), pcommon.NewTimestampFromTime(secondMetricTime))
require.NoError(t, err)
err = timer.wait(context.Background(), 0)
require.NoError(t, err)
assert.Equal(t, s.durations, []time.Duration{0, time.Second * 5})
}
type fakeSleeper struct {
durations []time.Duration
}
func (t *fakeSleeper) fakeSleep(_ context.Context, d time.Duration) error {
t.durations = append(t.durations, d)
return nil
}