Merge "Avoid master queries in Title::getLinksFrom()"
[lhc/web/wiklou.git] / tests / phpunit / includes / poolcounter / PoolCounterTest.php
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
12 class PoolCounterTest extends MediaWikiTestCase {
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, 'key ' . $i, 2 );
60 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'key ' . $i, 5 );
61 }
62
63 $this->assertArrayEquals( range( 0, 1 ), array_unique( $keysWithTwoSlots ) );
64 $this->assertArrayEquals( range( 0, 4 ), array_unique( $keysWithFiveSlots ) );
65
66 // make sure it is deterministic
67 $this->assertEquals(
68 $hashKeyIntoSlots->invoke( $poolCounter, 'asdfgh', 1000 ),
69 $hashKeyIntoSlots->invoke( $poolCounter, 'asdfgh', 1000 )
70 );
71 }
72 }