MediaWiki  1.23.2
BagOStuffTest.php
Go to the documentation of this file.
1 <?php
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 
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 
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  $pid = pcntl_fork();
78  if ( $pid == -1 ) {
79  // can't fork, ignore this test...
80  } elseif ( $pid ) {
81  // wait a little, making sure that the child process is calling merge
82  usleep( 3000 );
83 
84  // attempt a merge - this should fail
85  $merged = $this->cache->merge( $key, $callback, 0, 1 );
86 
87  // merge has failed because child process was merging (and we only attempted once)
88  $this->assertFalse( $merged );
89 
90  // make sure the child's merge is completed and verify
91  usleep( 3000 );
92  $this->assertEquals( $this->cache->get( $key ), 'mergedmergedmerged' );
93  } else {
94  $this->cache->merge( $key, $callback, 0, 1 );
95 
96  // Note: I'm not even going to check if the merge worked, I'll
97  // compare values in the parent process to test if this merge worked.
98  // I'm just going to exit this child process, since I don't want the
99  // child to output any test results (would be rather confusing to
100  // have test output twice)
101  exit;
102  }
103  }
104  }
105 
106  public function testAdd() {
107  $key = wfMemcKey( 'test' );
108  $this->assertTrue( $this->cache->add( $key, 'test' ) );
109  }
110 
111  public function testGet() {
112  $value = array( 'this' => 'is', 'a' => 'test' );
113 
114  $key = wfMemcKey( 'test' );
115  $this->cache->add( $key, $value );
116  $this->assertEquals( $this->cache->get( $key ), $value );
117  }
118 
122  public function testIncr() {
123  $key = wfMemcKey( 'test' );
124  $this->cache->add( $key, 0 );
125  $this->cache->incr( $key );
126  $expectedValue = 1;
127  $actualValue = $this->cache->get( $key );
128  $this->assertEquals( $expectedValue, $actualValue, 'Value should be 1 after incrementing' );
129  }
130 
131  public function testGetMulti() {
132  $value1 = array( 'this' => 'is', 'a' => 'test' );
133  $value2 = array( 'this' => 'is', 'another' => 'test' );
134 
135  $key1 = wfMemcKey( 'test1' );
136  $key2 = wfMemcKey( 'test2' );
137 
138  $this->cache->add( $key1, $value1 );
139  $this->cache->add( $key2, $value2 );
140 
141  $this->assertEquals( $this->cache->getMulti( array( $key1, $key2 ) ), array( $key1 => $value1, $key2 => $value2 ) );
142 
143  // cleanup
144  $this->cache->delete( $key1 );
145  $this->cache->delete( $key2 );
146  }
147 }
BagOStuffTest\setUp
setUp()
Definition: BagOStuffTest.php:10
ObjectCache\newFromId
static newFromId( $id)
Create a new cache object of the specified type.
Definition: ObjectCache.php:64
BagOStuffTest\testGetMulti
testGetMulti()
Definition: BagOStuffTest.php:131
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
HashBagOStuff
This is a test of the interface, mainly.
Definition: HashBagOStuff.php:30
EmptyBagOStuff
A BagOStuff object with no objects in it.
Definition: EmptyBagOStuff.php:29
MediaWikiTestCase\getCliArg
getCliArg( $offset)
Definition: MediaWikiTestCase.php:658
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:43
BagOStuffTest
This class will test BagOStuff.
Definition: BagOStuffTest.php:7
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
BagOStuffTest\testMerge
testMerge()
Definition: BagOStuffTest.php:29
BagOStuffTest\$cache
$cache
Definition: BagOStuffTest.php:8
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3571
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
BagOStuffTest\testIncr
testIncr()
@covers BagOStuff::incr
Definition: BagOStuffTest.php:122
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
BagOStuffTest\testGet
testGet()
Definition: BagOStuffTest.php:111
$value
$value
Definition: styleTest.css.php:45
MultiWriteBagOStuff
A cache class that replicates all writes to multiple child caches.
Definition: MultiWriteBagOStuff.php:31
BagOStuffTest\testAdd
testAdd()
Definition: BagOStuffTest.php:106
BagOStuffTest\tearDown
tearDown()
Definition: BagOStuffTest.php:26