MediaWiki REL1_32
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.
6abstract class PoolCounterAbstractMock extends PoolCounter {
7 public function __construct() {
8 call_user_func_array( 'parent::__construct', func_get_args() );
9 }
10}
11
16 public function testConstruct() {
17 $poolCounterConfig = [
18 'class' => 'PoolCounterMock',
19 'timeout' => 10,
20 'workers' => 10,
21 'maxqueue' => 100,
22 ];
23
24 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
25 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
26 // don't mock anything - the proper syntax would be setMethods(null), but due
27 // to a PHPUnit bug that does not work with getMockForAbstractClass()
28 ->setMethods( [ 'idontexist' ] )
29 ->getMockForAbstractClass();
30 $this->assertInstanceOf( PoolCounter::class, $poolCounter );
31 }
32
33 public function testConstructWithSlots() {
34 $poolCounterConfig = [
35 'class' => 'PoolCounterMock',
36 'timeout' => 10,
37 'workers' => 10,
38 'slots' => 2,
39 'maxqueue' => 100,
40 ];
41
42 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
43 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'key' ] )
44 ->setMethods( [ 'idontexist' ] ) // don't mock anything
45 ->getMockForAbstractClass();
46 $this->assertInstanceOf( PoolCounter::class, $poolCounter );
47 }
48
49 public function testHashKeyIntoSlots() {
50 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
51 // don't mock anything - the proper syntax would be setMethods(null), but due
52 // to a PHPUnit bug that does not work with getMockForAbstractClass()
53 ->setMethods( [ 'idontexist' ] )
54 ->disableOriginalConstructor()
55 ->getMockForAbstractClass();
56
57 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
58 $hashKeyIntoSlots->setAccessible( true );
59
60 $keysWithTwoSlots = $keysWithFiveSlots = [];
61 foreach ( range( 1, 100 ) as $i ) {
62 $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
63 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
64 }
65
66 $twoSlotKeys = [];
67 for ( $i = 0; $i <= 1; $i++ ) {
68 $twoSlotKeys[] = "test:$i";
69 }
70 $fiveSlotKeys = [];
71 for ( $i = 0; $i <= 4; $i++ ) {
72 $fiveSlotKeys[] = "test:$i";
73 }
74
75 $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
76 $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
77
78 // make sure it is deterministic
79 $this->assertEquals(
80 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
81 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
82 );
83 }
84}
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
When you have many workers (threads/servers) giving service, and a cached item expensive to produce e...