MediaWiki  REL1_31
MultiWriteBagOStuffTest.php
Go to the documentation of this file.
1 <?php
2 
8  private $cache1;
10  private $cache2;
12  private $cache;
13 
14  protected function setUp() {
15  parent::setUp();
16 
17  $this->cache1 = new HashBagOStuff();
18  $this->cache2 = new HashBagOStuff();
19  $this->cache = new MultiWriteBagOStuff( [
20  'caches' => [ $this->cache1, $this->cache2 ],
21  'replication' => 'async',
22  'asyncHandler' => 'DeferredUpdates::addCallableUpdate'
23  ] );
24  }
25 
30  public function testSetImmediate() {
31  $key = wfRandomString();
33  $this->cache->set( $key, $value );
34 
35  // Set in tier 1
36  $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
37  // Set in tier 2
38  $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
39  }
40 
44  public function testSyncMerge() {
45  $key = wfRandomString();
47  $func = function () use ( $value ) {
48  return $value;
49  };
50 
51  // XXX: DeferredUpdates bound to transactions in CLI mode
52  $dbw = wfGetDB( DB_MASTER );
53  $dbw->begin();
54  $this->cache->merge( $key, $func );
55 
56  // Set in tier 1
57  $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
58  // Not yet set in tier 2
59  $this->assertEquals( false, $this->cache2->get( $key ), 'Not written to tier 2' );
60 
61  $dbw->commit();
62 
63  // Set in tier 2
64  $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
65 
66  $key = wfRandomString();
67 
68  $dbw->begin();
69  $this->cache->merge( $key, $func, 0, 1, BagOStuff::WRITE_SYNC );
70 
71  // Set in tier 1
72  $this->assertEquals( $value, $this->cache1->get( $key ), 'Written to tier 1' );
73  // Also set in tier 2
74  $this->assertEquals( $value, $this->cache2->get( $key ), 'Written to tier 2' );
75 
76  $dbw->commit();
77  }
78 
82  public function testSetDelayed() {
83  $key = wfRandomString();
84  $value = (object)[ 'v' => wfRandomString() ];
85  $expectValue = clone $value;
86 
87  // XXX: DeferredUpdates bound to transactions in CLI mode
88  $dbw = wfGetDB( DB_MASTER );
89  $dbw->begin();
90  $this->cache->set( $key, $value );
91 
92  // Test that later changes to $value don't affect the saved value (e.g. T168040)
93  $value->v = 'bogus';
94 
95  // Set in tier 1
96  $this->assertEquals( $expectValue, $this->cache1->get( $key ), 'Written to tier 1' );
97  // Not yet set in tier 2
98  $this->assertEquals( false, $this->cache2->get( $key ), 'Not written to tier 2' );
99 
100  $dbw->commit();
101 
102  // Set in tier 2
103  $this->assertEquals( $expectValue, $this->cache2->get( $key ), 'Written to tier 2' );
104  }
105 
109  public function testMakeKey() {
110  $cache1 = $this->getMockBuilder( HashBagOStuff::class )
111  ->setMethods( [ 'makeKey' ] )->getMock();
112  $cache1->expects( $this->once() )->method( 'makeKey' )
113  ->willReturn( 'special' );
114 
115  $cache2 = $this->getMockBuilder( HashBagOStuff::class )
116  ->setMethods( [ 'makeKey' ] )->getMock();
117  $cache2->expects( $this->never() )->method( 'makeKey' );
118 
119  $cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
120  $this->assertSame( 'special', $cache->makeKey( 'a', 'b' ) );
121  }
122 
126  public function testMakeGlobalKey() {
127  $cache1 = $this->getMockBuilder( HashBagOStuff::class )
128  ->setMethods( [ 'makeGlobalKey' ] )->getMock();
129  $cache1->expects( $this->once() )->method( 'makeGlobalKey' )
130  ->willReturn( 'special' );
131 
132  $cache2 = $this->getMockBuilder( HashBagOStuff::class )
133  ->setMethods( [ 'makeGlobalKey' ] )->getMock();
134  $cache2->expects( $this->never() )->method( 'makeGlobalKey' );
135 
136  $cache = new MultiWriteBagOStuff( [ 'caches' => [ $cache1, $cache2 ] ] );
137 
138  $this->assertSame( 'special', $cache->makeGlobalKey( 'a', 'b' ) );
139  }
140 }
object
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:64
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
MultiWriteBagOStuffTest\testSetDelayed
testSetDelayed()
MultiWriteBagOStuff::set.
Definition: MultiWriteBagOStuffTest.php:82
MultiWriteBagOStuffTest\testMakeKey
testMakeKey()
MultiWriteBagOStuff::makeKey.
Definition: MultiWriteBagOStuffTest.php:109
BagOStuff\WRITE_SYNC
const WRITE_SYNC
Bitfield constants for set()/merge()
Definition: BagOStuff.php:89
MultiWriteBagOStuffTest\$cache1
HashBagOStuff $cache1
Definition: MultiWriteBagOStuffTest.php:8
MultiWriteBagOStuff\makeGlobalKey
makeGlobalKey( $class, $component=null)
Make a global cache key.
Definition: MultiWriteBagOStuff.php:241
MultiWriteBagOStuffTest\$cache2
HashBagOStuff $cache2
Definition: MultiWriteBagOStuffTest.php:10
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:55
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MultiWriteBagOStuffTest
Database.
Definition: MultiWriteBagOStuffTest.php:6
DB_MASTER
const DB_MASTER
Definition: defines.php:26
MultiWriteBagOStuffTest\testSetImmediate
testSetImmediate()
MultiWriteBagOStuff::set MultiWriteBagOStuff::doWrite.
Definition: MultiWriteBagOStuffTest.php:30
$value
$value
Definition: styleTest.css.php:45
MultiWriteBagOStuff\makeKey
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
Definition: MultiWriteBagOStuff.php:237
MultiWriteBagOStuff
A cache class that replicates all writes to multiple child caches.
Definition: MultiWriteBagOStuff.php:32
MultiWriteBagOStuffTest\$cache
MultiWriteBagOStuff $cache
Definition: MultiWriteBagOStuffTest.php:12
MultiWriteBagOStuffTest\testMakeGlobalKey
testMakeGlobalKey()
MultiWriteBagOStuff::makeGlobalKey.
Definition: MultiWriteBagOStuffTest.php:126
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
MultiWriteBagOStuffTest\setUp
setUp()
Definition: MultiWriteBagOStuffTest.php:14
MultiWriteBagOStuffTest\testSyncMerge
testSyncMerge()
MultiWriteBagOStuff.
Definition: MultiWriteBagOStuffTest.php:44
wfRandomString
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Definition: GlobalFunctions.php:305