Merge "Improve hidden field validation"
[lhc/web/wiklou.git] / tests / phpunit / includes / objectcache / BagOStuffTest.php
1 <?php
2 /**
3 * This class will test BagOStuff.
4 *
5 * @author Matthias Mullie <mmullie@wikimedia.org>
6 */
7 class BagOStuffTest extends MediaWikiTestCase {
8 private $cache;
9
10 protected function setUp() {
11 parent::setUp();
12
13 // type defined through parameter
14 if ( $this->getCliArg( 'use-bagostuff' ) ) {
15 $name = $this->getCliArg( 'use-bagostuff' );
16
17 $this->cache = ObjectCache::newFromId( $name );
18 } else {
19 // no type defined - use simple hash
20 $this->cache = new HashBagOStuff;
21 }
22
23 $this->cache->delete( wfMemcKey( 'test' ) );
24 }
25
26 public function testMerge() {
27 $key = wfMemcKey( 'test' );
28
29 $usleep = 0;
30
31 /**
32 * Callback method: append "merged" to whatever is in cache.
33 *
34 * @param BagOStuff $cache
35 * @param string $key
36 * @param int $existingValue
37 * @use int $usleep
38 * @return int
39 */
40 $callback = function ( BagOStuff $cache, $key, $existingValue ) use ( &$usleep ) {
41 // let's pretend this is an expensive callback to test concurrent merge attempts
42 usleep( $usleep );
43
44 if ( $existingValue === false ) {
45 return 'merged';
46 }
47
48 return $existingValue . 'merged';
49 };
50
51 // merge on non-existing value
52 $merged = $this->cache->merge( $key, $callback, 0 );
53 $this->assertTrue( $merged );
54 $this->assertEquals( $this->cache->get( $key ), 'merged' );
55
56 // merge on existing value
57 $merged = $this->cache->merge( $key, $callback, 0 );
58 $this->assertTrue( $merged );
59 $this->assertEquals( $this->cache->get( $key ), 'mergedmerged' );
60
61 /*
62 * Test concurrent merges by forking this process, if:
63 * - not manually called with --use-bagostuff
64 * - pcntl_fork is supported by the system
65 * - cache type will correctly support calls over forks
66 */
67 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
68 $fork &= function_exists( 'pcntl_fork' );
69 $fork &= !$this->cache instanceof HashBagOStuff;
70 $fork &= !$this->cache instanceof EmptyBagOStuff;
71 $fork &= !$this->cache instanceof MultiWriteBagOStuff;
72 if ( $fork ) {
73 // callback should take awhile now so that we can test concurrent merge attempts
74 $pid = pcntl_fork();
75 if ( $pid == -1 ) {
76 // can't fork, ignore this test...
77 } elseif ( $pid ) {
78 // wait a little, making sure that the child process is calling merge
79 usleep( 3000 );
80
81 // attempt a merge - this should fail
82 $merged = $this->cache->merge( $key, $callback, 0, 1 );
83
84 // merge has failed because child process was merging (and we only attempted once)
85 $this->assertFalse( $merged );
86
87 // make sure the child's merge is completed and verify
88 usleep( 3000 );
89 $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
90 } else {
91 $this->cache->merge( $key, $callback, 0, 1 );
92
93 // Note: I'm not even going to check if the merge worked, I'll
94 // compare values in the parent process to test if this merge worked.
95 // I'm just going to exit this child process, since I don't want the
96 // child to output any test results (would be rather confusing to
97 // have test output twice)
98 exit;
99 }
100 }
101 }
102
103 public function testAdd() {
104 $key = wfMemcKey( 'test' );
105 $this->assertTrue( $this->cache->add( $key, 'test' ) );
106 }
107
108 public function testGet() {
109 $value = array( 'this' => 'is', 'a' => 'test' );
110
111 $key = wfMemcKey( 'test' );
112 $this->cache->add( $key, $value );
113 $this->assertEquals( $this->cache->get( $key ), $value );
114 }
115
116 /**
117 * @covers BagOStuff::incr
118 */
119 public function testIncr() {
120 $key = wfMemcKey( 'test' );
121 $this->cache->add( $key, 0 );
122 $this->cache->incr( $key );
123 $expectedValue = 1;
124 $actualValue = $this->cache->get( $key );
125 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
126 }
127
128 public function testGetMulti() {
129 $value1 = array( 'this' => 'is', 'a' => 'test' );
130 $value2 = array( 'this' => 'is', 'another' => 'test' );
131
132 $key1 = wfMemcKey( 'test1' );
133 $key2 = wfMemcKey( 'test2' );
134
135 $this->cache->add( $key1, $value1 );
136 $this->cache->add( $key2, $value2 );
137
138 $this->assertEquals(
139 $this->cache->getMulti( array( $key1, $key2 ) ),
140 array( $key1 => $value1, $key2 => $value2 )
141 );
142
143 // cleanup
144 $this->cache->delete( $key1 );
145 $this->cache->delete( $key2 );
146 }
147 }