FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
phpredis/tests/RedisArrayTest.php at develop · phpredis/phpredis · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
phpredis
/
phpredis
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.2k
Code
Issues
100
Pull requests
34
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
phpredis
/
tests
/
RedisArrayTest.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
688 lines (553 loc) · 20 KB
Breadcrumbs
phpredis
/
tests
/
RedisArrayTest.php
Copy path
File metadata and controls
688 lines (553 loc) · 20 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<?php
defined
(
'
PHPREDIS_TESTRUN
'
)
or
die
(
"
Use TestRedis.php to run tests!
\n"
);
require_once
__DIR__
.
"
/TestSuite.php
"
;
define
(
'
REDIS_ARRAY_DATA_SIZE
'
,
1000
);
define
(
'
REDIS_RA_DEFAULT_PORTS
'
, [
6379
,
6380
,
6381
,
6382
]);
function
custom_hash
(
$
str
) {
// str has the following format: $APPID_fb$FACEBOOKID_$key.
$
pos
=
strpos
(
$
str
,
'
_fb
'
);
if
(
preg_match
(
"
#\w+_fb(?<facebook_id>\d+)_\w+#
"
,
$
str
,
$
out
)) {
return
$
out
[
'
facebook_id
'
];
}
return
$
str
;
}
function
parseHostPort
(
$
str
, &
$
host
, &
$
port
) {
$
pos
=
strrpos
(
$
str
,
'
:
'
);
$
host
=
substr
(
$
str
,
0
,
$
pos
);
$
port
=
substr
(
$
str
,
$
pos
+
1
);
}
function
getRedisVersion
(
object
$
client
) {
$
arr_info
=
$
client
->
info
();
if
( !
$
arr_info
|| !
isset
(
$
arr_info
[
'
redis_version
'
])) {
return
'
0.0.0
'
;
}
return
$
arr_info
[
'
redis_version
'
];
}
/* Determine the lowest redis version attached to this RedisArray object */
function
getMinVersion
(
object
$
ra
) {
$
min_version
=
'
0.0.0
'
;
foreach
(
$
ra
->
_hosts
()
as
$
host
) {
$
version
=
getRedisVersion
(
$
ra
->
_instance
(
$
host
));
if
(
version_compare
(
$
version
,
$
min_version
) >
0
) {
$
min_version
=
$
version
;
}
}
return
$
min_version
;
}
class
Redis_Array_Test
extends
TestSuite
{
private
$
min_version
;
private
$
strings
;
public
$
ra
=
NULL
;
private
$
data
=
NULL
;
public
function
setUp
() {
// initialize strings.
$
n
=
REDIS_ARRAY_DATA_SIZE
;
$
this
->
strings
= [];
for
(
$
i
=
0
;
$
i
<
$
n
;
$
i
++) {
$
this
->
strings
[
'
key-
'
.
$
i
] =
'
val-
'
.
$
i
;
}
global
$
new_ring
,
$
old_ring
,
$
use_index
;
$
options
= [
'
previous
'
=>
$
old_ring
,
'
index
'
=>
$
use_index
];
if
(
$
this
->
getAuth
()) {
$
options
[
'
auth
'
] =
$
this
->
getAuth
();
}
$
this
->
ra
=
new
RedisArray
(
$
new_ring
,
$
options
);
$
this
->
min_version
=
getMinVersion
(
$
this
->
ra
);
}
public
function
testMSet
() {
// run mset
$
this
->
assertTrue
(
$
this
->
ra
->
mset
(
$
this
->
strings
));
// check each key individually using the array
foreach
(
$
this
->
strings
as
$
k
=>
$
v
) {
$
this
->
assertEquals
(
$
v
,
$
this
->
ra
->
get
(
$
k
));
}
// check each key individually using a new connection
foreach
(
$
this
->
strings
as
$
k
=>
$
v
) {
parseHostPort
(
$
this
->
ra
->
_target
(
$
k
),
$
host
,
$
port
);
$
target
=
$
this
->
ra
->
_target
(
$
k
);
$
pos
=
strrpos
(
$
target
,
'
:
'
);
$
host
=
substr
(
$
target
,
0
,
$
pos
);
$
port
=
substr
(
$
target
,
$
pos
+
1
);
$
r
=
new
Redis
;
$
r
->
pconnect
(
$
host
, (
int
)
$
port
);
if
(
$
this
->
getAuth
()) {
$
this
->
assertTrue
(
$
r
->
auth
(
$
this
->
getAuth
()));
}
$
this
->
assertEquals
(
$
v
,
$
r
->
get
(
$
k
));
}
}
public
function
testMGet
() {
$
this
->
assertEquals
(
array_values
(
$
this
->
strings
),
$
this
->
ra
->
mget
(
array_keys
(
$
this
->
strings
)));
}
private
function
addData
(
$
commonString
) {
$
this
->
data
= [];
for
(
$
i
=
0
;
$
i
<
REDIS_ARRAY_DATA_SIZE
;
$
i
++) {
$
k
=
rand
().
'
_
'
.
$
commonString
.
'
_
'
.
rand
();
$
this
->
data
[
$
k
] =
rand
();
}
$
this
->
ra
->
mset
(
$
this
->
data
);
}
private
function
checkCommonLocality
() {
// check that they're all on the same node.
$
lastNode
=
NULL
;
foreach
(
$
this
->
data
as
$
k
=>
$
v
) {
$
node
=
$
this
->
ra
->
_target
(
$
k
);
if
(
$
lastNode
) {
$
this
->
assertEquals
(
$
node
,
$
lastNode
);
}
$
this
->
assertEqualsWeak
(
$
v
,
$
this
->
ra
->
get
(
$
k
));
$
lastNode
=
$
node
;
}
}
public
function
testKeyLocality
() {
// basic key locality with default hash
$
this
->
addData
(
'
{hashed part of the key}
'
);
$
this
->
checkCommonLocality
();
// with common hashing function
global
$
new_ring
,
$
old_ring
,
$
use_index
;
$
options
= [
'
previous
'
=>
$
old_ring
,
'
index
'
=>
$
use_index
,
'
function
'
=>
'
custom_hash
'
];
if
(
$
this
->
getAuth
()) {
$
options
[
'
auth
'
] =
$
this
->
getAuth
();
}
$
this
->
ra
=
new
RedisArray
(
$
new_ring
,
$
options
);
// basic key locality with custom hash
$
this
->
addData
(
'
fb
'
.
rand
());
$
this
->
checkCommonLocality
();
}
public
function
customDistributor
(
$
key
)
{
$
a
=
unpack
(
"
N*
"
,
md5
(
$
key
,
true
));
global
$
new_ring
;
$
pos
=
abs
(
$
a
[
1
]) %
count
(
$
new_ring
);
return
$
pos
;
}
public
function
testKeyDistributor
()
{
global
$
new_ring
,
$
useIndex
;
$
options
= [
'
index
'
=>
$
useIndex
,
'
function
'
=>
'
custom_hash
'
,
'
distributor
'
=> [
$
this
,
"
customDistributor
"
]
];
if
(
$
this
->
getAuth
()) {
$
options
[
'
auth
'
] =
$
this
->
getAuth
();
}
$
this
->
ra
=
new
RedisArray
(
$
new_ring
,
$
options
);
// custom key distribution function.
$
this
->
addData
(
'
fb
'
.
rand
());
// check that they're all on the expected node.
$
lastNode
=
NULL
;
foreach
(
$
this
->
data
as
$
k
=>
$
v
) {
$
node
=
$
this
->
ra
->
_target
(
$
k
);
$
pos
=
$
this
->
customDistributor
(
$
k
);
$
this
->
assertEquals
(
$
node
,
$
new_ring
[
$
pos
]);
}
}
/* Scan a whole key and return the overall result */
protected
function
execKeyScan
(
$
cmd
,
$
key
) {
$
res
= [];
$
it
=
NULL
;
do
{
$
chunk
=
$
this
->
ra
->
$
cmd
(
$
key
,
$
it
);
foreach
(
$
chunk
as
$
field
=>
$
value
) {
$
res
[
$
field
] =
$
value
;
}
}
while
(
$
it
!==
0
);
return
$
res
;
}
public
function
testKeyScanning
() {
$
h_vals
= [
'
foo
'
=>
'
bar
'
,
'
baz
'
=>
'
bop
'
];
$
z_vals
= [
'
one
'
=>
1
,
'
two
'
=>
2
,
'
three
'
=>
3
];
$
s_vals
= [
'
mem1
'
,
'
mem2
'
,
'
mem3
'
];
$
this
->
ra
->
del
([
'
scan-hash
'
,
'
scan-set
'
,
'
scan-zset
'
]);
$
this
->
ra
->
hMSet
(
'
scan-hash
'
,
$
h_vals
);
foreach
(
$
z_vals
as
$
k
=>
$
v
)
$
this
->
ra
->
zAdd
(
'
scan-zset
'
,
$
v
,
$
k
);
$
this
->
ra
->
sAdd
(
'
scan-set
'
, ...
$
s_vals
);
$
s_scan
=
$
this
->
execKeyScan
(
'
sScan
'
,
'
scan-set
'
);
$
this
->
assertTrue
(
count
(
array_diff_key
(
array_flip
(
$
s_vals
),
array_flip
(
$
s_scan
))) ==
0
);
$
this
->
assertEquals
(
$
h_vals
,
$
this
->
execKeyScan
(
'
hScan
'
,
'
scan-hash
'
));
$
z_scan
=
$
this
->
execKeyScan
(
'
zScan
'
,
'
scan-zset
'
);
$
this
->
assertTrue
(
count
(
$
z_scan
) ==
count
(
$
z_vals
) &&
count
(
array_diff_key
(
$
z_vals
,
$
z_scan
)) ==
0
&&
array_sum
(
$
z_scan
) ==
array_sum
(
$
z_vals
));
}
}
class
Redis_Rehashing_Test
extends
TestSuite
{
public
$
ra
=
NULL
;
private
$
useIndex
;
private
$
min_version
;
// data
private
$
strings
;
private
$
sets
;
private
$
lists
;
private
$
hashes
;
private
$
zsets
;
public
function
setUp
() {
// initialize strings.
$
n
=
REDIS_ARRAY_DATA_SIZE
;
$
this
->
strings
= [];
for
(
$
i
=
0
;
$
i
<
$
n
;
$
i
++) {
$
this
->
strings
[
'
key-
'
.
$
i
] =
'
val-
'
.
$
i
;
}
// initialize sets
for
(
$
i
=
0
;
$
i
<
$
n
;
$
i
++) {
// each set has 20 elements
$
this
->
sets
[
'
set-
'
.
$
i
] =
range
(
$
i
,
$
i
+
20
);
}
// initialize lists
for
(
$
i
=
0
;
$
i
<
$
n
;
$
i
++) {
// each list has 20 elements
$
this
->
lists
[
'
list-
'
.
$
i
] =
range
(
$
i
,
$
i
+
20
);
}
// initialize hashes
for
(
$
i
=
0
;
$
i
<
$
n
;
$
i
++) {
// each hash has 5 keys
$
this
->
hashes
[
'
hash-
'
.
$
i
] = [
'
A
'
=>
$
i
,
'
B
'
=>
$
i
+
1
,
'
C
'
=>
$
i
+
2
,
'
D
'
=>
$
i
+
3
,
'
E
'
=>
$
i
+
4
];
}
// initialize sorted sets
for
(
$
i
=
0
;
$
i
<
$
n
;
$
i
++) {
// each sorted sets has 5 elements
$
this
->
zsets
[
'
zset-
'
.
$
i
] = [
$
i
,
'
A
'
,
$
i
+
1
,
'
B
'
,
$
i
+
2
,
'
C
'
,
$
i
+
3
,
'
D
'
,
$
i
+
4
,
'
E
'
];
}
global
$
new_ring
,
$
old_ring
,
$
useIndex
;
$
options
= [
'
previous
'
=>
$
old_ring
,
'
index
'
=>
$
useIndex
];
if
(
$
this
->
getAuth
()) {
$
options
[
'
auth
'
] =
$
this
->
getAuth
();
}
// create array
$
this
->
ra
=
new
RedisArray
(
$
new_ring
,
$
options
);
$
this
->
min_version
=
getMinVersion
(
$
this
->
ra
);
}
public
function
testFlush
() {
// flush all servers first.
global
$
server_list
;
foreach
(
$
server_list
as
$
s
) {
parseHostPort
(
$
s
,
$
host
,
$
port
);
$
r
=
new
Redis
();
$
r
->
pconnect
(
$
host
, (
int
)
$
port
,
0
);
if
(
$
this
->
getAuth
()) {
$
this
->
assertTrue
(
$
r
->
auth
(
$
this
->
getAuth
()));
}
$
this
->
assertTrue
(
$
r
->
flushdb
());
}
}
private
function
distributeKeys
() {
// strings
foreach
(
$
this
->
strings
as
$
k
=>
$
v
) {
$
this
->
ra
->
set
(
$
k
,
$
v
);
}
// sets
foreach
(
$
this
->
sets
as
$
k
=>
$
v
) {
call_user_func_array
([
$
this
->
ra
,
'
sadd
'
],
array_merge
([
$
k
],
$
v
));
}
// lists
foreach
(
$
this
->
lists
as
$
k
=>
$
v
) {
call_user_func_array
([
$
this
->
ra
,
'
rpush
'
],
array_merge
([
$
k
],
$
v
));
}
// hashes
foreach
(
$
this
->
hashes
as
$
k
=>
$
v
) {
$
this
->
ra
->
hmset
(
$
k
,
$
v
);
}
// sorted sets
foreach
(
$
this
->
zsets
as
$
k
=>
$
v
) {
call_user_func_array
([
$
this
->
ra
,
'
zadd
'
],
array_merge
([
$
k
],
$
v
));
}
}
public
function
testDistribution
() {
$
this
->
distributeKeys
();
}
public
function
testSimpleRead
() {
$
this
->
readAllvalues
();
}
private
function
readAllvalues
() {
// strings
foreach
(
$
this
->
strings
as
$
k
=>
$
v
) {
$
this
->
assertEquals
(
$
v
,
$
this
->
ra
->
get
(
$
k
));
}
// sets
foreach
(
$
this
->
sets
as
$
k
=>
$
v
) {
$
ret
=
$
this
->
ra
->
smembers
(
$
k
);
// get values
$
this
->
assertEqualsWeak
(
$
v
,
$
ret
);
}
// lists
foreach
(
$
this
->
lists
as
$
k
=>
$
v
) {
$
ret
=
$
this
->
ra
->
lrange
(
$
k
,
0
, -
1
);
$
this
->
assertEqualsWeak
(
$
v
,
$
ret
);
}
// hashes
foreach
(
$
this
->
hashes
as
$
k
=>
$
v
) {
$
ret
=
$
this
->
ra
->
hgetall
(
$
k
);
// get values
$
this
->
assertEqualsWeak
(
$
v
,
$
ret
);
}
// sorted sets
foreach
(
$
this
->
zsets
as
$
k
=>
$
v
) {
$
ret
=
$
this
->
ra
->
zrange
(
$
k
,
0
, -
1
,
true
);
// create assoc array from local dataset
$
tmp
= [];
for
(
$
i
=
0
;
$
i
<
count
(
$
v
);
$
i
+=
2
) {
$
tmp
[
$
v
[
$
i
+
1
]] =
$
v
[
$
i
];
}
// compare to RA value
$
this
->
assertEqualsWeak
(
$
tmp
,
$
ret
);
}
}
// add a new node.
public
function
testCreateSecondRing
() {
global
$
new_ring
,
$
old_ring
,
$
server_list
;
$
old_ring
=
$
new_ring
;
// back up the original.
$
new_ring
=
$
server_list
;
// add a new node to the main ring.
}
public
function
testReadUsingFallbackMechanism
() {
$
this
->
readAllvalues
();
// some of the reads will fail and will go to another target node.
}
public
function
testRehash
() {
$
this
->
ra
->
_rehash
();
// this will redistribute the keys
}
public
function
testRehashWithCallback
() {
$
total
=
0
;
$
this
->
ra
->
_rehash
(
function
(
$
host
,
$
count
)
use
(&
$
total
) {
$
total
+=
$
count
;
});
$
this
->
assertGT
(
0
,
$
total
);
}
public
function
testReadRedistributedKeys
() {
$
this
->
readAllvalues
();
// we shouldn't have any missed reads now.
}
}
// Test auto-migration of keys
class
Redis_Auto_Rehashing_Test
extends
TestSuite {
public
$
ra
=
NULL
;
private
$
min_version
;
// data
private
$
strings
;
public
function
setUp
() {
// initialize strings.
$
n
=
REDIS_ARRAY_DATA_SIZE
;
$
this
->
strings
= [];
for
(
$
i
=
0
;
$
i
<
$
n
;
$
i
++) {
$
this
->
strings
[
'
key-
'
.
$
i
] =
'
val-
'
.
$
i
;
}
global
$
new_ring
,
$
old_ring
,
$
useIndex
;
$
options
= [
'
previous
'
=>
$
old_ring
,
'
index
'
=>
$
useIndex
,
'
autorehash
'
=>
true
];
if
(
$
this
->
getAuth
()) {
$
options
[
'
auth
'
] =
$
this
->
getAuth
();
}
// create array
$
this
->
ra
=
new
RedisArray
(
$
new_ring
,
$
options
);
$
this
->
min_version
=
getMinVersion
(
$
this
->
ra
);
}
public
function
testDistribute
() {
// strings
foreach
(
$
this
->
strings
as
$
k
=>
$
v
) {
$
this
->
ra
->
set
(
$
k
,
$
v
);
}
}
private
function
readAllvalues
() {
foreach
(
$
this
->
strings
as
$
k
=>
$
v
) {
$
this
->
assertEquals
(
$
v
,
$
this
->
ra
->
get
(
$
k
));
}
}
public
function
testReadAll
() {
$
this
->
readAllvalues
();
}
// add a new node.
public
function
testCreateSecondRing
() {
global
$
new_ring
,
$
old_ring
,
$
server_list
;
$
old_ring
=
$
new_ring
;
// back up the original.
$
new_ring
=
$
server_list
;
// add a new node to the main ring.
}
// Read and migrate keys on fallback, causing the whole ring to be rehashed.
public
function
testReadAndMigrateAll
() {
$
this
->
readAllvalues
();
}
// Read and migrate keys on fallback, causing the whole ring to be rehashed.
public
function
testAllKeysHaveBeenMigrated
() {
foreach
(
$
this
->
strings
as
$
k
=>
$
v
) {
parseHostPort
(
$
this
->
ra
->
_target
(
$
k
),
$
host
,
$
port
);
$
r
=
new
Redis
;
$
r
->
pconnect
(
$
host
,
$
port
);
if
(
$
this
->
getAuth
()) {
$
this
->
assertTrue
(
$
r
->
auth
(
$
this
->
getAuth
()));
}
// check that the key has actually been migrated to the new node.
$
this
->
assertEquals
(
$
v
,
$
r
->
get
(
$
k
));
}
}
}
// Test node-specific multi/exec
class
Redis_Multi_Exec_Test
extends
TestSuite {
private
$
min_version
;
public
$
ra
=
NULL
;
private
static
$
new_group
=
NULL
;
private
static
$
new_salary
=
NULL
;
public
function
setUp
() {
global
$
new_ring
,
$
old_ring
,
$
useIndex
;
$
options
= [
'
previous
'
=>
$
old_ring
,
'
index
'
=>
$
useIndex
];
if
(
$
this
->
getAuth
()) {
$
options
[
'
auth
'
] =
$
this
->
getAuth
();
}
// create array
$
this
->
ra
=
new
RedisArray
(
$
new_ring
,
$
options
);
$
this
->
min_version
=
getMinVersion
(
$
this
->
ra
);
}
public
function
testInit
() {
$
this
->
ra
->
set
(
'
{groups}:managers
'
,
2
);
$
this
->
ra
->
set
(
'
{groups}:executives
'
,
3
);
$
this
->
ra
->
set
(
'
1_{employee:joe}_name
'
,
'
joe
'
);
$
this
->
ra
->
set
(
'
1_{employee:joe}_group
'
,
2
);
$
this
->
ra
->
set
(
'
1_{employee:joe}_salary
'
,
2000
);
}
public
function
testKeyDistribution
() {
// check that all of joe's keys are on the same instance
$
lastNode
=
NULL
;
foreach
([
'
name
'
,
'
group
'
,
'
salary
'
]
as
$
field
) {
$
node
=
$
this
->
ra
->
_target
(
'
1_{employee:joe}_
'
.
$
field
);
if
(
$
lastNode
) {
$
this
->
assertEquals
(
$
node
,
$
lastNode
);
}
$
lastNode
=
$
node
;
}
}
public
function
testMultiExec
() {
// Joe gets a promotion
self
::
$
new_group
=
$
this
->
ra
->
get
(
'
{groups}:executives
'
);
self
::
$
new_salary
=
4000
;
// change both in a transaction.
// transactions are per-node, so we need a reference to it.
$
host
=
$
this
->
ra
->
_target
(
'
{employee:joe}
'
);
$
this
->
ra
->
multi
(
$
host
)
->
set
(
'
1_{employee:joe}_group
'
,
self
::
$
new_group
)
->
set
(
'
1_{employee:joe}_salary
'
,
self
::
$
new_salary
)
->
exec
();
// check that the group and salary have been changed
$
this
->
assertEquals
(
self
::
$
new_group
,
$
this
->
ra
->
get
(
'
1_{employee:joe}_group
'
));
$
this
->
assertEqualsWeak
(
self
::
$
new_salary
,
$
this
->
ra
->
get
(
'
1_{employee:joe}_salary
'
));
}
public
function
testMultiExecMSet
() {
self
::
$
new_group
=
1
;
self
::
$
new_salary
=
10000
;
// test MSET, making Joe a top-level executive
$
out
=
$
this
->
ra
->
multi
(
$
this
->
ra
->
_target
(
'
{employee:joe}
'
))
->
mset
([
'
1_{employee:joe}_group
'
=>
self
::
$
new_group
,
'
1_{employee:joe}_salary
'
=>
self
::
$
new_salary
])
->
exec
();
$
this
->
assertTrue
(
$
out
[
0
]);
}
public
function
testMultiExecMGet
() {
$
out
=
$
this
->
ra
->
multi
(
$
this
->
ra
->
_target
(
'
{employee:joe}
'
))
->
mget
([
'
1_{employee:joe}_group
'
,
'
1_{employee:joe}_salary
'
])
->
exec
();
$
this
->
assertEqualsWeak
(
self
::
$
new_group
,
$
out
[
0
][
0
]);
$
this
->
assertEqualsWeak
(
self
::
$
new_salary
,
$
out
[
0
][
1
]);
}
public
function
testMultiExecDel
() {
$
out
=
$
this
->
ra
->
multi
(
$
this
->
ra
->
_target
(
'
{employee:joe}
'
))
->
del
(
'
1_{employee:joe}_group
'
,
'
1_{employee:joe}_salary
'
)
->
exec
();
$
this
->
assertEquals
(
2
,
$
out
[
0
]);
$
this
->
assertEquals
(
0
,
$
this
->
ra
->
exists
(
'
1_{employee:joe}_group
'
));
$
this
->
assertEquals
(
0
,
$
this
->
ra
->
exists
(
'
1_{employee:joe}_salary
'
));
}
public
function
testMultiExecUnlink
() {
if
(
version_compare
(
$
this
->
min_version
,
"
4.0.0
"
,
"
lt
"
)) {
$
this
->
markTestSkipped
();
}
$
this
->
ra
->
set
(
'
{unlink}:key1
'
,
'
bar
'
);
$
this
->
ra
->
set
(
'
{unlink}:key2
'
,
'
bar
'
);
$
out
=
$
this
->
ra
->
multi
(
$
this
->
ra
->
_target
(
'
{unlink}
'
))
->
del
(
'
{unlink}:key1
'
,
'
{unlink}:key2
'
)
->
exec
();
$
this
->
assertEquals
(
2
,
$
out
[
0
]);
}
public
function
testDiscard
() {
/* phpredis issue #87 */
$
key
=
'
test_err
'
;
$
this
->
assertTrue
(
$
this
->
ra
->
set
(
$
key
,
'
test
'
));
$
this
->
assertEquals
(
'
test
'
,
$
this
->
ra
->
get
(
$
key
));
$
this
->
ra
->
watch
(
$
key
);
// After watch, same
$
this
->
assertEquals
(
'
test
'
,
$
this
->
ra
->
get
(
$
key
));
// change in a multi/exec block.
$
ret
=
$
this
->
ra
->
multi
(
$
this
->
ra
->
_target
(
$
key
))->
set
(
$
key
,
'
test1
'
)->
exec
();
$
this
->
assertEquals
([
true
],
$
ret
);
// Get after exec, 'test1':
$
this
->
assertEquals
(
'
test1
'
,
$
this
->
ra
->
get
(
$
key
));
$
this
->
ra
->
watch
(
$
key
);
// After second watch, still test1.
$
this
->
assertEquals
(
'
test1
'
,
$
this
->
ra
->
get
(
$
key
));
$
ret
=
$
this
->
ra
->
multi
(
$
this
->
ra
->
_target
(
$
key
))->
set
(
$
key
,
'
test2
'
)->
discard
();
// Ret after discard: NULL";
$
this
->
assertNull
(
$
ret
);
// Get after discard, unchanged:
$
this
->
assertEquals
(
'
test1
'
,
$
this
->
ra
->
get
(
$
key
));
}
}
// Test custom distribution function
class
Redis_Distributor_Test
extends
TestSuite {
public
$
ra
=
NULL
;
private
$
min_version
;
public
function
setUp
() {
global
$
new_ring
,
$
old_ring
,
$
useIndex
;
$
options
= [
'
previous
'
=>
$
old_ring
,
'
index
'
=>
$
useIndex
,
'
distributor
'
=> [
$
this
,
'
distribute
'
]
];
if
(
$
this
->
getAuth
()) {
$
options
[
'
auth
'
] =
$
this
->
getAuth
();
}
// create array
$
this
->
ra
=
new
RedisArray
(
$
new_ring
,
$
options
);
$
this
->
min_version
=
getMinVersion
(
$
this
->
ra
);
}
public
function
testInit
() {
$
this
->
ra
->
set
(
'
{uk}test
'
,
'
joe
'
);
$
this
->
ra
->
set
(
'
{us}test
'
,
'
bob
'
);
}
public
function
distribute
(
$
key
) {
$
matches
= [];
if
(
preg_match
(
'
/{([^}]+)}.*/
'
,
$
key
,
$
matches
) ==
1
) {
$
countries
= [
'
uk
'
=>
0
,
'
us
'
=>
1
];
if
(
array_key_exists
(
$
matches
[
1
],
$
countries
)) {
return
$
countries
[
$
matches
[
1
]];
}
}
return
2
;
// default server
}
public
function
testDistribution
() {
$
UK_server
=
$
this
->
ra
->
_target
(
'
{uk}test
'
);
$
US_server
=
$
this
->
ra
->
_target
(
'
{us}test
'
);
$
DE_server
=
$
this
->
ra
->
_target
(
'
{de}test
'
);
$
XX_server
=
$
this
->
ra
->
_target
(
'
{xx}test
'
);
$
nodes
=
$
this
->
ra
->
_hosts
();
$
this
->
assertEquals
(
$
UK_server
,
$
nodes
[
0
]);
$
this
->
assertEquals
(
$
US_server
,
$
nodes
[
1
]);
$
this
->
assertEquals
(
$
DE_server
,
$
nodes
[
2
]);
$
this
->
assertEquals
(
$
XX_server
,
$
nodes
[
2
]);
}
}
function
run_ra_tests
(
$
test_class
,
$
filter
,
$
host
,
array
$
full_ring
,
array
$
sub_ring
,
$
auth
)
{
global
$
new_ring
,
$
old_ring
,
$
server_list
;
$
server_list
=
$
full_ring
;
$
new_ring
=
$
sub_ring
;
$
old_ring
= [];
return
TestSuite::
run
(
$
test_class
,
$
filter
,
$
host
,
NULL
,
$
auth
);
}
?>
Back
|
FazBrowse Home
|
New Git URL