Repository navigation
Expand file tree
/
Copy pathcache-checks.php
More file actions
1112 lines (1029 loc) · 38.8 KB
/
Copy pathcache-checks.php
File metadata and controls
1112 lines (1029 loc) · 38.8 KB
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Correctness checks for the object cache API.
*
* This file is copied into the WordPress root and requested over HTTP,
* so that it runs under php-fpm with the same extensions as the site.
* Each step of a check runs in its own request, so that a step can verify
* what an earlier request wrote to the persistent cache.
*
* Requests:
* ?list JSON list of checks and their step counts
* ?info JSON describing the active cache
* ?check=NAME&step=N Run step N (from 0) of a check
* ?helper=set&key=K&group=G&value=V Set a value from another request,
* where V is base64-encoded serialized data
*
* Each step prints JSON: {"errors": [...], "misses": [...], "skipped": null|string}.
* An error is a wrong value or return value. A miss is a value that should
* have been found but wasn't, which a cache may do, e.g. after an eviction.
*/
ob_start();
register_shutdown_function(
function () {
$error = error_get_last();
if ( $error && in_array( $error['type'], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR ), true ) ) {
while ( ob_get_level() ) {
ob_end_clean();
}
header( 'Content-Type: application/json', true, 200 );
echo json_encode(
array(
'errors' => array( 'Fatal error: ' . $error['message'] . ' in ' . $error['file'] . ':' . $error['line'] ),
'misses' => array(),
'skipped' => null,
)
);
}
}
);
require __DIR__ . '/wp-load.php';
class WCB_Skip extends Exception {}
class WCB_Checker {
public $errors = array();
public $misses = array();
public function error( $message ) {
$this->errors[] = $message;
}
/**
* Check the return value of a write. Returning false is allowed,
* since a cache may fail to store a value, so that is a miss.
*/
public function stored( $label, $actual ) {
if ( false === $actual ) {
$this->misses[] = $label . ': failed to store';
} else {
$this->eq( $label, true, $actual );
}
}
public function eq( $label, $expected, $actual ) {
if ( ! wcb_same( $expected, $actual ) ) {
$this->error( $label . ': ' . wcb_diff( $expected, $actual ) );
}
}
/**
* Check a value returned for a key that should exist.
* Not finding it is a miss. Finding the wrong value is an error.
*/
public function found( $label, $expected, $actual, $found = true ) {
if ( ! $found || ( false === $actual && false !== $expected ) ) {
$this->misses[] = $label . ': expected ' . wcb_export( $expected ) . ', got a miss';
} else {
$this->eq( $label, $expected, $actual );
}
}
/**
* Check the results of a get_multiple. Not finding some keys is a miss.
*/
public function multiple( $label, $expected, $actual ) {
if ( wcb_same( $expected, $actual ) ) {
return;
}
if ( wcb_is_miss( $expected, $actual ) ) {
$this->misses[] = $label . ': ' . wcb_diff( $expected, $actual );
} else {
$this->error( $label . ': ' . wcb_diff( $expected, $actual ) );
}
}
/** Check that wp_cache_get finds the expected value. */
public function get( $label, $key, $group, $expected ) {
$found = null;
$value = wp_cache_get( $key, $group, false, $found );
if ( true !== $found && false !== $found ) {
$this->error( $label . ': $found should be a bool, got ' . wcb_export( $found ) );
}
$this->found( $label, $expected, $value, $found );
}
/** Check that wp_cache_get does not find the key. */
public function absent( $label, $key, $group ) {
$found = null;
$value = wp_cache_get( $key, $group, false, $found );
if ( false !== $found || false !== $value ) {
$this->error( $label . ': expected a miss, got ' . wcb_export( $value ) . ' with $found = ' . wcb_export( $found ) );
}
}
}
/** Strict comparison that compares objects by class and properties. */
function wcb_same( $a, $b ) {
if ( is_object( $a ) && is_object( $b ) ) {
return get_class( $a ) === get_class( $b ) && wcb_same( get_object_vars( $a ), get_object_vars( $b ) );
}
if ( is_array( $a ) && is_array( $b ) ) {
if ( array_keys( $a ) !== array_keys( $b ) ) {
return false;
}
foreach ( $a as $k => $v ) {
if ( ! wcb_same( $v, $b[ $k ] ) ) {
return false;
}
}
return true;
}
return $a === $b;
}
/**
* Whether $actual differs from $expected only by not finding values,
* as false, or as an array of results with false for some keys.
*/
function wcb_is_miss( $expected, $actual ) {
if ( false === $actual ) {
return false !== $expected;
}
if ( ! is_array( $expected ) || ! is_array( $actual ) || array_keys( $expected ) !== array_keys( $actual ) ) {
return false;
}
foreach ( $expected as $k => $v ) {
if ( ! wcb_same( $v, $actual[ $k ] ) && ( false !== $actual[ $k ] || false === $v ) ) {
return false;
}
}
return true;
}
/** Describe how $actual differs from $expected, showing only differing keys of arrays. */
function wcb_diff( $expected, $actual ) {
if ( ! is_array( $expected ) || ! is_array( $actual ) || ( ! $expected && ! $actual ) ) {
return 'expected ' . wcb_export( $expected ) . ', got ' . wcb_export( $actual );
}
$diffs = array();
$expected_keys = array_keys( $expected );
$actual_keys = array_keys( $actual );
if ( $expected_keys !== $actual_keys && array_diff( $expected_keys, $actual_keys ) === array_diff( $actual_keys, $expected_keys ) ) {
$diffs[] = 'keys in a different order: ' . wcb_export( array_keys( $actual ) );
}
foreach ( $expected as $k => $v ) {
if ( ! array_key_exists( $k, $actual ) ) {
$diffs[] = wcb_export( $k ) . ' missing';
} elseif ( ! wcb_same( $v, $actual[ $k ] ) ) {
$diffs[] = wcb_export( $k ) . ': ' . wcb_diff( $v, $actual[ $k ] );
}
}
foreach ( $actual as $k => $v ) {
if ( ! array_key_exists( $k, $expected ) ) {
$diffs[] = 'unexpected ' . wcb_export( $k ) . ' => ' . wcb_export( $v );
}
}
return 'array differs: ' . implode( ', ', $diffs );
}
function wcb_export( $value ) {
$s = is_object( $value )
? get_class( $value ) . json_encode( get_object_vars( $value ) )
: var_export( $value, true );
$s = preg_replace( '/\s+/', ' ', $s );
return strlen( $s ) > 200 ? substr( $s, 0, 200 ) . '…(' . strlen( $s ) . ' bytes)' : $s;
}
function wcb_obj( $props ) {
$o = new stdClass();
foreach ( $props as $k => $v ) {
$o->$k = $v;
}
return $o;
}
/**
* Skip unless there is a persistent cache. Some drop-ins, like
* Docket Cache's, persist without setting wp_using_ext_object_cache().
*/
function wcb_require_persistent() {
if ( ! wp_using_ext_object_cache() && ! file_exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
throw new WCB_Skip( 'No persistent object cache' );
}
}
function wcb_require_support( $feature ) {
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( $feature ) ) {
throw new WCB_Skip( "wp_cache_supports( '$feature' ) is false" );
}
}
/**
* Set a value from a separate request, as another visitor would.
* Returns whether the request succeeded.
*/
function wcb_remote_set( $key, $group, $value ) {
$url = add_query_arg(
array(
'helper' => 'set',
'key' => rawurlencode( $key ),
'group' => rawurlencode( $group ),
'value' => rawurlencode( base64_encode( serialize( $value ) ) ),
),
'http://127.0.0.1:' . $_SERVER['SERVER_PORT'] . '/' . basename( __FILE__ )
);
$resp = wp_remote_get( $url, array( 'timeout' => 30 ) );
return ! is_wp_error( $resp ) && 'ok' === trim( wp_remote_retrieve_body( $resp ) );
}
function wcb_value_types() {
return array(
'string' => 'hello',
'empty-string' => '',
'zero-string' => '0',
'int' => 42,
'zero' => 0,
'negative' => -7,
'float' => 1.5,
'true' => true,
'false' => false,
'null' => null,
'empty-array' => array(),
'array' => array(
'a' => 1,
2 => array( 'nested' => true ),
),
'list' => array( 3, 2, 1 ),
'object' => wcb_obj(
array(
'a' => 1,
'b' => array( 'x' ),
)
),
'binary' => "\x00\x01\xff\xfe" . "\r\n",
'unicode' => 'héllo wörld ✓ 日本',
'numeric-like' => '0123',
'serialized' => serialize( array( 'looks' => 'serialized' ) ),
'large' => str_repeat( 'abcdefghij', 50000 ),
);
}
/**
* Keys that are easy to get wrong. Each pair of keys must not collide.
* Returns a list of [key, group, value].
*/
function wcb_tricky_keys() {
$long = str_repeat( 'k', 300 );
return array(
array( 'colon:key', 'grp', 'v1' ),
array( 'key', 'grp:colon', 'v2' ),
array( 'grp:colon:key', '', 'v3' ),
array( 'grp:key', 'colon', 'v4' ),
array( 'key with spaces', 'grp', 'v5' ),
array( 'key_with_spaces', 'grp', 'v5b' ),
array( "key\twith\ncontrol", 'grp', 'v6' ),
array( 'ключ-日本', 'grp', 'v7' ),
array( $long . 'a', 'grp', 'v8' ),
array( $long . 'b', 'grp', 'v9' ),
array( 'k', str_repeat( 'g', 300 ), 'v10' ),
array( 'slash/key', 'grp', 'v11' ),
array( 'slash', 'grp/key', 'v12' ),
array( 'k_x', 'grp', 'v13' ),
array( 'x', 'grp_k', 'v14' ),
);
}
/** A small deterministic PRNG, so plugins calling mt_rand can't disturb it. */
class WCB_Random {
private $state;
public function __construct( $seed ) {
$this->state = $seed & 0x7fffffff ?: 1;
}
public function int( $n ) {
$this->state = ( $this->state * 1103515245 + 12345 ) & 0x7fffffff;
return ( $this->state >> 8 ) % $n;
}
public function pick( $a ) {
return $a[ $this->int( count( $a ) ) ];
}
}
/**
* Compares random operations against a model of the cache API's documented
* behavior, as implemented by WordPress core's WP_Object_Cache.
*/
class WCB_Model_Test {
const COUNTERS = 'wcb-rand-counters';
const KEYS = array( 'a', 'b', 'c', '7', 'd e', 'x:y' );
const NP = 'wcb-rand-np';
/** group => key => value */
private $model = array();
private $c;
private $count = 0;
private $groups;
private $ops;
private $rand;
private $trace = array();
/** $ops is a list of operations to choose from, repeated to weight them. */
public function __construct( WCB_Checker $c, $seed, $ops, $groups ) {
$this->c = $c;
$this->groups = $groups;
$this->ops = $ops;
$this->rand = new WCB_Random( $seed );
}
public function values() {
// No null, since a cache whose get_multiple returns false for null
// would fail on that before finding anything else.
// The value-types check covers null.
return array( 'v', '', '0', 0, 12, 1.25, true, false, array(), array( 'k' => 'v' ), wcb_obj( array( 'p' => 1 ) ), str_repeat( 'z', 3000 ) );
}
private function has( $key, $group ) {
return isset( $this->model[ $group ] ) && array_key_exists( (string) $key, $this->model[ $group ] );
}
private function model_get( $key, $group ) {
return $this->has( $key, $group ) ? $this->model[ $group ][ (string) $key ] : false;
}
private function model_set( $key, $group, $value ) {
$this->model[ $group ][ (string) $key ] = is_object( $value ) ? clone $value : $value;
}
/** Drop keys that a new request won't have. */
public function new_request() {
unset( $this->model[ self::NP ] );
}
/**
* Returns false on the first divergence.
* Returning false instead of a value is a miss unless $can_miss is false.
*/
private function expect( $op, $expected, $actual, $can_miss = true ) {
if ( wcb_same( $expected, $actual ) ) {
return true;
}
$message = "after {$this->count} ops, $op: " . wcb_diff( $expected, $actual )
. '. Last ops: ' . implode( '; ', array_slice( $this->trace, -8 ) );
if ( $can_miss && wcb_is_miss( $expected, $actual ) ) {
$this->c->misses[] = $message;
} else {
$this->c->error( $message );
}
return false;
}
public function run( $ops ) {
for ( $i = 0; $i < $ops; $i++ ) {
++$this->count;
if ( ! $this->step() ) {
return false;
}
}
return true;
}
private function keys( $n ) {
$keys = array();
for ( $i = 0; $i < $n; $i++ ) {
$keys[] = $this->rand->pick( self::KEYS );
}
return array_values( array_unique( $keys ) );
}
private function step() {
$r = $this->rand;
$group = $r->pick( $this->groups );
$key = $r->pick( self::KEYS );
$value = $r->pick( $this->values() );
$op = $r->pick( $this->ops );
$desc = "$op($key, $group)";
switch ( $op ) {
case 'get':
$found = null;
$actual = wp_cache_get( $key, $group, false, $found );
$this->trace[] = $desc;
return $this->expect( $desc, $this->model_get( $key, $group ), $actual )
&& $this->expect( "$desc \$found", $this->has( $key, $group ), $found );
case 'set':
$this->trace[] = "$desc = " . wcb_export( $value );
$this->model_set( $key, $group, $value );
return $this->expect( $desc, true, wp_cache_set( $key, $value, $group ) );
case 'add':
$this->trace[] = "$desc = " . wcb_export( $value );
$expected = ! $this->has( $key, $group );
if ( $expected ) {
$this->model_set( $key, $group, $value );
}
return $this->expect( $desc, $expected, wp_cache_add( $key, $value, $group ) );
case 'replace':
$this->trace[] = "$desc = " . wcb_export( $value );
$expected = $this->has( $key, $group );
if ( $expected ) {
$this->model_set( $key, $group, $value );
}
return $this->expect( $desc, $expected, wp_cache_replace( $key, $value, $group ) );
case 'delete':
$this->trace[] = $desc;
$expected = $this->has( $key, $group );
unset( $this->model[ $group ][ (string) $key ] );
return $this->expect( $desc, $expected, wp_cache_delete( $key, $group ) );
case 'get_multiple':
$keys = $this->keys( 1 + $r->int( 4 ) );
$this->trace[] = 'get_multiple([' . implode( ',', $keys ) . "], $group)";
$expected = array();
foreach ( $keys as $k ) {
$expected[ $k ] = $this->model_get( $k, $group );
}
return $this->expect( end( $this->trace ), $expected, wp_cache_get_multiple( $keys, $group ) );
case 'set_multiple':
case 'add_multiple':
$data = array();
$expected = array();
foreach ( $this->keys( 1 + $r->int( 4 ) ) as $k ) {
$data[ $k ] = $r->pick( $this->values() );
}
$this->trace[] = "$op(" . wcb_export( $data ) . ", $group)";
foreach ( $data as $k => $v ) {
$expected[ $k ] = 'set_multiple' === $op || ! $this->has( $k, $group );
if ( $expected[ $k ] ) {
$this->model_set( $k, $group, $v );
}
}
$actual = 'set_multiple' === $op ? wp_cache_set_multiple( $data, $group ) : wp_cache_add_multiple( $data, $group );
return $this->expect( end( $this->trace ), $expected, $actual );
case 'delete_multiple':
$keys = $this->keys( 1 + $r->int( 4 ) );
$this->trace[] = 'delete_multiple([' . implode( ',', $keys ) . "], $group)";
$expected = array();
foreach ( $keys as $k ) {
$expected[ $k ] = $this->has( $k, $group );
unset( $this->model[ $group ][ (string) $k ] );
}
return $this->expect( end( $this->trace ), $expected, wp_cache_delete_multiple( $keys, $group ) );
case 'incr':
case 'decr':
// Counters only ever hold ints. Behavior with other values varies
// between backends and isn't a correctness problem.
$group = self::COUNTERS;
$offset = $r->int( 10 );
$desc = "$op($key, $offset, $group)";
if ( ! $this->has( $key, $group ) ) {
$this->trace[] = "set($key, $group) = 5";
$this->model_set( $key, $group, 5 );
if ( ! $this->expect( "set($key, $group)", true, wp_cache_set( $key, 5, $group ) ) ) {
return false;
}
}
$this->trace[] = $desc;
$new = max( 0, $this->model_get( $key, $group ) + ( 'incr' === $op ? $offset : -$offset ) );
$this->model_set( $key, $group, $new );
$actual = 'incr' === $op ? wp_cache_incr( $key, $offset, $group ) : wp_cache_decr( $key, $offset, $group );
// The key was just set or changed, so false is not a miss.
return $this->expect( $desc, $new, $actual, false );
case 'mutate':
// Changing a fetched object must not change the cached value.
$this->trace[] = "mutate($key, $group)";
$actual = wp_cache_get( $key, $group );
if ( is_object( $actual ) ) {
$actual->p = 'mutated';
}
return true;
}
}
/** Check every key in the model against the cache. */
public function verify() {
foreach ( array_merge( $this->groups, array( self::COUNTERS ) ) as $group ) {
$expected = array();
foreach ( self::KEYS as $k ) {
$expected[ $k ] = $this->model_get( $k, $group );
}
if ( ! $this->expect( "verify get_multiple(all keys, $group)", $expected, wp_cache_get_multiple( self::KEYS, $group ) ) ) {
return false;
}
}
return true;
}
private static function state_file( $seed ) {
return WP_CONTENT_DIR . "/wcb-model-$seed.ser";
}
/** Save the model for the next request to load. */
public function save( $seed ) {
file_put_contents( self::state_file( $seed ), serialize( $this->model ) );
}
public static function forget( $seed ) {
if ( file_exists( self::state_file( $seed ) ) ) {
unlink( self::state_file( $seed ) );
}
}
public function load( $seed ) {
if ( ! file_exists( self::state_file( $seed ) ) ) {
throw new WCB_Skip( 'An earlier step failed' );
}
$this->model = unserialize( file_get_contents( self::state_file( $seed ) ) );
$this->new_request();
}
}
/**
* Each check is a list of steps. Each step runs in its own request,
* so later steps see only what was persisted.
* The first step of each check starts with a flushed cache.
*/
function wcb_checks() {
$checks = array();
$checks['get-missing'] = array(
function ( WCB_Checker $c ) {
$c->absent( 'get missing key', 'missing', 'wcb' );
$c->multiple( 'get_multiple missing keys', array( 'm1' => false, 'm2' => false ), wp_cache_get_multiple( array( 'm1', 'm2' ), 'wcb' ) );
$c->eq( 'delete missing key', false, wp_cache_delete( 'missing', 'wcb' ) );
$c->eq( 'replace missing key', false, wp_cache_replace( 'missing', 'v', 'wcb' ) );
$c->absent( 'replace must not create the key', 'missing', 'wcb' );
$c->eq( 'incr missing key', false, wp_cache_incr( 'missing', 1, 'wcb' ) );
$c->eq( 'decr missing key', false, wp_cache_decr( 'missing', 1, 'wcb' ) );
$c->absent( 'incr must not create the key', 'missing', 'wcb' );
},
);
$checks['value-types'] = array(
function ( WCB_Checker $c ) {
foreach ( wcb_value_types() as $name => $value ) {
$c->stored( "set $name", wp_cache_set( "type-$name", $value, 'wcb' ) );
$c->get( "get $name", "type-$name", 'wcb', $value );
}
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$expected = array();
foreach ( wcb_value_types() as $name => $value ) {
$expected[ "type-$name" ] = $value;
}
$c->multiple( 'get_multiple persisted types', $expected, wp_cache_get_multiple( array_keys( $expected ), 'wcb' ) );
// After get_multiple, so that get_multiple reads from the persistent cache.
foreach ( wcb_value_types() as $name => $value ) {
$c->get( "get persisted $name", "type-$name", 'wcb', $value );
}
},
);
$checks['keys'] = array(
function ( WCB_Checker $c ) {
foreach ( wcb_tricky_keys() as list( $key, $group, $value ) ) {
$c->stored( 'set ' . wcb_export( "$group/$key" ), wp_cache_set( $key, $value, $group ) );
}
foreach ( wcb_tricky_keys() as list( $key, $group, $value ) ) {
$c->get( 'get ' . wcb_export( "$group/$key" ), $key, $group, $value );
}
// Int keys and numeric string keys are the same key.
wp_cache_set( 123, 'int', 'wcb' );
$c->get( "get '123' after set 123", '123', 'wcb', 'int' );
wp_cache_set( 'k', 'default group', '' );
$c->get( "group '' is 'default'", 'k', 'default', 'default group' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
foreach ( wcb_tricky_keys() as list( $key, $group, $value ) ) {
$c->get( 'get persisted ' . wcb_export( "$group/$key" ), $key, $group, $value );
}
$c->get( "get persisted '123' after set 123", '123', 'wcb', 'int' );
$c->get( "persisted group '' is 'default'", 'k', 'default', 'default group' );
},
);
$checks['groups'] = array(
function ( WCB_Checker $c ) {
wp_cache_set( 'same', 'g1', 'wcb-g1' );
wp_cache_set( 'same', 'g2', 'wcb-g2' );
$c->get( 'group 1', 'same', 'wcb-g1', 'g1' );
$c->get( 'group 2', 'same', 'wcb-g2', 'g2' );
wp_cache_delete( 'same', 'wcb-g1' );
$c->absent( 'deleted from group 1', 'same', 'wcb-g1' );
$c->get( 'group 2 after delete from group 1', 'same', 'wcb-g2', 'g2' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->absent( 'persisted delete from group 1', 'same', 'wcb-g1' );
$c->get( 'persisted group 2', 'same', 'wcb-g2', 'g2' );
},
);
$checks['add-replace-delete'] = array(
function ( WCB_Checker $c ) {
$c->eq( 'add new key', true, wp_cache_add( 'k', 'v1', 'wcb' ) );
$c->eq( 'add existing key', false, wp_cache_add( 'k', 'v2', 'wcb' ) );
$c->get( 'value after failed add', 'k', 'wcb', 'v1' );
$c->eq( 'replace existing key', true, wp_cache_replace( 'k', 'v3', 'wcb' ) );
$c->get( 'value after replace', 'k', 'wcb', 'v3' );
$c->eq( 'delete existing key', true, wp_cache_delete( 'k', 'wcb' ) );
$c->absent( 'value after delete', 'k', 'wcb' );
$c->eq( 'add after delete', true, wp_cache_add( 'k', 'v4', 'wcb' ) );
$c->get( 'value after add after delete', 'k', 'wcb', 'v4' );
$c->eq( 'add false value', true, wp_cache_add( 'false', false, 'wcb' ) );
$c->eq( 'add over false value', false, wp_cache_add( 'false', 'v', 'wcb' ) );
$c->eq( 'replace false value', true, wp_cache_replace( 'false', 'v', 'wcb' ) );
wp_cache_set( 'persisted', 'p1', 'wcb' );
wp_cache_set( 'to-delete', 'd', 'wcb' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
// These keys are in the persistent cache but not yet in this request's memory.
$c->eq( 'add key that exists only in the persistent cache', false, wp_cache_add( 'persisted', 'p2', 'wcb' ) );
$c->get( 'value after failed add', 'persisted', 'wcb', 'p1' );
$c->eq( 'replace key that exists only in the persistent cache', true, wp_cache_replace( 'to-delete', 'r', 'wcb' ) );
$c->eq( 'delete key that exists only in the persistent cache', true, wp_cache_delete( 'persisted', 'wcb' ) );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->absent( 'persisted delete', 'persisted', 'wcb' );
$c->get( 'persisted replace', 'to-delete', 'wcb', 'r' );
},
);
$checks['incr-decr'] = array(
function ( WCB_Checker $c ) {
wp_cache_set( 'n', 5, 'wcb' );
$c->eq( 'incr 5 by 3', 8, wp_cache_incr( 'n', 3, 'wcb' ) );
$c->get( 'value after incr', 'n', 'wcb', 8 );
$c->eq( 'decr 8 by 2', 6, wp_cache_decr( 'n', 2, 'wcb' ) );
$c->eq( 'decr 6 by 10 stops at 0', 0, wp_cache_decr( 'n', 10, 'wcb' ) );
$c->get( 'value after decr below 0', 'n', 'wcb', 0 );
wp_cache_set( 's', '10', 'wcb' );
$c->eq( "incr '10' by 1", 11, wp_cache_incr( 's', 1, 'wcb' ) );
wp_cache_set( 'p', 100, 'wcb' );
wp_cache_incr( 'p', 1, 'wcb' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->get( 'persisted incr', 'p', 'wcb', 101 );
$c->eq( 'incr persisted value', 102, wp_cache_incr( 'p', 1, 'wcb' ) );
$c->eq( 'decr persisted value', 100, wp_cache_decr( 'p', 2, 'wcb' ) );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->get( 'persisted incr and decr', 'p', 'wcb', 100 );
},
);
$checks['object-isolation'] = array(
function ( WCB_Checker $c ) {
$o = wcb_obj( array( 'v' => 1 ) );
wp_cache_set( 'o', $o, 'wcb' );
$o->v = 2;
$c->get( 'set copies the object', 'o', 'wcb', wcb_obj( array( 'v' => 1 ) ) );
$got = wp_cache_get( 'o', 'wcb' );
if ( is_object( $got ) ) {
$got->v = 3;
}
$c->get( 'get returns a copy', 'o', 'wcb', wcb_obj( array( 'v' => 1 ) ) );
$got = wp_cache_get_multiple( array( 'o' ), 'wcb' );
if ( is_object( $got['o'] ?? null ) ) {
$got['o']->v = 4;
}
$c->get( 'get_multiple returns copies', 'o', 'wcb', wcb_obj( array( 'v' => 1 ) ) );
$o2 = wcb_obj( array( 'v' => 1 ) );
wp_cache_set_multiple( array( 'o2' => $o2 ), 'wcb' );
$o2->v = 5;
$c->get( 'set_multiple copies objects', 'o2', 'wcb', wcb_obj( array( 'v' => 1 ) ) );
$o3 = wcb_obj( array( 'v' => 1 ) );
wp_cache_add( 'o3', $o3, 'wcb' );
$o3->v = 6;
$c->get( 'add copies the object', 'o3', 'wcb', wcb_obj( array( 'v' => 1 ) ) );
$o4 = wcb_obj( array( 'v' => 1 ) );
wp_cache_add_multiple( array( 'o4' => $o4 ), 'wcb' );
$o4->v = 7;
$c->get( 'add_multiple copies objects', 'o4', 'wcb', wcb_obj( array( 'v' => 1 ) ) );
wp_cache_add_non_persistent_groups( array( 'wcb-np' ) );
$o5 = wcb_obj( array( 'v' => 1 ) );
wp_cache_set( 'o5', $o5, 'wcb-np' );
$o5->v = 8;
$c->get( 'set copies the object in a non-persistent group', 'o5', 'wcb-np', wcb_obj( array( 'v' => 1 ) ) );
$got = wp_cache_get( 'o5', 'wcb-np' );
if ( is_object( $got ) ) {
$got->v = 9;
}
$c->get( 'get returns a copy in a non-persistent group', 'o5', 'wcb-np', wcb_obj( array( 'v' => 1 ) ) );
},
);
$checks['multiple'] = array(
function ( WCB_Checker $c ) {
$c->eq(
'set_multiple returns results by key',
array( 'a' => true, 'b' => true, 5 => true ),
wp_cache_set_multiple( array( 'a' => 'A', 'b' => 'B', 5 => 'five' ), 'wcb' )
);
$c->get( 'get after set_multiple', 'a', 'wcb', 'A' );
$c->get( 'get int key after set_multiple', 5, 'wcb', 'five' );
$c->multiple(
'get_multiple keeps order and caller keys',
array( 'b' => 'B', 'missing' => false, 'a' => 'A', 5 => 'five' ),
wp_cache_get_multiple( array( 'b', 'missing', 'a', 5 ), 'wcb' )
);
$c->eq(
'add_multiple adds only new keys',
array( 'a' => false, 'c' => true ),
wp_cache_add_multiple( array( 'a' => 'ignored', 'c' => 'C' ), 'wcb' )
);
$c->get( 'value after add_multiple skipped it', 'a', 'wcb', 'A' );
$c->get( 'value added by add_multiple', 'c', 'wcb', 'C' );
$c->eq(
'delete_multiple returns results by key',
array( 'a' => true, 'missing' => false, 5 => true ),
wp_cache_delete_multiple( array( 'a', 'missing', 5 ), 'wcb' )
);
$c->absent( 'get after delete_multiple', 'a', 'wcb' );
$c->multiple(
'get_multiple after delete_multiple',
array( 'a' => false, 'b' => 'B', 5 => false ),
wp_cache_get_multiple( array( 'a', 'b', 5 ), 'wcb' )
);
$c->multiple(
'get_multiple with falsy values',
array( 'f' => false, 'z' => 0, 'e' => '', 'n' => null ),
( function () {
wp_cache_set_multiple( array( 'f' => false, 'z' => 0, 'e' => '', 'n' => null ), 'wcb' );
return wp_cache_get_multiple( array( 'f', 'z', 'e', 'n' ), 'wcb' );
} )()
);
$c->multiple( 'get_multiple with no keys', array(), wp_cache_get_multiple( array(), 'wcb' ) );
// Same keys in other groups must not interfere.
wp_cache_set_multiple( array( 'b' => 'other' ), 'wcb-other' );
$c->get( 'set_multiple in another group', 'b', 'wcb', 'B' );
wp_cache_add_non_persistent_groups( array( 'wcb-np' ) );
$c->eq(
'set_multiple in a non-persistent group',
array( 'x' => true, 'y' => true ),
wp_cache_set_multiple( array( 'x' => 'X', 'y' => 'Y' ), 'wcb-np' )
);
$c->get( 'get after set_multiple in a non-persistent group', 'x', 'wcb-np', 'X' );
$c->eq(
'add_multiple in a non-persistent group',
array( 'x' => false, 'z' => true ),
wp_cache_add_multiple( array( 'x' => 'ignored', 'z' => 'Z' ), 'wcb-np' )
);
$c->get( 'value after add_multiple skipped it in a non-persistent group', 'x', 'wcb-np', 'X' );
$c->get( 'get after add_multiple in a non-persistent group', 'z', 'wcb-np', 'Z' );
$c->multiple(
'get_multiple in a non-persistent group',
array( 'x' => 'X', 'y' => 'Y', 'z' => 'Z', 'w' => false ),
wp_cache_get_multiple( array( 'x', 'y', 'z', 'w' ), 'wcb-np' )
);
$c->eq(
'delete_multiple in a non-persistent group',
array( 'x' => true, 'w' => false ),
wp_cache_delete_multiple( array( 'x', 'w' ), 'wcb-np' )
);
$c->absent( 'get after delete_multiple in a non-persistent group', 'x', 'wcb-np' );
wp_cache_set_multiple( array( 'p1' => 'P1', 'p2' => 'P2', 'p3' => 'P3' ), 'wcb-p' );
wp_cache_delete_multiple( array( 'p2' ), 'wcb-p' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->multiple(
'persisted set_multiple and delete_multiple',
array( 'p1' => 'P1', 'p2' => false, 'p3' => 'P3' ),
wp_cache_get_multiple( array( 'p1', 'p2', 'p3' ), 'wcb-p' )
);
$c->get( 'persisted set_multiple', 'p3', 'wcb-p', 'P3' );
$c->eq(
'add_multiple with keys that exist only in the persistent cache',
array( 'p1' => false, 'p2' => true ),
wp_cache_add_multiple( array( 'p1' => 'ignored', 'p2' => 'P2b' ), 'wcb-p' )
);
$c->eq(
'delete_multiple with keys that exist only in the persistent cache',
array( 'p3' => true ),
wp_cache_delete_multiple( array( 'p3' ), 'wcb-p' )
);
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->multiple(
'persisted add_multiple and delete_multiple',
array( 'p1' => 'P1', 'p2' => 'P2b', 'p3' => false ),
wp_cache_get_multiple( array( 'p1', 'p2', 'p3' ), 'wcb-p' )
);
},
);
$checks['non-persistent'] = array(
function ( WCB_Checker $c ) {
wp_cache_add_non_persistent_groups( array( 'wcb-np' ) );
wp_cache_set( 'k', 'v', 'wcb-np' );
wp_cache_add( 'a', 'v', 'wcb-np' );
wp_cache_set_multiple( array( 'm' => 'v' ), 'wcb-np' );
wp_cache_add_multiple( array( 'am' => 'v' ), 'wcb-np' );
$c->get( 'get in a non-persistent group', 'k', 'wcb-np', 'v' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
// Not registered as non-persistent in this request, so this reads the persistent cache.
foreach ( array( 'k', 'a', 'm', 'am' ) as $k ) {
$c->absent( "non-persistent key $k must not be persisted", $k, 'wcb-np' );
}
},
);
$checks['flush'] = array(
function ( WCB_Checker $c ) {
wp_cache_set( 'k', 'v', 'wcb' );
wp_cache_add_non_persistent_groups( array( 'wcb-np' ) );
wp_cache_set( 'k', 'v', 'wcb-np' );
$c->eq( 'flush', true, wp_cache_flush() );
$c->absent( 'get after flush', 'k', 'wcb' );
$c->absent( 'get from a non-persistent group after flush', 'k', 'wcb-np' );
wp_cache_set( 'after', 'v', 'wcb' );
$c->get( 'set after flush', 'after', 'wcb', 'v' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->absent( 'persisted flush', 'k', 'wcb' );
$c->get( 'persisted set after flush', 'after', 'wcb', 'v' );
},
);
$checks['flush-group'] = array(
function ( WCB_Checker $c ) {
wcb_require_support( 'flush_group' );
wp_cache_set( 'k', 'v1', 'wcb-fg1' );
wp_cache_set( 'k', 'v2', 'wcb-fg2' );
wp_cache_set( 'k2', 'v3', 'wcb-fg1' );
$c->eq( 'flush_group', true, wp_cache_flush_group( 'wcb-fg1' ) );
$c->absent( 'get after flush_group', 'k', 'wcb-fg1' );
$c->absent( 'get other key after flush_group', 'k2', 'wcb-fg1' );
$c->get( 'other group after flush_group', 'k', 'wcb-fg2', 'v2' );
wp_cache_set( 'after', 'v4', 'wcb-fg1' );
$c->get( 'set after flush_group', 'after', 'wcb-fg1', 'v4' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->absent( 'persisted flush_group', 'k', 'wcb-fg1' );
$c->absent( 'persisted flush_group, other key', 'k2', 'wcb-fg1' );
$c->get( 'persisted other group after flush_group', 'k', 'wcb-fg2', 'v2' );
$c->get( 'persisted set after flush_group', 'after', 'wcb-fg1', 'v4' );
// Flush a group that is only in the persistent cache.
wp_cache_flush_group( 'wcb-fg2' );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
$c->absent( 'persisted flush_group of a group not loaded in that request', 'k', 'wcb-fg2' );
},
);
$checks['flush-runtime'] = array(
function ( WCB_Checker $c ) {
wcb_require_persistent();
wcb_require_support( 'flush_runtime' );
wp_cache_set( 'k', 'v', 'wcb' );
$c->eq( 'flush_runtime', true, wp_cache_flush_runtime() );
$c->get( 'get after flush_runtime', 'k', 'wcb', 'v' );
},
);
$checks['expiration'] = array(
function ( WCB_Checker $c ) {
wcb_require_persistent();
wp_cache_set( 'short', 'v', 'wcb', 2 );
wp_cache_set( 'none', 'v', 'wcb', 0 );
wp_cache_set( 'long', 'v', 'wcb', 3600 );
// More than 30 days, which memcached would read as a Unix timestamp.
wp_cache_set( 'month', 'v', 'wcb', 40 * DAY_IN_SECONDS );
wp_cache_add( 'add-short', 'v', 'wcb', 2 );
wp_cache_set_multiple( array( 'multi-short' => 'v' ), 'wcb', 2 );
wp_cache_add_multiple( array( 'add-multi-short' => 'v' ), 'wcb', 2 );
wp_cache_set( 'replace-short', 'v', 'wcb' );
wp_cache_replace( 'replace-short', 'v2', 'wcb', 2 );
set_transient( 'wcb_transient_short', 'v', 2 );
set_transient( 'wcb_transient_long', 'v', 3600 );
},
function ( WCB_Checker $c ) {
wcb_require_persistent();
sleep( 4 );
foreach ( array( 'short', 'add-short', 'multi-short', 'add-multi-short', 'replace-short' ) as $k ) {
$c->absent( "$k expired", $k, 'wcb' );
}
$c->get( 'no expiration', 'none', 'wcb', 'v' );
$c->get( 'long expiration', 'long', 'wcb', 'v' );
$c->get( 'expiration over 30 days', 'month', 'wcb', 'v' );
$c->eq( 'transient expired', false, get_transient( 'wcb_transient_short' ) );
$c->found( 'transient not expired', 'v', get_transient( 'wcb_transient_long' ) );
},
);
$checks['transients'] = array(
function ( WCB_Checker $c ) {
$c->eq( 'set_transient', true, set_transient( 'wcb_t', array( 'v' => 1 ) ) );
$c->found( 'get_transient', array( 'v' => 1 ), get_transient( 'wcb_t' ) );
set_transient( 'wcb_t_del', 'v' );
set_site_transient( 'wcb_st', 'site' );
},
function ( WCB_Checker $c ) {
$c->found( 'persisted transient', array( 'v' => 1 ), get_transient( 'wcb_t' ) );
$c->found( 'persisted site transient', 'site', get_site_transient( 'wcb_st' ) );
$c->eq( 'delete_transient', true, delete_transient( 'wcb_t_del' ) );
},
function ( WCB_Checker $c ) {
$c->eq( 'persisted delete_transient', false, get_transient( 'wcb_t_del' ) );
},
);
$checks['options'] = array(
function ( WCB_Checker $c ) {
update_option( 'wcb_option', 'v1' );
update_option( 'wcb_option_noautoload', 'n1', false );
},
function ( WCB_Checker $c ) {
$c->eq( 'persisted option', 'v1', get_option( 'wcb_option' ) );
$c->eq( 'persisted non-autoloaded option', 'n1', get_option( 'wcb_option_noautoload' ) );
update_option( 'wcb_option', 'v2' );
delete_option( 'wcb_option_noautoload' );
},
function ( WCB_Checker $c ) {
$c->eq( 'updated option', 'v2', get_option( 'wcb_option' ) );
$c->eq( 'deleted option', false, get_option( 'wcb_option_noautoload' ) );
delete_option( 'wcb_option' );
},
);
$checks['other-request'] = array(
function ( WCB_Checker $c ) {
wcb_require_persistent();
wp_cache_set( 'k', 'v1', 'wcb' );
$c->get( 'before the other request', 'k', 'wcb', 'v1' );
if ( ! wcb_remote_set( 'k', 'wcb', 'v2' ) ) {
$c->error( 'The helper request failed' );
return;
}
$c->found( 'get with force after another request set the key', 'v2', wp_cache_get( 'k', 'wcb', true ) );
wcb_remote_set( 'k2', 'wcb', 'other' );
$c->found( 'get a key that another request set', 'other', wp_cache_get( 'k2', 'wcb' ) );
$c->eq( 'add a key that another request set', false, wp_cache_add( 'k2', 'mine', 'wcb' ) );
},
);
// Several requests of random operations, verified against a model.
// Each step saves the model for the next step to load.
// Counters are separate so that a broken incr or decr doesn't hide
// problems with the other operations.
// Non-persistent groups are separate too.
$all_ops = array( 'get', 'get', 'get', 'set', 'set', 'add', 'replace', 'delete', 'get_multiple', 'set_multiple', 'add_multiple', 'delete_multiple', 'mutate' );
$families = array(
'random' => array( $all_ops, array( 'wcb-rand-a', 'wcb-rand-b' ) ),
'random-counters' => array( array( 'get', 'delete', 'incr', 'incr', 'decr', 'decr' ), array( 'wcb-rand-a' ) ),
'random-non-persistent' => array( $all_ops, array( WCB_Model_Test::NP ) ),
);
$requests = 3;
$ops = 400;
$runs = array();
foreach ( $families as $family => list( $op_names, $groups ) ) {
foreach ( array( 1, 2, 3 ) as $n ) {
$runs[ "$family-$n" ] = array( count( $runs ) + 1, $op_names, $groups );
}
}