-
Notifications
You must be signed in to change notification settings - Fork 4
/
dict_test.go
67 lines (50 loc) · 1.37 KB
/
dict_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
package main
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDict(t *testing.T) {
assert := assert.New(t)
t.Run("set", func(t *testing.T) {
dict := New()
dict.Set("key", []byte("hello"))
data, ttl := dict.Get("key")
assert.Equal(ttl, KeepTTL)
assert.Equal(data, []byte("hello"))
data, ttl = dict.Get("none")
assert.Nil(data)
assert.Equal(ttl, KEY_NOT_EXIST)
})
t.Run("setTTL", func(t *testing.T) {
dict := New()
dict.SetWithTTL("key", []byte("hello"), time.Now().Add(time.Minute).UnixNano())
time.Sleep(time.Second / 10)
data, ttl := dict.Get("key")
assert.Equal(ttl, 59)
assert.Equal(data, []byte("hello"))
res := dict.SetTTL("key", time.Now().Add(-time.Second).UnixNano())
assert.Equal(res, 1)
res = dict.SetTTL("not-exist", KeepTTL)
assert.Equal(res, 0)
// get expired
data, ttl = dict.Get("key")
assert.Equal(ttl, KEY_NOT_EXIST)
assert.Nil(data)
// setTTL expired
dict.SetWithTTL("keyx", []byte("hello"), time.Now().Add(-time.Second).UnixNano())
res = dict.SetTTL("keyx", 1)
assert.Equal(res, 0)
})
t.Run("delete", func(t *testing.T) {
dict := New()
dict.Set("key", []byte("hello"))
ok := dict.Delete("key")
assert.True(ok)
ok = dict.Delete("none")
assert.False(ok)
dict.SetWithTTL("keyx", []byte("hello"), time.Now().UnixNano())
ok = dict.Delete("keyx")
assert.True(ok)
})
}