Debian Codegraph @

tests/phpunit/unit/includes/poolcounter/PoolCounterTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\PoolCounter\PoolCounter;
4
5 /**
6 * @covers \MediaWiki\PoolCounter\PoolCounter
7 */
8 class PoolCounterTest extends MediaWikiUnitTestCase {
9 public function testConstruct() {
10 $poolCounterConfig = [
11 'class' => 'PoolCounterMock',
12 'timeout' => 10,
13 'workers' => 10,
14 'maxqueue' => 100,
15 ];
16
17 $poolCounter = $this->getMockBuilder( PoolCounterAbstractMock::class )
18 ->setConstructorArgs( [ $poolCounterConfig, 'testCounter', 'someKey' ] )
19 ->onlyMethods( [] )
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 ->onlyMethods( [] ) // don't mock anything
36 ->getMockForAbstractClass();
37 $this->assertInstanceOf( PoolCounter::class, $poolCounter );
38 }
39
40 public function testHashKeyIntoSlots() {
41 $poolCounter = $this->createMock( PoolCounterAbstractMock::class );
42
43 $hashKeyIntoSlots = new ReflectionMethod( $poolCounter, 'hashKeyIntoSlots' );
44
45 $keysWithTwoSlots = $keysWithFiveSlots = [];
46 foreach ( range( 1, 100 ) as $i ) {
47 $keysWithTwoSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 2 );
48 $keysWithFiveSlots[] = $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'key ' . $i, 5 );
49 }
50
51 $twoSlotKeys = [];
52 for ( $i = 0; $i <= 1; $i++ ) {
53 $twoSlotKeys[] = "test:$i";
54 }
55 $fiveSlotKeys = [];
56 for ( $i = 0; $i <= 4; $i++ ) {
57 $fiveSlotKeys[] = "test:$i";
58 }
59
60 $this->assertArrayEquals( $twoSlotKeys, array_unique( $keysWithTwoSlots ) );
61 $this->assertArrayEquals( $fiveSlotKeys, array_unique( $keysWithFiveSlots ) );
62
63 // make sure it is deterministic
64 $this->assertEquals(
65 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 ),
66 $hashKeyIntoSlots->invoke( $poolCounter, 'test', 'asdfgh', 1000 )
67 );
68 }
69 }
70
71 // We will use this class with getMockForAbstractClass to create a concrete mock class.
72 // That call will die if the contructor is not public, unless we use disableOriginalConstructor(),
73 // in which case we could not test the constructor.
74 abstract class PoolCounterAbstractMock extends PoolCounter {
75 public function __construct( ...$args ) {
76 parent::__construct( ...$args );
77 }
78 }