3 * This class will test BagOStuff.
5 * @author Matthias Mullie <mmullie@wikimedia.org>
7 class BagOStuffTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 // type defined through parameter
14 if ( $this->getCliArg( 'use-bagostuff' ) ) {
15 $name = $this->getCliArg( 'use-bagostuff' );
17 $this->cache
= ObjectCache
::newFromId( $name );
19 // no type defined - use simple hash
20 $this->cache
= new HashBagOStuff
;
23 $this->cache
->delete( wfMemcKey( 'test' ) );
26 public function testMerge() {
27 $key = wfMemcKey( 'test' );
32 * Callback method: append "merged" to whatever is in cache.
34 * @param BagOStuff $cache
36 * @param int $existingValue
40 $callback = function ( BagOStuff
$cache, $key, $existingValue ) use ( &$usleep ) {
41 // let's pretend this is an expensive callback to test concurrent merge attempts
44 if ( $existingValue === false ) {
48 return $existingValue . 'merged';
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' );
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' );
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
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
;
73 // callback should take awhile now so that we can test concurrent merge attempts
76 // can't fork, ignore this test...
78 // wait a little, making sure that the child process is calling merge
81 // attempt a merge - this should fail
82 $merged = $this->cache
->merge( $key, $callback, 0, 1 );
84 // merge has failed because child process was merging (and we only attempted once)
85 $this->assertFalse( $merged );
87 // make sure the child's merge is completed and verify
89 $this->assertEquals( $this->cache
->get( $key ), 'mergedmergedmerged' );
91 $this->cache
->merge( $key, $callback, 0, 1 );
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)
103 public function testAdd() {
104 $key = wfMemcKey( 'test' );
105 $this->assertTrue( $this->cache
->add( $key, 'test' ) );
108 public function testGet() {
109 $value = array( 'this' => 'is', 'a' => 'test' );
111 $key = wfMemcKey( 'test' );
112 $this->cache
->add( $key, $value );
113 $this->assertEquals( $this->cache
->get( $key ), $value );
117 * @covers BagOStuff::incr
119 public function testIncr() {
120 $key = wfMemcKey( 'test' );
121 $this->cache
->add( $key, 0 );
122 $this->cache
->incr( $key );
124 $actualValue = $this->cache
->get( $key );
125 $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
128 public function testGetMulti() {
129 $value1 = array( 'this' => 'is', 'a' => 'test' );
130 $value2 = array( 'this' => 'is', 'another' => 'test' );
132 $key1 = wfMemcKey( 'test1' );
133 $key2 = wfMemcKey( 'test2' );
135 $this->cache
->add( $key1, $value1 );
136 $this->cache
->add( $key2, $value2 );
139 $this->cache
->getMulti( array( $key1, $key2 ) ),
140 array( $key1 => $value1, $key2 => $value2 )
144 $this->cache
->delete( $key1 );
145 $this->cache
->delete( $key2 );