MediaWiki  1.30.0
PoolCounterTest.php
Go to the documentation of this file.
1 <?php
2 
3 // We will use this class with getMockForAbstractClass to create a concrete mock class.
4 // That call will die if the contructor is not public, unless we use disableOriginalConstructor(),
5 // in which case we could not test the constructor.
6 abstract class PoolCounterAbstractMock extends PoolCounter {
7  public function __construct() {
8  call_user_func_array( 'parent::__construct', func_get_args() );
9  }
10 }
11 
13  public function testConstruct() {
14  $poolCounterConfig = [
15  'class' => 'PoolCounterMock',
16  'timeout' => 10,
17  'workers' => 10,
18  'maxqueue' => 100,
19  ];
20 
21  $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
22  ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
23  // don't mock anything - the proper syntax would be setMethods(null), but due
24  // to a PHPUnit bug that does not work with getMockForAbstractClass()
25  ->setMethods( [ 'idontexist' ] )
26  ->getMockForAbstractClass();
27  $this->assertInstanceOf( 'PoolCounter', $poolCounter );
28  }
29 
30  public function testConstructWithSlots() {
31  $poolCounterConfig = [
32  'class' => 'PoolCounterMock',
33  'timeout' => 10,
34  'workers' => 10,
35  'slots' => 2,
36  'maxqueue' => 100,
37  ];
38 
39  $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
40  ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
41  ->setMethods( [ 'idontexist' ] ) // don't mock anything
42  ->getMockForAbstractClass();
43  $this->assertInstanceOf( 'PoolCounter', $poolCounter );
44  }
45 
46  public function testHashKeyIntoSlots() {
47  $poolCounter = $this->getMockBuilder( 'PoolCounterAbstractMock' )
48  // don't mock anything - the proper syntax would be setMethods(null), but due
49  // to a PHPUnit bug that does not work with getMockForAbstractClass()
50  ->setMethods( [ 'idontexist' ] )
51  ->disableOriginalConstructor()
52  ->getMockForAbstractClass();
53 
54  $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
55  $hashKeyIntoSlots->setAccessible( true );
56 
57  $keysWithTwoSlots = $keysWithFiveSlots = [];
58  foreach ( range( 1, 100 ) as $i ) {
59  $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
60  $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
61  }
62 
63  $twoSlotKeys = [];
64  for ( $i = 0; $i <= 1; $i++ ) {
65  $twoSlotKeys[] = "test:$i";
66  }
67  $fiveSlotKeys = [];
68  for ( $i = 0; $i <= 4; $i++ ) {
69  $fiveSlotKeys[] = "test:$i";
70  }
71 
72  $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
73  $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
74 
75  // make sure it is deterministic
76  $this->assertEquals(
77  $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
78  $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
79  );
80  }
81 }
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:1552
PoolCounterTest\testHashKeyIntoSlots
testHashKeyIntoSlots()
Definition: PoolCounterTest.php:46
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:35
PoolCounterTest
Definition: PoolCounterTest.php:12
PoolCounter
When you have many workers (threads/servers) giving service, and a cached item expensive to produce e...
Definition: PoolCounter.php:45
PoolCounterTest\testConstruct
testConstruct()
Definition: PoolCounterTest.php:13
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
PoolCounterTest\testConstructWithSlots
testConstructWithSlots()
Definition: PoolCounterTest.php:30
PoolCounterAbstractMock
Definition: PoolCounterTest.php:6
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
PoolCounterAbstractMock\__construct
__construct()
Definition: PoolCounterTest.php:7