-
Notifications
You must be signed in to change notification settings - Fork 121
/
item_test.go
48 lines (40 loc) · 1.29 KB
/
item_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ccache
import (
"math"
"testing"
"time"
"github.com/karlseguin/ccache/v3/assert"
)
func Test_Item_Key(t *testing.T) {
item := &Item[int]{key: "foo"}
assert.Equal(t, item.Key(), "foo")
}
func Test_Item_Promotability(t *testing.T) {
item := &Item[int]{promotions: 4}
assert.Equal(t, item.shouldPromote(5), true)
assert.Equal(t, item.shouldPromote(5), false)
}
func Test_Item_Expired(t *testing.T) {
now := time.Now().UnixNano()
item1 := &Item[int]{expires: now + (10 * int64(time.Millisecond))}
item2 := &Item[int]{expires: now - (10 * int64(time.Millisecond))}
assert.Equal(t, item1.Expired(), false)
assert.Equal(t, item2.Expired(), true)
}
func Test_Item_TTL(t *testing.T) {
now := time.Now().UnixNano()
item1 := &Item[int]{expires: now + int64(time.Second)}
item2 := &Item[int]{expires: now - int64(time.Second)}
assert.Equal(t, int(math.Ceil(item1.TTL().Seconds())), 1)
assert.Equal(t, int(math.Ceil(item2.TTL().Seconds())), -1)
}
func Test_Item_Expires(t *testing.T) {
now := time.Now().UnixNano()
item := &Item[int]{expires: now + (10)}
assert.Equal(t, item.Expires().UnixNano(), now+10)
}
func Test_Item_Extend(t *testing.T) {
item := &Item[int]{expires: time.Now().UnixNano() + 10}
item.Extend(time.Minute * 2)
assert.Equal(t, item.Expires().Unix(), time.Now().Unix()+120)
}