-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval_hash_test.go
143 lines (112 loc) · 4.81 KB
/
interval_hash_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package redisx_test
import (
"strings"
"testing"
"time"
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/redisx"
"github.com/nyaruka/redisx/assertredis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIntervalHash(t *testing.T) {
rp := assertredis.TestDB()
rc := rp.Get()
defer rc.Close()
defer assertredis.FlushDB()
defer dates.SetNowSource(dates.DefaultNowSource)
setNow := func(d time.Time) { dates.SetNowSource(dates.NewFixedNowSource(d)) }
setNow(time.Date(2021, 11, 18, 12, 7, 3, 234567, time.UTC))
assertGet := func(h *redisx.IntervalHash, k, expected string) {
actual, err := h.Get(rc, k)
assert.NoError(t, err, "unexpected error getting key %s", k)
assert.Equal(t, expected, actual, "expected hash key %s to contain %s", k, expected)
}
assertMGet := func(h *redisx.IntervalHash, ks []string, expected []string) {
actual, err := h.MGet(rc, ks...)
assert.NoError(t, err, "unexpected error getting keys %s", strings.Join(ks, ","))
assert.Equal(t, expected, actual, "expected hash keys %s to contain %s", strings.Join(ks, ","), strings.Join(expected, ","))
}
// create a 24-hour x 2 based hash
hash1 := redisx.NewIntervalHash("foos", time.Hour*24, 2)
assert.NoError(t, hash1.Set(rc, "A", "1"))
assert.NoError(t, hash1.Set(rc, "B", "2"))
assert.NoError(t, hash1.Set(rc, "C", "3"))
assertredis.HGetAll(t, rc, "foos:2021-11-18", map[string]string{"A": "1", "B": "2", "C": "3"})
assertredis.HGetAll(t, rc, "foos:2021-11-17", map[string]string{})
assertGet(hash1, "A", "1")
assertGet(hash1, "B", "2")
assertGet(hash1, "C", "3")
assertGet(hash1, "D", "")
assertMGet(hash1, []string{"A", "C", "D"}, []string{"1", "3", ""})
assertMGet(hash1, []string{"D", "A"}, []string{"", "1"})
_, err := hash1.MGet(rc) // zero fields is an error
assert.EqualError(t, err, "wrong number of arguments for command")
// move forward a day..
setNow(time.Date(2021, 11, 19, 12, 7, 3, 234567, time.UTC))
hash1.Set(rc, "A", "5")
hash1.Set(rc, "B", "6")
assertredis.HGetAll(t, rc, "foos:2021-11-19", map[string]string{"A": "5", "B": "6"})
assertredis.HGetAll(t, rc, "foos:2021-11-18", map[string]string{"A": "1", "B": "2", "C": "3"})
assertredis.HGetAll(t, rc, "foos:2021-11-17", map[string]string{})
assertGet(hash1, "A", "5")
assertGet(hash1, "B", "6")
assertGet(hash1, "C", "3")
assertGet(hash1, "D", "")
assertMGet(hash1, []string{"A", "C", "D"}, []string{"5", "3", ""})
assertMGet(hash1, []string{"B"}, []string{"6"})
// move forward again..
setNow(time.Date(2021, 11, 20, 12, 7, 3, 234567, time.UTC))
hash1.Set(rc, "A", "7")
hash1.Set(rc, "Z", "9")
assertredis.HGetAll(t, rc, "foos:2021-11-20", map[string]string{"A": "7", "Z": "9"})
assertredis.HGetAll(t, rc, "foos:2021-11-19", map[string]string{"A": "5", "B": "6"})
assertredis.HGetAll(t, rc, "foos:2021-11-18", map[string]string{"A": "1", "B": "2", "C": "3"})
assertredis.HGetAll(t, rc, "foos:2021-11-17", map[string]string{})
assertGet(hash1, "A", "7")
assertGet(hash1, "Z", "9")
assertGet(hash1, "B", "6")
assertGet(hash1, "C", "") // too old
assertGet(hash1, "D", "")
assertMGet(hash1, []string{"B", "A", "D"}, []string{"6", "7", ""})
err = hash1.Del(rc, "A") // from today and yesterday
require.NoError(t, err)
err = hash1.Del(rc, "B") // from yesterday
require.NoError(t, err)
assertredis.HGetAll(t, rc, "foos:2021-11-20", map[string]string{"Z": "9"})
assertredis.HGetAll(t, rc, "foos:2021-11-19", map[string]string{})
assertredis.HGetAll(t, rc, "foos:2021-11-18", map[string]string{"A": "1", "B": "2", "C": "3"})
assertredis.HGetAll(t, rc, "foos:2021-11-17", map[string]string{})
assertGet(hash1, "A", "")
assertGet(hash1, "Z", "9")
assertGet(hash1, "B", "")
assertGet(hash1, "C", "")
assertGet(hash1, "D", "")
err = hash1.Clear(rc)
require.NoError(t, err)
assertredis.HGetAll(t, rc, "foos:2021-11-20", map[string]string{})
assertredis.HGetAll(t, rc, "foos:2021-11-19", map[string]string{})
assertGet(hash1, "A", "")
assertGet(hash1, "Z", "")
assertGet(hash1, "B", "")
assertGet(hash1, "C", "")
assertGet(hash1, "D", "")
// create a 5 minute x 3 based hash
hash2 := redisx.NewIntervalHash("foos", time.Minute*5, 3)
hash2.Set(rc, "A", "1")
hash2.Set(rc, "B", "2")
assertredis.HGetAll(t, rc, "foos:2021-11-20T12:05", map[string]string{"A": "1", "B": "2"})
assertredis.HGetAll(t, rc, "foos:2021-11-20T12:00", map[string]string{})
assertGet(hash2, "A", "1")
assertGet(hash2, "B", "2")
assertGet(hash2, "C", "")
// create a 5 second x 2 based set
hash3 := redisx.NewIntervalHash("foos", time.Second*5, 2)
hash3.Set(rc, "A", "1")
hash3.Set(rc, "B", "2")
assertredis.HGetAll(t, rc, "foos:2021-11-20T12:07:00", map[string]string{"A": "1", "B": "2"})
assertredis.HGetAll(t, rc, "foos:2021-11-20T12:06:55", map[string]string{})
assertGet(hash3, "A", "1")
assertGet(hash3, "B", "2")
assertGet(hash3, "C", "")
}