github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/lib/hrtime/time_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package hrtime
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  func TestUnixTimerResolution(t *testing.T) {
    15  	res := unix.Timespec{}
    16  	_ = unix.ClockGetres(unix.CLOCK_MONOTONIC, &res)
    17  	t.Logf("Monotonic clock resolution is %d nanoseconds\n", res.Nsec)
    18  }
    19  
    20  func TestNow(t *testing.T) {
    21  	defaultTZOffset := DefaultTimezoneOffset()
    22  	t1 := NowInDefaultTZ()
    23  	t2 := NowIn(TzAsiaShanghaiOffset)
    24  	assert.Equal(t, 0, int(t2.Sub(t1).Minutes()))
    25  	_, tz1 := t1.Zone()
    26  	_, tz2 := t2.Zone()
    27  	assert.Equal(t, int(TzAsiaShanghaiOffset)-defaultTZOffset, tz2-tz1)
    28  }
    29  
    30  func TestMonotonicClock(t *testing.T) {
    31  	startTs := time.Now().UnixNano()
    32  
    33  	time.Sleep(1 * time.Second)
    34  
    35  	elapsedMs := (time.Now().UnixNano() - startTs) / int64(time.Millisecond)
    36  	t.Logf("elapsed ms: %v\n", elapsedMs)
    37  }
    38  
    39  func TestMonotonicClockByUnix(t *testing.T) {
    40  	ts := unix.Timespec{}
    41  	err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts)
    42  	assert.NoError(t, err)
    43  	startTs := ts.Nano()
    44  
    45  	time.Sleep(1 * time.Second)
    46  
    47  	err = unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts)
    48  	assert.NoError(t, err)
    49  	currentTs := ts.Nano()
    50  	elapsedMs := (currentTs - startTs) / int64(time.Millisecond)
    51  	t.Logf("elapsed ms: %v\n", elapsedMs)
    52  }
    53  
    54  func TestNonSysClockTime(t *testing.T) {
    55  	ClockInit()
    56  	t1 := NowInDefaultTZ()
    57  	t.Logf("system clock current time: %v\n", t1)
    58  	time.Sleep(200 * time.Millisecond)
    59  	t2, t3 := GoMonotonicClock.NowInDefaultTZ(), UnixMonotonicClock.NowInDefaultTZ()
    60  	t.Logf("go native non-sys clock current time: %v\n", t2)
    61  	t.Logf("unix non-sys clock current time: %v\n", t3)
    62  	assert.True(t, t2.UnixMilli()-t1.UnixMilli()-5 <= int64(200) && t2.UnixMilli()-t1.UnixMilli()+5 >= int64(200))
    63  	assert.True(t, t3.UnixMilli()-t1.UnixMilli()-5 <= int64(200) && t3.UnixMilli()-t1.UnixMilli()+5 >= int64(200))
    64  	time.Sleep(500 * time.Millisecond)
    65  	t.Logf("go native sys clock current UTC time: %v; asia/shanghai time: %v\n",
    66  		NowInUTC(), NowIn(TzAsiaShanghaiOffset))
    67  	t.Logf("go native non-sys clock current UTC time: %v; asia/shanghai time: %v\n",
    68  		GoMonotonicClock.NowInUTC(), GoMonotonicClock.NowIn(TzAsiaShanghaiOffset))
    69  	t.Logf("unix non-sys clock current UTC time: %v; asia/shanghai time: %v\n",
    70  		UnixMonotonicClock.NowInUTC(), UnixMonotonicClock.NowIn(TzAsiaShanghaiOffset))
    71  	elapsedMs := int64(720)
    72  	assert.GreaterOrEqual(t, elapsedMs, MonotonicElapsed().Milliseconds())
    73  	assert.GreaterOrEqual(t, elapsedMs, GoMonotonicClock.MonotonicElapsed().Milliseconds())
    74  	assert.GreaterOrEqual(t, elapsedMs, UnixMonotonicClock.MonotonicElapsed().Milliseconds())
    75  }