MediaWiki REL1_33
PoolCounterTest.php
Go to the documentation of this file.
1<?php
2
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.
80abstract class PoolCounterAbstractMock extends PoolCounter {
81 public function __construct() {
82 call_user_func_array( 'parent::__construct', func_get_args() );
83 }
84}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
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...