MediaWiki  REL1_23
BagOStuffTest.php
Go to the documentation of this file.
00001 <?php
00007 class BagOStuffTest extends MediaWikiTestCase {
00008     private $cache;
00009 
00010     protected function setUp() {
00011         parent::setUp();
00012 
00013         // type defined through parameter
00014         if ( $this->getCliArg( 'use-bagostuff=' ) ) {
00015             $name = $this->getCliArg( 'use-bagostuff=' );
00016 
00017             $this->cache = ObjectCache::newFromId( $name );
00018         } else {
00019             // no type defined - use simple hash
00020             $this->cache = new HashBagOStuff;
00021         }
00022 
00023         $this->cache->delete( wfMemcKey( 'test' ) );
00024     }
00025 
00026     protected function tearDown() {
00027     }
00028 
00029     public function testMerge() {
00030         $key = wfMemcKey( 'test' );
00031 
00032         $usleep = 0;
00033 
00043         $callback = function ( BagOStuff $cache, $key, $existingValue ) use ( &$usleep ) {
00044             // let's pretend this is an expensive callback to test concurrent merge attempts
00045             usleep( $usleep );
00046 
00047             if ( $existingValue === false ) {
00048                 return 'merged';
00049             }
00050 
00051             return $existingValue . 'merged';
00052         };
00053 
00054         // merge on non-existing value
00055         $merged = $this->cache->merge( $key, $callback, 0 );
00056         $this->assertTrue( $merged );
00057         $this->assertEquals( $this->cache->get( $key ), 'merged' );
00058 
00059         // merge on existing value
00060         $merged = $this->cache->merge( $key, $callback, 0 );
00061         $this->assertTrue( $merged );
00062         $this->assertEquals( $this->cache->get( $key ), 'mergedmerged' );
00063 
00064         /*
00065          * Test concurrent merges by forking this process, if:
00066          * - not manually called with --use-bagostuff
00067          * - pcntl_fork is supported by the system
00068          * - cache type will correctly support calls over forks
00069          */
00070         $fork = (bool)$this->getCliArg( 'use-bagostuff=' );
00071         $fork &= function_exists( 'pcntl_fork' );
00072         $fork &= !$this->cache instanceof HashBagOStuff;
00073         $fork &= !$this->cache instanceof EmptyBagOStuff;
00074         $fork &= !$this->cache instanceof MultiWriteBagOStuff;
00075         if ( $fork ) {
00076             // callback should take awhile now so that we can test concurrent merge attempts
00077             $pid = pcntl_fork();
00078             if ( $pid == -1 ) {
00079                 // can't fork, ignore this test...
00080             } elseif ( $pid ) {
00081                 // wait a little, making sure that the child process is calling merge
00082                 usleep( 3000 );
00083 
00084                 // attempt a merge - this should fail
00085                 $merged = $this->cache->merge( $key, $callback, 0, 1 );
00086 
00087                 // merge has failed because child process was merging (and we only attempted once)
00088                 $this->assertFalse( $merged );
00089 
00090                 // make sure the child's merge is completed and verify
00091                 usleep( 3000 );
00092                 $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
00093             } else {
00094                 $this->cache->merge( $key, $callback, 0, 1 );
00095 
00096                 // Note: I'm not even going to check if the merge worked, I'll
00097                 // compare values in the parent process to test if this merge worked.
00098                 // I'm just going to exit this child process, since I don't want the
00099                 // child to output any test results (would be rather confusing to
00100                 // have test output twice)
00101                 exit;
00102             }
00103         }
00104     }
00105 
00106     public function testAdd() {
00107         $key = wfMemcKey( 'test' );
00108         $this->assertTrue( $this->cache->add( $key, 'test' ) );
00109     }
00110 
00111     public function testGet() {
00112         $value = array( 'this' => 'is', 'a' => 'test' );
00113 
00114         $key = wfMemcKey( 'test' );
00115         $this->cache->add( $key, $value );
00116         $this->assertEquals( $this->cache->get( $key ), $value );
00117     }
00118 
00122     public function testIncr() {
00123         $key = wfMemcKey( 'test' );
00124         $this->cache->add( $key, 0 );
00125         $this->cache->incr( $key );
00126         $expectedValue = 1;
00127         $actualValue = $this->cache->get( $key );
00128         $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
00129     }
00130 
00131     public function testGetMulti() {
00132         $value1 = array( 'this' => 'is', 'a' => 'test' );
00133         $value2 = array( 'this' => 'is', 'another' => 'test' );
00134 
00135         $key1 = wfMemcKey( 'test1' );
00136         $key2 = wfMemcKey( 'test2' );
00137 
00138         $this->cache->add( $key1, $value1 );
00139         $this->cache->add( $key2, $value2 );
00140 
00141         $this->assertEquals( $this->cache->getMulti( array( $key1, $key2 ) ), array( $key1 => $value1, $key2 => $value2 ) );
00142 
00143         // cleanup
00144         $this->cache->delete( $key1 );
00145         $this->cache->delete( $key2 );
00146     }
00147 }