-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapped_zset_test.go
63 lines (44 loc) · 1.7 KB
/
capped_zset_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
package redisx_test
import (
"testing"
"time"
"github.com/nyaruka/redisx"
"github.com/nyaruka/redisx/assertredis"
"github.com/stretchr/testify/assert"
)
func TestCappedZSet(t *testing.T) {
rp := assertredis.TestDB()
rc := rp.Get()
defer rc.Close()
defer assertredis.FlushDB()
assertMembers := func(s *redisx.CappedZSet, expectedMembers []string, expectedScores []float64) {
actualMembers, actualScores, err := s.Members(rc)
assert.NoError(t, err)
assert.Equal(t, expectedMembers, actualMembers)
assert.Equal(t, expectedScores, actualScores)
}
zset := redisx.NewCappedZSet("foo", 3, time.Minute*5)
assert.NoError(t, zset.Add(rc, "A", 1))
assert.NoError(t, zset.Add(rc, "C", 3))
assert.NoError(t, zset.Add(rc, "B", 2))
assertredis.ZGetAll(t, rc, "foo", map[string]float64{"A": 1, "B": 2, "C": 3})
card, err := zset.Card(rc)
assert.NoError(t, err)
assert.Equal(t, 3, card)
assertMembers(zset, []string{"A", "B", "C"}, []float64{1, 2, 3})
// adding a new member with a higher score, pushes out the lowest scoring element
zset.Add(rc, "D", 4)
assertMembers(zset, []string{"B", "C", "D"}, []float64{2, 3, 4})
// adding a new member with a non-unique score still maintains the cap
zset.Add(rc, "E", 4)
assertMembers(zset, []string{"C", "D", "E"}, []float64{3, 4, 4})
// adding a new member with a score that's too low is a noop
zset.Add(rc, "F", 2)
assertMembers(zset, []string{"C", "D", "E"}, []float64{3, 4, 4})
// order is always based on score rather than lex
zset.Add(rc, "G", 3.5)
assertMembers(zset, []string{"G", "D", "E"}, []float64{3.5, 4, 4})
// re-adding a member updates the score
zset.Add(rc, "D", 4.5)
assertMembers(zset, []string{"G", "E", "D"}, []float64{3.5, 4, 4.5})
}