Require ClassMatchesFilename sniff to pass for most of tests/
[lhc/web/wiklou.git] / tests / phpunit / includes / poolcounter / PoolCounterTest.php
1 <?php
2
3 /**
4 * @covers PoolCounter
5 */
6 class PoolCounterTest extends MediaWikiTestCase {
7 public function testConstruct() {
8 $poolCounterConfig = [
9 'class' => 'PoolCounterMock',
10 'timeout' => 10,
11 'workers' => 10,
12 'maxqueue' => 100,
13 ];
14
15 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
16 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
17 // don't mock anything - the proper syntax would be setMethods(null), but due
18 // to a PHPUnit bug that does not work with getMockForAbstractClass()
19 ->setMethods( [ 'idontexist' ] )
20 ->getMockForAbstractClass();
21 $this->assertInstanceOf( PoolCounter::class, $poolCounter );
22 }
23
24 public function testConstructWithSlots() {
25 $poolCounterConfig = [
26 'class' => 'PoolCounterMock',
27 'timeout' => 10,
28 'workers' => 10,
29 'slots' => 2,
30 'maxqueue' => 100,
31 ];
32
33 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
34 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
35 ->setMethods( [ 'idontexist' ] ) // don't mock anything
36 ->getMockForAbstractClass();
37 $this->assertInstanceOf( PoolCounter::class, $poolCounter );
38 }
39
40 public function testHashKeyIntoSlots() {
41 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
42 // don't mock anything - the proper syntax would be setMethods(null), but due
43 // to a PHPUnit bug that does not work with getMockForAbstractClass()
44 ->setMethods( [ 'idontexist' ] )
45 ->disableOriginalConstructor()
46 ->getMockForAbstractClass();
47
48 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
49 $hashKeyIntoSlots->setAccessible( true );
50
51 $keysWithTwoSlots = $keysWithFiveSlots = [];
52 foreach ( range( 1, 100 ) as $i ) {
53 $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
54 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
55 }
56
57 $twoSlotKeys = [];
58 for ( $i = 0; $i <= 1; $i++ ) {
59 $twoSlotKeys[] = "test:$i";
60 }
61 $fiveSlotKeys = [];
62 for ( $i = 0; $i <= 4; $i++ ) {
63 $fiveSlotKeys[] = "test:$i";
64 }
65
66 $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
67 $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
68
69 // make sure it is deterministic
70 $this->assertEquals(
71 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
72 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
73 );
74 }
75 }
76
77 // We will use this class with getMockForAbstractClass to create a concrete mock class.
78 // That call will die if the contructor is not public, unless we use disableOriginalConstructor(),
79 // in which case we could not test the constructor.
80 abstract class PoolCounterAbstractMock extends PoolCounter {
81 public function __construct() {
82 call_user_func_array( 'parent::__construct', func_get_args() );
83 }
84 }