-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_test.go
152 lines (117 loc) · 5.06 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package hot
import (
"testing"
"time"
"github.com/samber/hot/internal"
"github.com/stretchr/testify/assert"
)
func TestNewItem(t *testing.T) {
is := assert.New(t)
// no value without ttl
got := newItem[int64](0, false, 0, 0)
is.EqualValues(&item[int64]{false, 0, 0, 0, 0}, got)
got = newItem[int64](42, false, 0, 0)
is.EqualValues(&item[int64]{false, 0, 0, 0, 0}, got)
// no value with ttl
got = newItem[int64](0, false, 2_000, 1_000)
is.False(got.hasValue)
is.Equal(int64(0), got.value)
is.InEpsilon(internal.NowMicro()+2_000, got.expiryMicro, 100)
is.InEpsilon(internal.NowMicro()+2_000+1_000, got.staleExpiryMicro, 100)
// has value without ttl
is.EqualValues(&item[int64]{true, 42, 8, 0, 0}, newItem[int64](42, true, 0, 0))
// has value with ttl
got = newItem[int64](42, true, 2_000, 1_000)
is.True(got.hasValue)
is.Equal(int64(42), got.value)
is.InEpsilon(internal.NowMicro()+2_000, got.expiryMicro, 100)
is.InEpsilon(internal.NowMicro()+2_000+1_000, got.staleExpiryMicro, 100)
// size
is.EqualValues(&item[map[string]int]{true, map[string]int{"a": 1, "b": 2}, 79, 0, 0}, newItem(map[string]int{"a": 1, "b": 2}, true, 0, 0))
is.EqualValues(&item[*item[int64]]{true, &item[int64]{false, 0, 0, 0, 0}, 40, 0, 0}, newItem(newItem[int64](42, false, 0, 0), true, 0, 0))
}
func TestNewItemWithValue(t *testing.T) {
is := assert.New(t)
is.Equal(&item[int64]{true, int64(42), 8, 0, 0}, newItemWithValue(int64(42), 0, 0))
item := newItemWithValue(int64(42), 2_000, 1_000)
is.True(item.hasValue)
is.Equal(int64(42), item.value)
is.InEpsilon(internal.NowMicro()+2_000, item.expiryMicro, 100)
is.InEpsilon(internal.NowMicro()+2_000+1_000, item.staleExpiryMicro, 100)
}
func TestNewItemNoValue(t *testing.T) {
is := assert.New(t)
is.Equal(&item[int64]{false, 0, 0, 0, 0}, newItemNoValue[int64](0, 0))
item := newItemNoValue[int](2_000_000, 1_000_000)
is.False(item.hasValue)
is.Equal(0, item.value)
is.InEpsilon(internal.NowMicro()+2_000, item.expiryMicro, 100)
is.InEpsilon(internal.NowMicro()+2_000+1_000, item.staleExpiryMicro, 100)
}
func TestItem_isExpired(t *testing.T) {
is := assert.New(t)
got := newItemNoValue[int64](0, 0)
is.False(got.isExpired(internal.NowMicro()))
got = newItemNoValue[int64](-1_000, 0)
is.True(got.isExpired(internal.NowMicro()))
got = newItemNoValue[int64](1_000, 0)
is.False(got.isExpired(internal.NowMicro()))
got = newItemNoValue[int64](-1_000, 800)
is.True(got.isExpired(internal.NowMicro()))
got = newItemNoValue[int64](-1_000, 1_200)
is.False(got.isExpired(internal.NowMicro()))
}
func TestItem_shouldRevalidate(t *testing.T) {
is := assert.New(t)
got := newItemNoValue[int64](0, 0)
is.False(got.shouldRevalidate(internal.NowMicro()))
got = newItemNoValue[int64](-1_000, 0)
is.False(got.shouldRevalidate(internal.NowMicro()))
got = newItemNoValue[int64](1_000, 0)
is.False(got.shouldRevalidate(internal.NowMicro()))
got = newItemNoValue[int64](-1_000, 800)
is.False(got.shouldRevalidate(internal.NowMicro()))
got = newItemNoValue[int64](-1_000, 1_200)
is.True(got.shouldRevalidate(internal.NowMicro()))
}
func TestItemMapsToValues(t *testing.T) {
is := assert.New(t)
twice := func(i int) int { return i * 2 }
itemNo := newItem(0, false, 0, 0)
itemA := newItem(42, true, 0, 0)
itemB := newItem(21, true, 0, 0)
// no map
gotFound, gotMissing := itemMapsToValues[string, int](nil)
is.EqualValues(map[string]int{}, gotFound)
is.EqualValues([]string{}, gotMissing)
// no map
gotFound, gotMissing = itemMapsToValues[string, int](twice)
is.EqualValues(map[string]int{}, gotFound)
is.EqualValues([]string{}, gotMissing)
// has map
gotFound, gotMissing = itemMapsToValues[string, int](nil, map[string]*item[int]{"a": itemA, "b": itemB, "c": itemNo})
is.EqualValues(map[string]int{"a": 42, "b": 21}, gotFound)
is.EqualValues([]string{"c"}, gotMissing)
gotFound, gotMissing = itemMapsToValues[string, int](nil, map[string]*item[int]{"a": itemA}, map[string]*item[int]{"b": itemB, "c": itemNo, "a": itemNo})
is.EqualValues(map[string]int{"a": 42, "b": 21}, gotFound)
is.EqualValues([]string{"c"}, gotMissing)
// has map
gotFound, gotMissing = itemMapsToValues[string, int](twice, map[string]*item[int]{"a": itemA, "b": itemB, "c": itemNo})
is.EqualValues(map[string]int{"a": 84, "b": 42}, gotFound)
is.EqualValues([]string{"c"}, gotMissing)
gotFound, gotMissing = itemMapsToValues[string, int](twice, map[string]*item[int]{"a": itemA}, map[string]*item[int]{"b": itemB, "c": itemNo, "a": itemNo})
is.EqualValues(map[string]int{"a": 84, "b": 42}, gotFound)
is.EqualValues([]string{"c"}, gotMissing)
}
func TestApplyJitter(t *testing.T) {
is := assert.New(t)
// no jitter
is.Equal(int64(1_000), applyJitter(1_000, 0, 0))
is.Equal(int64(1_000), applyJitter(1_000, 0, time.Second))
is.Equal(int64(1_000), applyJitter(1_000, 0.5, 0))
// with jitter
is.InEpsilon(1_000, applyJitter(1_000, 3, 100*time.Millisecond), 100)
is.InEpsilon(1_000, applyJitter(1_000, 3, 100*time.Millisecond), 100)
is.InEpsilon(1_000, applyJitter(1_000, 3, 100*time.Millisecond), 100)
is.InEpsilon(1_000, applyJitter(1_000, 3, 100*time.Millisecond), 100)
}