-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_test.go
53 lines (39 loc) · 1.28 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
package goche
import (
"reflect"
"testing"
"time"
)
func TestCache_newItem(t *testing.T) {
t.Parallel()
t.Run("base test", func(t *testing.T) {
t.Parallel()
cache := New[string, string]()
cache.timeNowFunc = func() time.Time { return time.Unix(5, 0) }
got := cache.newItem("foo bar")
want := cacheItem[string]{val: "foo bar", cachedAt: cache.timeNowFunc()}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Got=%v, want=%v", got, want)
}
})
t.Run("ttl test", func(t *testing.T) {
t.Parallel()
cache := New[string, string]()
cache.timeNowFunc = func() time.Time { return time.Unix(0, 0) }
got := cache.newItem("foo bar", TTL[string](1*time.Second))
want := cacheItem[string]{val: "foo bar", cachedAt: cache.timeNowFunc(), ttl: 1 * time.Second}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Got=%v, want=%v", got, want)
}
})
t.Run("ttl with reset test", func(t *testing.T) {
t.Parallel()
cache := New[string, string]()
cache.timeNowFunc = func() time.Time { return time.Unix(0, 0) }
got := cache.newItem("foo bar", TTLWithReset[string](1*time.Second))
want := cacheItem[string]{val: "foo bar", cachedAt: cache.timeNowFunc(), ttl: 1 * time.Second, ttlReset: true}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Got=%v, want=%v", got, want)
}
})
}