Skip to content

Commit d44e936

Browse files
committed
fix code problem
1 parent 19d4394 commit d44e936

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

utils/now_unix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// Now is a faster method to get current time
1111
func Now() time.Time {
1212
var tv syscall.Timeval
13-
if err := syscall.Gettimeofday(&tv); nil != err {
13+
if err := syscall.Gettimeofday(&tv); err != nil {
1414
// If it failed at syscall, use time package instead
1515
return time.Now()
1616
}

utils/now_unix_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package utils
44

55
import (
66
"fmt"
7-
"os"
87
"testing"
98
"time"
109

@@ -15,13 +14,12 @@ func TestCustomTimeNow(t *testing.T) {
1514
precision := time.Millisecond
1615

1716
for i := 0; i < 1000; i++ {
18-
timestamp := time.Now().UnixNano()
19-
customTimestamp := Now().UnixNano()
17+
timestamp := time.Now()
18+
customTimestamp := Now()
2019

2120
// two timestamp should within 1 percistion
22-
assert.Equal(t, timestamp+int64(precision) >= customTimestamp && timestamp-int64(precision) <= customTimestamp, true, fmt.Sprintf("Loop: %d: customTimestamp should within %s. timestamp: %d, customTimestamp: %d", i, precision.String(), timestamp, customTimestamp))
21+
assert.WithinDuration(t, timestamp, customTimestamp, precision, fmt.Sprintf("Loop: %d: customTimestamp should within %s. timestamp: %d, customTimestamp: %d", i, precision.String(), timestamp.UnixNano(), customTimestamp.UnixNano()))
2322

24-
os.Setenv("TZ", fmt.Sprintf("UTC%d", 14-i%27))
2523
time.Sleep(time.Nanosecond)
2624
}
2725
}

0 commit comments

Comments
 (0)