-
Notifications
You must be signed in to change notification settings - Fork 1
/
value_test.go
66 lines (62 loc) · 1.91 KB
/
value_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
package sc
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_value_isFresh(t *testing.T) {
type args struct {
now monoTime
freshFor time.Duration
}
tests := []struct {
name string
created monoTime
args args
want bool
}{
{"not fresh", monoTime(-10 * time.Minute), args{0, 5 * time.Minute}, false},
{"exact fresh", monoTime(-5 * time.Minute), args{0, 5 * time.Minute}, true},
{"fresh", monoTime(-3 * time.Minute), args{0, 5 * time.Minute}, true},
{"fresh (future)", monoTime(3 * time.Minute), args{0, 5 * time.Minute}, true},
{"fresh (distant future)", monoTime(30 * time.Minute), args{0, 5 * time.Minute}, true},
{"fresh (now)", 0, args{0, 5 * time.Minute}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := &value[string]{
v: "",
created: tt.created,
}
assert.Equalf(t, tt.want, v.isFresh(tt.args.now, tt.args.freshFor), "isFresh(%v, %v)", tt.args.now, tt.args.freshFor)
})
}
}
func Test_value_isExpired(t *testing.T) {
type args struct {
now monoTime
ttl time.Duration
}
tests := []struct {
name string
created monoTime
args args
want bool
}{
{"expired", monoTime(-10 * time.Minute), args{0, 5 * time.Minute}, true},
{"exact not expired", monoTime(-5 * time.Minute), args{0, 5 * time.Minute}, false},
{"not expired", monoTime(-3 * time.Minute), args{0, 5 * time.Minute}, false},
{"not expired (future)", monoTime(3 * time.Minute), args{0, 5 * time.Minute}, false},
{"not expired (distant future)", monoTime(30 * time.Minute), args{0, 5 * time.Minute}, false},
{"not expired (now)", 0, args{0, 5 * time.Minute}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := &value[string]{
v: "",
created: tt.created,
}
assert.Equalf(t, tt.want, v.isExpired(tt.args.now, tt.args.ttl), "isExpired(%v, %v)", tt.args.now, tt.args.ttl)
})
}
}