...
1 package platform
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/tetratelabs/wazero/internal/testing/require"
8 "github.com/tetratelabs/wazero/sys"
9 )
10
11 func Test_NewFakeWalltime(t *testing.T) {
12 wt := NewFakeWalltime()
13
14
15 sec, nsec := wt()
16 ft := time.UnixMicro(FakeEpochNanos / time.Microsecond.Nanoseconds()).UTC()
17 require.Equal(t, ft, time.Unix(sec, int64(nsec)).UTC())
18
19
20 sec, nsec = wt()
21 require.Equal(t, ft.Add(time.Millisecond), time.Unix(sec, int64(nsec)).UTC())
22 }
23
24 func Test_NewFakeNanotime(t *testing.T) {
25 nt := NewFakeNanotime()
26
27 require.Equal(t, int64(0), nt())
28
29
30 require.Equal(t, int64(time.Millisecond), nt())
31 }
32
33 func Test_Walltime(t *testing.T) {
34 now := time.Now().Unix()
35 sec, nsec := Walltime()
36
37
38
39 require.True(t, now == sec || now == sec-1)
40
41
42 require.True(t, nsec >= 0)
43 require.True(t, nsec < int32(time.Second.Nanoseconds()))
44 }
45
46 func Test_Nanotime(t *testing.T) {
47 tests := []struct {
48 name string
49 nanotime sys.Nanotime
50 }{
51 {"Nanotime", Nanotime},
52 {"nanotimePortable", nanotimePortable},
53 }
54
55 for _, tt := range tests {
56 tc := tt
57 t.Run(tc.name, func(t *testing.T) {
58 delta := time.Since(nanoBase).Nanoseconds()
59 nanos := Nanotime()
60
61
62
63 require.True(t, delta <= nanos)
64 })
65 }
66 }
67
68 func Test_Nanotime_ensure_monotonic(t *testing.T) {
69 n1 := Nanotime()
70 time.Sleep(time.Millisecond)
71 n2 := Nanotime()
72 require.True(t, n1 < n2)
73 }
74
75 func Test_Nanosleep(t *testing.T) {
76
77
78 ns := int64(50 * time.Millisecond)
79 max := ns * 5
80
81 start := Nanotime()
82 Nanosleep(ns)
83 duration := Nanotime() - start
84
85 require.True(t, duration > 0 && duration < max, "Nanosleep(%d) slept for %d", ns, duration)
86 }
87
View as plain text