Merge "Added a function to LoginForm to show the "return to" page."
[lhc/web/wiklou.git] / tests / phpunit / includes / objectcache / BagOStuffTest.php
1 <?php
2 /**
3 * This class will test BagOStuff.
4 *
5 * @author Matthias Mullie <mmullie@wikimedia.org>
6 */
7 class BagOStuffTest extends MediaWikiTestCase {
8 private $cache;
9
10 protected function setUp() {
11 parent::setUp();
12
13 // type defined through parameter
14 if ( $this->getCliArg( 'use-bagostuff=' ) ) {
15 $name = $this->getCliArg( 'use-bagostuff=' );
16
17 $this->cache = ObjectCache::newFromId( $name );
18 } else {
19 // no type defined - use simple hash
20 $this->cache = new HashBagOStuff;
21 }
22
23 $this->cache->delete( wfMemcKey( 'test' ) );
24 }
25
26 protected function tearDown() {
27 }
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 $usleep = 5000;
78
79 $pid = pcntl_fork();
80 if ( $pid == -1 ) {
81 // can't fork, ignore this test...
82 } elseif ( $pid ) {
83 // wait a little, making sure that the child process is calling merge
84 usleep( 3000 );
85
86 // attempt a merge - this should fail
87 $merged = $this->cache->merge( $key, $callback, 0, 1 );
88
89 // merge has failed because child process was merging (and we only attempted once)
90 $this->assertFalse( $merged );
91
92 // make sure the child's merge is completed and verify
93 usleep( 3000 );
94 $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
95 } else {
96 $this->cache->merge( $key, $callback, 0, 1 );
97
98 // Note: I'm not even going to check if the merge worked, I'll
99 // compare values in the parent process to test if this merge worked.
100 // I'm just going to exit this child process, since I don't want the
101 // child to output any test results (would be rather confusing to
102 // have test output twice)
103 exit;
104 }
105 }
106 }
107
108 public function testAdd() {
109 $key = wfMemcKey( 'test' );
110 $this->assertTrue( $this->cache->add( $key, 'test' ) );
111 }
112
113 public function testGet() {
114 $value = array( 'this' => 'is', 'a' => 'test' );
115
116 $key = wfMemcKey( 'test' );
117 $this->cache->add( $key, $value );
118 $this->assertEquals( $this->cache->get( $key ), $value );
119 }
120
121 public function testGetMulti() {
122 $value1 = array( 'this' => 'is', 'a' => 'test' );
123 $value2 = array( 'this' => 'is', 'another' => 'test' );
124
125 $key1 = wfMemcKey( 'test1' );
126 $key2 = wfMemcKey( 'test2' );
127
128 $this->cache->add( $key1, $value1 );
129 $this->cache->add( $key2, $value2 );
130
131 $this->assertEquals( $this->cache->getMulti( array( $key1, $key2 ) ), array( $key1 => $value1, $key2 => $value2 ) );
132
133 // cleanup
134 $this->cache->delete( $key1 );
135 $this->cache->delete( $key2 );
136 }
137 }