Slight improvements to FormSpecialPage behavior.
[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
19 } else {
20 // no type defined - use simple hash
21 $this->cache = new HashBagOStuff;
22 }
23
24 $this->cache->delete( wfMemcKey( 'test' ) );
25 }
26
27 protected function tearDown() {
28 }
29
30 public function testMerge() {
31 $key = wfMemcKey( 'test' );
32
33 $usleep = 0;
34
35 /**
36 * Callback method: append "merged" to whatever is in cache.
37 *
38 * @param BagOStuff $cache
39 * @param string $key
40 * @param int $existingValue
41 * @use int $usleep
42 * @return int
43 */
44 $callback = function ( BagOStuff $cache, $key, $existingValue ) use ( &$usleep ) {
45 // let's pretend this is an expensive callback to test concurrent merge attempts
46 usleep( $usleep );
47
48 if ( $existingValue === false ) {
49 return 'merged';
50 }
51
52 return $existingValue . 'merged';
53 };
54
55 // merge on non-existing value
56 $merged = $this->cache->merge( $key, $callback, 0 );
57 $this->assertTrue( $merged );
58 $this->assertEquals( $this->cache->get( $key ), 'merged' );
59
60 // merge on existing value
61 $merged = $this->cache->merge( $key, $callback, 0 );
62 $this->assertTrue( $merged );
63 $this->assertEquals( $this->cache->get( $key ), 'mergedmerged' );
64
65 /*
66 * Test concurrent merges by forking this process, if:
67 * - not manually called with --use-bagostuff
68 * - pcntl_fork is supported by the system
69 * - cache type will correctly support calls over forks
70 */
71 $fork = (bool)$this->getCliArg( 'use-bagostuff=' );
72 $fork &= function_exists( 'pcntl_fork' );
73 $fork &= !$this->cache instanceof HashBagOStuff;
74 $fork &= !$this->cache instanceof EmptyBagOStuff;
75 $fork &= !$this->cache instanceof MultiWriteBagOStuff;
76 if ( $fork ) {
77 // callback should take awhile now so that we can test concurrent merge attempts
78 $usleep = 5000;
79
80 $pid = pcntl_fork();
81 if ( $pid == -1 ) {
82 // can't fork, ignore this test...
83 } elseif ( $pid ) {
84 // wait a little, making sure that the child process is calling merge
85 usleep( 3000 );
86
87 // attempt a merge - this should fail
88 $merged = $this->cache->merge( $key, $callback, 0, 1 );
89
90 // merge has failed because child process was merging (and we only attempted once)
91 $this->assertFalse( $merged );
92
93 // make sure the child's merge is completed and verify
94 usleep( 3000 );
95 $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
96 } else {
97 $this->cache->merge( $key, $callback, 0, 1 );
98
99 // Note: I'm not even going to check if the merge worked, I'll
100 // compare values in the parent process to test if this merge worked.
101 // I'm just going to exit this child process, since I don't want the
102 // child to output any test results (would be rather confusing to
103 // have test output twice)
104 exit;
105 }
106 }
107 }
108
109 public function testAdd() {
110 $key = wfMemcKey( 'test' );
111 $this->assertTrue( $this->cache->add( $key, 'test' ) );
112 }
113
114 public function testGet() {
115 $value = array( 'this' => 'is', 'a' => 'test' );
116
117 $key = wfMemcKey( 'test' );
118 $this->cache->add( $key, $value );
119 $this->assertEquals( $this->cache->get( $key ), $value );
120 }
121
122 public function testGetMulti() {
123 $value1 = array( 'this' => 'is', 'a' => 'test' );
124 $value2 = array( 'this' => 'is', 'another' => 'test' );
125
126 $key1 = wfMemcKey( 'test1' );
127 $key2 = wfMemcKey( 'test2' );
128
129 $this->cache->add( $key1, $value1 );
130 $this->cache->add( $key2, $value2 );
131
132 $this->assertEquals( $this->cache->getMulti( array( $key1, $key2 ) ), array( $key1 => $value1, $key2 => $value2 ) );
133
134 // cleanup
135 $this->cache->delete( $key1 );
136 $this->cache->delete( $key2 );
137 }
138 }