forked from mattrobenolt/go-memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_test.go
73 lines (65 loc) · 1.71 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
package memcached
import (
"testing"
"time"
)
const TEN_SECONDS = time.Duration(10) * time.Second
func TestIsExpiredZero(t *testing.T) {
item := &Item{}
if item.IsExpired() {
t.Error("Zero time shouldn't be expired")
}
}
func TestIsExpiredFuture(t *testing.T) {
item := &Item{
Expires: time.Now().Add(TEN_SECONDS),
}
if item.IsExpired() {
t.Error("Future shouldn't be expired")
}
}
func TestIsExpiredPast(t *testing.T) {
item := &Item{
Expires: time.Now().Add(-TEN_SECONDS),
}
if !item.IsExpired() {
t.Error("Past should be expired")
}
}
func TestSetExpiresZero(t *testing.T) {
item := &Item{}
item.SetExpires(0)
if !item.Expires.IsZero() {
t.Error("Zero should have a Zero time:", item.Expires)
}
if item.Ttl != 0 {
t.Error("Zero should have a Ttl of 0:", item.Ttl)
}
}
func TestSetExpiresTypical(t *testing.T) {
item := &Item{}
item.SetExpires(10)
if item.Expires.IsZero() {
t.Error("Shouldn't have a Zero Expires time")
}
if !item.Expires.After(time.Now()) {
t.Error("Expires should be in the future.")
}
if item.Expires.Sub(time.Now()) >= TEN_SECONDS || item.Expires.Sub(time.Now()) < time.Duration(9)*time.Second {
t.Error("Expires should be > 9, and < 10 seconds")
}
if item.Ttl != 10 {
t.Error("Ttl should be 10:", item.Ttl)
}
}
func TestSetExpiresMaxExptime(t *testing.T) {
item := &Item{}
expires := time.Unix(int64(60*60*24*30)+1, 0) // 1 second greater than 30 days
item.SetExpires(expires.Unix())
if !item.Expires.Equal(expires) {
t.Error("Expires should be 1970-01-30:", item.Expires)
}
if item.Ttl > -1370325162 { // well, will always be smaller than this known point in time since it's based on Now()
t.Error("Ttl should be really really really low:", item.Ttl)
}
}