Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / objectcache / BagOStuffTest.php
blob987b6e6467c11bc5e6bf70f6baa83dd8a7097d39
1 <?php
2 /**
3 * This class will test BagOStuff.
5 * @author Matthias Mullie <mmullie@wikimedia.org>
6 */
7 class BagOStuffTest extends MediaWikiTestCase {
8 private $cache;
10 protected function setUp() {
11 parent::setUp();
13 // type defined through parameter
14 if ( $this->getCliArg( 'use-bagostuff' ) ) {
15 $name = $this->getCliArg( 'use-bagostuff' );
17 $this->cache = ObjectCache::newFromId( $name );
18 } else {
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' );
29 $usleep = 0;
31 /**
32 * Callback method: append "merged" to whatever is in cache.
34 * @param BagOStuff $cache
35 * @param string $key
36 * @param int $existingValue
37 * @use int $usleep
38 * @return int
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 );
44 if ( $existingValue === false ) {
45 return 'merged';
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;
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 );
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
88 usleep( 3000 );
89 $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
90 } else {
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)
98 exit;
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 );
123 $expectedValue = 1;
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 );
138 $this->assertEquals(
139 $this->cache->getMulti( array( $key1, $key2 ) ),
140 array( $key1 => $value1, $key2 => $value2 )
143 // cleanup
144 $this->cache->delete( $key1 );
145 $this->cache->delete( $key2 );