-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_test.go
74 lines (70 loc) · 1.54 KB
/
entry_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
package smartcache
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEntryExpired(t *testing.T) {
tests := []struct {
name string
created time.Time
fixedExp *time.Time
ttl time.Duration
wantExpired bool
}{
{
name: "zero ttl",
created: time.Now(),
fixedExp: nil,
ttl: 0,
wantExpired: true,
},
{
name: "non-zero ttl",
created: time.Now(),
fixedExp: nil,
ttl: time.Second,
wantExpired: false,
},
{
name: "fixed exp in past, expired",
created: time.Now(),
fixedExp: timePtr(time.Now().Add(-time.Minute)),
ttl: 30 * time.Second,
wantExpired: true,
},
{
name: "fixed exp in future, not expired",
created: time.Now(),
fixedExp: timePtr(time.Now().Add(time.Minute)),
ttl: 0,
wantExpired: false,
},
{
name: "created in past, expired",
created: time.Now().Add(-time.Minute),
ttl: 30 * time.Second,
wantExpired: true,
},
{
name: "created in past, not expired",
created: time.Now().Add(-time.Minute),
ttl: 2 * time.Minute,
wantExpired: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := CacheEntry[bool]{
Data: new(bool),
Err: nil,
Created: tt.created,
FixedExpiration: tt.fixedExp,
}
assert.Equal(t, tt.wantExpired, e.IsExpired(tt.ttl))
})
}
}
func timePtr(t time.Time) *time.Time {
return &t
}