-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval_set_test.go
134 lines (104 loc) · 3.81 KB
/
interval_set_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
package redisx_test
import (
"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 TestIntervalSet(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, 0, 3, 234567, time.UTC))
// create a 24-hour x 2 based set
set1 := redisx.NewIntervalSet("foos", time.Hour*24, 2)
assert.NoError(t, set1.Add(rc, "A"))
assert.NoError(t, set1.Add(rc, "B"))
assert.NoError(t, set1.Add(rc, "C"))
assertredis.SMembers(t, rc, "foos:2021-11-18", []string{"A", "B", "C"})
assertredis.SMembers(t, rc, "foos:2021-11-17", []string{})
assertIsMember := func(s *redisx.IntervalSet, v string) {
contains, err := s.IsMember(rc, v)
assert.NoError(t, err)
assert.True(t, contains, "expected marker to contain %s", v)
}
assertNotIsMember := func(s *redisx.IntervalSet, v string) {
contains, err := s.IsMember(rc, v)
assert.NoError(t, err)
assert.False(t, contains, "expected marker to not contain %s", v)
}
assertIsMember(set1, "A")
assertIsMember(set1, "B")
assertIsMember(set1, "C")
assertNotIsMember(set1, "D")
// move forward a day..
setNow(time.Date(2021, 11, 19, 12, 0, 3, 234567, time.UTC))
set1.Add(rc, "D")
set1.Add(rc, "E")
assertredis.SMembers(t, rc, "foos:2021-11-19", []string{"D", "E"})
assertredis.SMembers(t, rc, "foos:2021-11-18", []string{"A", "B", "C"})
assertredis.SMembers(t, rc, "foos:2021-11-17", []string{})
assertIsMember(set1, "A")
assertIsMember(set1, "B")
assertIsMember(set1, "C")
assertIsMember(set1, "D")
assertIsMember(set1, "E")
assertNotIsMember(set1, "F")
// move forward again..
setNow(time.Date(2021, 11, 20, 12, 7, 3, 234567, time.UTC))
set1.Add(rc, "F")
set1.Add(rc, "G")
assertredis.SMembers(t, rc, "foos:2021-11-20", []string{"F", "G"})
assertredis.SMembers(t, rc, "foos:2021-11-19", []string{"D", "E"})
assertredis.SMembers(t, rc, "foos:2021-11-18", []string{"A", "B", "C"})
assertredis.SMembers(t, rc, "foos:2021-11-17", []string{})
assertNotIsMember(set1, "A") // too old
assertNotIsMember(set1, "B") // too old
assertNotIsMember(set1, "C") // too old
assertIsMember(set1, "D")
assertIsMember(set1, "E")
assertIsMember(set1, "F")
assertIsMember(set1, "G")
err := set1.Rem(rc, "F") // from today
require.NoError(t, err)
err = set1.Rem(rc, "E") // from yesterday
require.NoError(t, err)
assertredis.SMembers(t, rc, "foos:2021-11-20", []string{"G"})
assertredis.SMembers(t, rc, "foos:2021-11-19", []string{"D"})
assertIsMember(set1, "D")
assertNotIsMember(set1, "E")
assertNotIsMember(set1, "F")
assertIsMember(set1, "G")
err = set1.Clear(rc)
require.NoError(t, err)
assertredis.SMembers(t, rc, "foos:2021-11-20", []string{})
assertredis.SMembers(t, rc, "foos:2021-11-19", []string{})
assertNotIsMember(set1, "D")
assertNotIsMember(set1, "E")
assertNotIsMember(set1, "F")
assertNotIsMember(set1, "G")
// create a 5 minute x 3 based set
set2 := redisx.NewIntervalSet("foos", time.Minute*5, 3)
set2.Add(rc, "A")
set2.Add(rc, "B")
assertredis.SMembers(t, rc, "foos:2021-11-20T12:05", []string{"A", "B"})
assertredis.SMembers(t, rc, "foos:2021-11-20T12:00", []string{})
assertIsMember(set2, "A")
assertIsMember(set2, "B")
assertNotIsMember(set2, "C")
// create a 5 second x 2 based set
set3 := redisx.NewIntervalSet("foos", time.Second*5, 2)
set3.Add(rc, "A")
set3.Add(rc, "B")
assertredis.SMembers(t, rc, "foos:2021-11-20T12:07:00", []string{"A", "B"})
assertredis.SMembers(t, rc, "foos:2021-11-20T12:06:55", []string{})
assertIsMember(set3, "A")
assertIsMember(set3, "B")
assertNotIsMember(set3, "C")
}