wa-lang.org/wazero@v1.0.2/internal/platform/time_test.go (about)

     1  package platform
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"wa-lang.org/wazero/internal/testing/require"
     9  	"wa-lang.org/wazero/sys"
    10  )
    11  
    12  func Test_NewFakeWalltime(t *testing.T) {
    13  	wt := NewFakeWalltime()
    14  
    15  	// Base should be the same as FakeEpochNanos
    16  	sec, nsec := (*wt)(context.Background())
    17  	ft := time.UnixMicro(FakeEpochNanos / time.Microsecond.Nanoseconds()).UTC()
    18  	require.Equal(t, ft, time.Unix(sec, int64(nsec)).UTC())
    19  
    20  	// next reading should increase by 1ms
    21  	sec, nsec = (*wt)(context.Background())
    22  	require.Equal(t, ft.Add(time.Millisecond), time.Unix(sec, int64(nsec)).UTC())
    23  }
    24  
    25  func Test_NewFakeNanotime(t *testing.T) {
    26  	nt := NewFakeNanotime()
    27  
    28  	require.Equal(t, int64(0), (*nt)(context.Background()))
    29  
    30  	// next reading should increase by 1ms
    31  	require.Equal(t, int64(time.Millisecond), (*nt)(context.Background()))
    32  }
    33  
    34  func Test_Walltime(t *testing.T) {
    35  	now := time.Now().Unix()
    36  	sec, nsec := Walltime(context.Background())
    37  
    38  	// Loose test that the second variant is close to now.
    39  	// The only thing that could flake this is a time adjustment during the test.
    40  	require.True(t, now == sec || now == sec-1)
    41  
    42  	// Verify bounds of nanosecond fraction as measuring it precisely won't work.
    43  	require.True(t, nsec >= 0)
    44  	require.True(t, nsec < int32(time.Second.Nanoseconds()))
    45  }
    46  
    47  func Test_Nanotime(t *testing.T) {
    48  	tests := []struct {
    49  		name     string
    50  		nanotime sys.Nanotime
    51  	}{
    52  		{"Nanotime", Nanotime},
    53  		{"nanotimePortable", func(ctx context.Context) int64 {
    54  			return nanotimePortable()
    55  		}},
    56  	}
    57  
    58  	for _, tt := range tests {
    59  		tc := tt
    60  		t.Run(tc.name, func(t *testing.T) {
    61  			delta := time.Since(nanoBase).Nanoseconds()
    62  			nanos := Nanotime(context.Background())
    63  
    64  			// It takes more than a nanosecond to make the two clock readings required
    65  			// to implement time.Now. Hence, delta will always be less than nanos.
    66  			require.True(t, delta <= nanos)
    67  		})
    68  	}
    69  }
    70  
    71  func Test_Nanosleep(t *testing.T) {
    72  	// In CI, Nanosleep(50ms) returned after 197ms.
    73  	// As we can't control the platform clock, we have to be lenient
    74  	ns := int64(50 * time.Millisecond)
    75  	max := ns * 5
    76  
    77  	start := Nanotime(context.Background())
    78  	Nanosleep(context.Background(), ns)
    79  	duration := Nanotime(context.Background()) - start
    80  
    81  	require.True(t, duration > 0 && duration < max, "Nanosleep(%d) slept for %d", ns, duration)
    82  }