-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard_test.go
248 lines (209 loc) · 5.73 KB
/
leaderboard_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package goleaderboard
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"testing"
)
var (
redisClient *redis.Client
)
func setup(t *testing.T) {
redisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
if err := redisClient.Ping(context.Background()).Err(); err != nil {
t.Fatal("you must start redis server", err)
}
}
func teardown(t *testing.T) {
if err := redisClient.Close(); err != nil {
t.Fatal(err)
}
}
func clean(t *testing.T, ctx context.Context, leader Leaderboard) {
if err := leader.Clean(ctx); err != nil {
t.Fatal("failed to clean leaderboard", err.Error())
}
}
func initLeaderboard(
t *testing.T,
ctx context.Context,
numberOfMember int,
opts *Options,
) Leaderboard {
leaderboard := NewLeaderBoard(redisClient, "test", opts)
for i := 0; i < numberOfMember; i++ {
id := fmt.Sprintf("P%v", i)
addMember(t, ctx, leaderboard, id, numberOfMember-i)
}
return leaderboard
}
func getRank(
t *testing.T,
ctx context.Context,
leaderboard Leaderboard,
id interface{},
expected int,
) {
rank, err := leaderboard.GetRank(ctx, id)
if err != nil {
t.Error("failed to get rank of member", err.Error())
return
}
if rank != expected {
t.Errorf("Error in get rank of member\nExpected: rank #%v\nReceived: rank #%v", expected, rank)
}
}
func addMember(t *testing.T, ctx context.Context, leaderboard Leaderboard, id interface{}, score int) {
err := leaderboard.AddMember(ctx, id, score)
if err != nil {
t.Error("failed to add member", err.Error())
return
}
}
func getAround(
t *testing.T,
ctx context.Context,
leaderboard Leaderboard,
id interface{},
limit int,
expected int,
) ([]*Member, Cursor) {
list, cursor, err := leaderboard.GetAround(ctx, id, limit, OrderDesc)
if err != nil {
t.Error("failed to get around", err.Error())
return nil, Cursor{}
}
if len(list) != expected {
t.Errorf("Error in get around\nExpected: %v\nReceived: %v", limit, len(list))
return nil, Cursor{}
}
return list, cursor
}
func TestAddMember(t *testing.T) {
setup(t)
defer teardown(t)
testCases := []Options{
{
AllowSameRank: false,
},
{
AllowSameRank: true,
},
}
for _, tc := range testCases {
ctx := context.Background()
numberOfMember := 10
leaderboard := initLeaderboard(t, ctx, numberOfMember, &tc)
defer clean(t, ctx, leaderboard)
members, cursor, err := leaderboard.List(ctx, 0, numberOfMember, OrderDesc)
if err != nil {
t.Error("failed to list members", err.Error())
return
}
if cursor.Begin != 0 {
t.Errorf("something went wrong when list member\nExpected: cursor begins at %v\nReceived: cursor begins at %v", cursor.Begin, 0)
}
if cursor.End != numberOfMember {
t.Errorf("something went wrong when list member\nExpected: cursor ends at %v\nReceived: cursor ends at %v", cursor.End, numberOfMember)
}
if len(members) != numberOfMember {
t.Errorf("something went wrong when add member\nExpected: adding %v members\nReceived: adding %v members", numberOfMember, len(members))
}
clean(t, ctx, leaderboard)
}
}
func TestRankingMember(t *testing.T) {
setup(t)
defer teardown(t)
testCases := []Options{
{
AllowSameRank: false,
},
{
AllowSameRank: true,
},
}
for _, tc := range testCases {
ctx := context.Background()
numberOfMember := 10
leaderboard := initLeaderboard(t, ctx, numberOfMember, &tc)
defer clean(t, ctx, leaderboard)
player := "PMax"
addMember(t, ctx, leaderboard, player, 100)
getRank(t, ctx, leaderboard, player, 1)
playerMaxMax := "PMaxMax"
addMember(t, ctx, leaderboard, playerMaxMax, 1000)
getRank(t, ctx, leaderboard, player, 2)
clean(t, ctx, leaderboard)
}
}
func TestSameRankingMember(t *testing.T) {
setup(t)
defer teardown(t)
ctx := context.Background()
numberOfMember := 10
leaderboard := initLeaderboard(t, ctx, numberOfMember, &Options{AllowSameRank: true})
defer clean(t, ctx, leaderboard)
player1 := "PSame1"
err := leaderboard.AddMember(ctx, player1, 10)
if err != nil {
t.Error("failed to add member", err.Error())
return
}
player2 := "PSame2"
err = leaderboard.AddMember(ctx, player2, 10)
if err != nil {
t.Error("failed to add member", err.Error())
return
}
getRank(t, ctx, leaderboard, player1, 1)
getRank(t, ctx, leaderboard, player2, 1)
}
func TestGetAround(t *testing.T) {
setup(t)
defer teardown(t)
testCases := []Options{
{
AllowSameRank: false,
},
{
AllowSameRank: true,
},
}
for _, tc := range testCases {
ctx := context.Background()
numberOfMember := 10
leaderboard := initLeaderboard(t, ctx, numberOfMember, &tc)
defer clean(t, ctx, leaderboard)
player := "P4"
limit := 1
list, _ := getAround(t, ctx, leaderboard, player, limit, limit)
if list != nil && list[0].ID != player {
t.Errorf("Error in get around of member with limit %v\nExpected: player %v\nReceived: player %v", limit, player, list[0].ID)
return
}
limit = 6
list, _ = getAround(t, ctx, leaderboard, player, limit, limit)
if list != nil && list[limit/2].ID != player {
t.Errorf("Error in get around of member with limit %v\nExpected: player %v\nReceived: player %v", limit, player, list[limit/2].ID)
return
}
player = "P0"
list, _ = getAround(t, ctx, leaderboard, player, limit, limit)
if list != nil && list[0].ID != player {
t.Errorf("Error in get around of member with player in top 1\nExpected: player %v\nReceived: player %v", player, list[0].ID)
return
}
player = "P9"
list, _ = getAround(t, ctx, leaderboard, player, limit, limit)
if list != nil && list[limit-1].ID != player {
t.Errorf("Error in get around of member with player in top 10\nExpected: player %v\nReceived: player %v", player, list[limit-1].ID)
return
}
clean(t, ctx, leaderboard)
}
}