-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix ExpireNano overflow * fix flaky test
- Loading branch information
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package clock_test | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Yiling-J/theine-go/internal/clock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestClock_NowNano(t *testing.T) { | ||
c := &clock.Clock{Start: time.Now()} | ||
start := c.NowNano() | ||
time.Sleep(5 * time.Millisecond) | ||
end := c.NowNano() | ||
|
||
require.Greater(t, end, start) | ||
} | ||
|
||
func TestClock_ExpireNano(t *testing.T) { | ||
c := &clock.Clock{Start: time.Now()} | ||
nano := c.NowNano() | ||
|
||
ttl := 1 * time.Second | ||
expireNano := c.ExpireNano(ttl) | ||
lower := nano + ttl.Nanoseconds() | ||
upper := c.NowNano() + ttl.Nanoseconds() | ||
require.Greater(t, expireNano, lower) | ||
require.Less(t, expireNano, upper) | ||
|
||
overflowTTL := time.Duration(math.MaxInt64) | ||
expireNano = c.ExpireNano(overflowTTL) | ||
require.Equal(t, int64(math.MaxInt64), expireNano) | ||
|
||
} | ||
|
||
func TestClock_RefreshNowCache(t *testing.T) { | ||
c := &clock.Clock{Start: time.Now()} | ||
now := c.NowNanoCached() | ||
time.Sleep(5 * time.Millisecond) | ||
require.Equal(t, now, c.NowNanoCached()) | ||
|
||
c.RefreshNowCache() | ||
require.NotEqual(t, c.NowNanoCached(), now) | ||
} | ||
|
||
func TestClock_SetNowCache(t *testing.T) { | ||
c := &clock.Clock{} | ||
c.SetNowCache(123456789) | ||
require.Equal(t, int64(123456789), c.NowNanoCached()) | ||
} | ||
|
||
func TestClock_SetStart(t *testing.T) { | ||
c := &clock.Clock{} | ||
ts := time.Now().UnixNano() | ||
c.SetStart(ts) | ||
require.Equal(t, ts, c.Start.UnixNano()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters