forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtime_test.go
More file actions
85 lines (65 loc) · 2.69 KB
/
time_test.go
File metadata and controls
85 lines (65 loc) · 2.69 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package metering
import (
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMonoToEpochPrecision(t *testing.T) {
a := time.Now().UnixNano()
b := MonoToEpoch(Monotime())
// Conversion error is less than a millisecond.
assert.True(t, math.Abs(float64(a-b)) < float64(time.Millisecond))
}
func TestEpochToMonoPrecision(t *testing.T) {
a := Monotime()
b := TimeToMono(time.Now())
// Conversion error is less than a millisecond.
assert.Less(t, math.Abs(float64(b-a)), float64(1*time.Millisecond))
}
func TestExtensionsResetDurationProfilerForExtensionsResetWithNoExtensions(t *testing.T) {
mono := Monotime()
profiler := ExtensionsResetDurationProfiler{}
profiler.extensionsResetStartTimeNs = mono
profiler.extensionsResetEndTimeNs = mono + time.Second.Nanoseconds()
profiler.AvailableNs = 3 * time.Second.Nanoseconds()
profiler.NumAgentsRegisteredForShutdown = 0
extensionsResetMs, resetTimeout := profiler.CalculateExtensionsResetMs()
assert.Equal(t, int64(0), extensionsResetMs)
assert.Equal(t, false, resetTimeout)
}
func TestExtensionsResetDurationProfilerForExtensionsResetWithinDeadline(t *testing.T) {
mono := Monotime()
profiler := ExtensionsResetDurationProfiler{}
profiler.extensionsResetStartTimeNs = mono
profiler.extensionsResetEndTimeNs = mono + time.Second.Nanoseconds()
profiler.AvailableNs = 3 * time.Second.Nanoseconds()
profiler.NumAgentsRegisteredForShutdown = 1
extensionsResetMs, resetTimeout := profiler.CalculateExtensionsResetMs()
assert.Equal(t, time.Second.Milliseconds(), extensionsResetMs)
assert.Equal(t, false, resetTimeout)
}
func TestExtensionsResetDurationProfilerForExtensionsResetTimeout(t *testing.T) {
mono := Monotime()
profiler := ExtensionsResetDurationProfiler{}
profiler.extensionsResetStartTimeNs = mono
profiler.extensionsResetEndTimeNs = mono + 3*time.Second.Nanoseconds()
profiler.AvailableNs = time.Second.Nanoseconds()
profiler.NumAgentsRegisteredForShutdown = 1
extensionsResetMs, resetTimeout := profiler.CalculateExtensionsResetMs()
assert.Equal(t, time.Second.Milliseconds(), extensionsResetMs)
assert.Equal(t, true, resetTimeout)
}
func TestExtensionsResetDurationProfilerEndToEnd(t *testing.T) {
profiler := ExtensionsResetDurationProfiler{}
profiler.Start()
time.Sleep(time.Second)
profiler.Stop()
profiler.AvailableNs = 2 * time.Second.Nanoseconds()
profiler.NumAgentsRegisteredForShutdown = 1
extensionsResetMs, _ := profiler.CalculateExtensionsResetMs()
assert.GreaterOrEqual(t, 2*time.Second.Milliseconds(), extensionsResetMs)
assert.LessOrEqual(t, time.Second.Milliseconds(), extensionsResetMs)
}