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