3 * @author Matthias Mullie <mmullie@wikimedia.org>
5 class BagOStuffTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 // type defined through parameter
12 if ( $this->getCliArg( 'use-bagostuff' ) ) {
13 $name = $this->getCliArg( 'use-bagostuff' );
15 $this->cache
= ObjectCache
::newFromId( $name );
17 // no type defined - use simple hash
18 $this->cache
= new HashBagOStuff
;
21 $this->cache
->delete( wfMemcKey( 'test' ) );
25 * @covers BagOStuff::merge
26 * @covers BagOStuff::mergeViaLock
28 public function testMerge() {
29 $key = wfMemcKey( 'test' );
34 * Callback method: append "merged" to whatever is in cache.
36 * @param BagOStuff $cache
38 * @param int $existingValue
42 $callback = function ( BagOStuff
$cache, $key, $existingValue ) use ( &$usleep ) {
43 // let's pretend this is an expensive callback to test concurrent merge attempts
46 if ( $existingValue === false ) {
50 return $existingValue . 'merged';
53 // merge on non-existing value
54 $merged = $this->cache
->merge( $key, $callback, 0 );
55 $this->assertTrue( $merged );
56 $this->assertEquals( $this->cache
->get( $key ), 'merged' );
58 // merge on existing value
59 $merged = $this->cache
->merge( $key, $callback, 0 );
60 $this->assertTrue( $merged );
61 $this->assertEquals( $this->cache
->get( $key ), 'mergedmerged' );
64 * Test concurrent merges by forking this process, if:
65 * - not manually called with --use-bagostuff
66 * - pcntl_fork is supported by the system
67 * - cache type will correctly support calls over forks
69 $fork = (bool)$this->getCliArg( 'use-bagostuff' );
70 $fork &= function_exists( 'pcntl_fork' );
71 $fork &= !$this->cache
instanceof HashBagOStuff
;
72 $fork &= !$this->cache
instanceof EmptyBagOStuff
;
73 $fork &= !$this->cache
instanceof MultiWriteBagOStuff
;
75 // callback should take awhile now so that we can test concurrent merge attempts
78 // can't fork, ignore this test...
80 // wait a little, making sure that the child process is calling merge
83 // attempt a merge - this should fail
84 $merged = $this->cache
->merge( $key, $callback, 0, 1 );
86 // merge has failed because child process was merging (and we only attempted once)
87 $this->assertFalse( $merged );
89 // make sure the child's merge is completed and verify
91 $this->assertEquals( $this->cache
->get( $key ), 'mergedmergedmerged' );
93 $this->cache
->merge( $key, $callback, 0, 1 );
95 // Note: I'm not even going to check if the merge worked, I'll
96 // compare values in the parent process to test if this merge worked.
97 // I'm just going to exit this child process, since I don't want the
98 // child to output any test results (would be rather confusing to
99 // have test output twice)
106 * @covers BagOStuff::add
108 public function testAdd() {
109 $key = wfMemcKey( 'test' );
110 $this->assertTrue( $this->cache
->add( $key, 'test' ) );
113 public function testGet() {
114 $value = array( 'this' => 'is', 'a' => 'test' );
116 $key = wfMemcKey( 'test' );
117 $this->cache
->add( $key, $value );
118 $this->assertEquals( $this->cache
->get( $key ), $value );
122 * @covers BagOStuff::incr
124 public function testIncr() {
125 $key = wfMemcKey( 'test' );
126 $this->cache
->add( $key, 0 );
127 $this->cache
->incr( $key );
129 $actualValue = $this->cache
->get( $key );
130 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
134 * @covers BagOStuff::getMulti
136 public function testGetMulti() {
137 $value1 = array( 'this' => 'is', 'a' => 'test' );
138 $value2 = array( 'this' => 'is', 'another' => 'test' );
140 $key1 = wfMemcKey( 'test1' );
141 $key2 = wfMemcKey( 'test2' );
143 $this->cache
->add( $key1, $value1 );
144 $this->cache
->add( $key2, $value2 );
147 $this->cache
->getMulti( array( $key1, $key2 ) ),
148 array( $key1 => $value1, $key2 => $value2 )
152 $this->cache
->delete( $key1 );
153 $this->cache
->delete( $key2 );