Debian Codegraph @

tests/phpunit/unit/includes/libs/WRStats/WRStatsRateLimiterTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\WRStats;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\WRStats\ArrayStatsStore;
7 use Wikimedia\WRStats\LimitCondition;
8 use Wikimedia\WRStats\LimitOperation;
9 use Wikimedia\WRStats\LocalEntityKey;
10 use Wikimedia\WRStats\WRStatsRateLimiter;
11
12 /**
13 * @covers \Wikimedia\WRStats\WRStatsRateLimiter
14 * @covers \Wikimedia\WRStats\LimitOperationResult
15 * @covers \Wikimedia\WRStats\LimitBatch
16 * @covers \Wikimedia\WRStats\LimitBatchResult
17 * @covers \Wikimedia\WRStats\LimitCondition
18 * @covers \Wikimedia\WRStats\LimitOperation
19 */
20 class WRStatsRateLimiterTest extends TestCase {
21 public function testTryIncrBatch() {
22 $store = new ArrayStatsStore;
23 $conds = [
24 'cond1' => new LimitCondition( 3, 60 ),
25 ];
26 $rateLimiter = new WRStatsRateLimiter( $store, $conds );
27 $rateLimiter->setCurrentTime( 1000 );
28 $actions = [ new LimitOperation( 'cond1' ) ];
29
30 $batchResult = $rateLimiter->tryIncrBatch( $actions );
31 $this->assertTrue( $batchResult->isAllowed() );
32 $this->assertSame( "LimitActionResult{1/3}",
33 $batchResult->getAllResults()[0]->dump() );
34
35 $this->assertTrue( $rateLimiter->tryIncrBatch( $actions )->isAllowed() );
36 $this->assertTrue( $rateLimiter->tryIncrBatch( $actions )->isAllowed() );
37
38 $batchResult = $rateLimiter->tryIncrBatch( $actions );
39 $this->assertFalse( $batchResult->isAllowed() );
40 $this->assertCount( 0, $batchResult->getPassedResults() );
41 $failed = $batchResult->getFailedResults();
42 $this->assertCount( 1, $failed );
43 $this->assertSame( "LimitActionResult{4/3}", $failed[0]->dump() );
44 $this->assertSame( 3, $failed[0]->prevTotal );
45 $this->assertSame( 4, $failed[0]->newTotal );
46 }
47
48 public function testMultiEntity() {
49 $store = new ArrayStatsStore;
50 $conds = [
51 'cond1' => new LimitCondition( 1, 60 ),
52 ];
53 $entity1 = new LocalEntityKey( [ 'user_id', 1 ] );
54 $entity2 = new LocalEntityKey( [ 'user_id', 2 ] );
55 $rateLimiter = new WRStatsRateLimiter( $store, $conds );
56 $rateLimiter->setCurrentTime( 1000 );
57 $this->assertTrue(
58 $rateLimiter->tryIncrBatch( [
59 new LimitOperation( 'cond1', $entity1 )
60 ] )->isAllowed()
61 );
62 $this->assertTrue(
63 $rateLimiter->tryIncrBatch( [
64 new LimitOperation( 'cond1', $entity2 )
65 ] )->isAllowed()
66 );
67 $rateLimiter->incr( 'cond1', $entity1, 10 );
68 $rateLimiter->incr( 'cond1', $entity2, 20 );
69 $res = $rateLimiter->tryIncr( 'cond1', $entity1 );
70 $this->assertSame( 'LimitActionResult{12/1}', $res->dump() );
71 $res = $rateLimiter->tryIncr( 'cond1', $entity2 );
72 $this->assertSame( 'LimitActionResult{22/1}', $res->dump() );
73 }
74
75 public function testMultiEntityBatch() {
76 $store = new ArrayStatsStore;
77 $conds = [
78 'id' => new LimitCondition( 2, 60 ),
79 'ip' => new LimitCondition( 3, 60 )
80 ];
81 $rateLimiter = new WRStatsRateLimiter( $store, $conds );
82 $rateLimiter->setCurrentTime( 1000 );
83
84 // Increment both metrics to 1
85 $res = $rateLimiter->createBatch()
86 ->localOp( 'id', 1 )
87 ->globalOp( 'ip', '127.0.0.1' )
88 ->tryIncr();
89 $this->assertTrue( $res->isAllowed() );
90 $this->assertSame( 'LimitActionResult{1/2}',
91 $res->getAllResults()['id']->dump() );
92 $this->assertSame( 'LimitActionResult{1/3}',
93 $res->getAllResults()['ip']->dump() );
94
95 // Increment to 2, testing peek/incr
96 $batch = $rateLimiter->createBatch()
97 ->localOp( 'id', 1 )
98 ->globalOp( 'ip', '127.0.0.1' );
99 $this->assertTrue( $batch->peek()->isAllowed() );
100 $batch->incr();
101
102 // Increment to 3, in which the id metric is expected to fail
103 $res = $rateLimiter->createBatch()
104 ->localOp( 'id', 1 )
105 ->globalOp( 'ip', '127.0.0.1' )
106 ->tryIncr();
107 $this->assertFalse( $res->isAllowed() );
108 $this->assertCount( 1, $res->getPassedResults() );
109 $failed = $res->getFailedResults();
110 $this->assertCount( 1, $failed );
111 $all = $res->getAllResults();
112 $this->assertSame( 'LimitActionResult{3/2}', $all['id']->dump() );
113 $this->assertSame( 'LimitActionResult{3/3}', $all['ip']->dump() );
114 }
115
116 public function testTimeDependent() {
117 $store = new ArrayStatsStore;
118 $conds = [
119 'cond1' => new LimitCondition( 3, 100 ),
120 ];
121 $rateLimiter = new WRStatsRateLimiter( $store, $conds, 'WRLimit',
122 [ 'bucketCount' => 10 ] );
123
124 $rateLimiter->setCurrentTime( 1000 );
125 $this->assertTrue( $rateLimiter->tryIncr( 'cond1' )->isAllowed() );
126 $rateLimiter->setCurrentTime( 1010 );
127 $this->assertTrue( $rateLimiter->tryIncr( 'cond1' )->isAllowed() );
128 $rateLimiter->setCurrentTime( 1020 );
129 $this->assertTrue( $rateLimiter->tryIncr( 'cond1' )->isAllowed() );
130 $rateLimiter->setCurrentTime( 1090 );
131 $this->assertSame( 'LimitActionResult{4/3}',
132 $rateLimiter->peek( 'cond1' )->dump() );
133
134 $rateLimiter->setCurrentTime( 1100 );
135 // The range goes back to 1000 so the count is still 3
136 $this->assertSame( 3,
137 $rateLimiter->peek( 'cond1' )->prevTotal );
138
139 $rateLimiter->setCurrentTime( 1103 );
140 // The range goes back to 1000 so the count should be 2.7, rounded up to 3
141 $this->assertSame( 3,
142 $rateLimiter->peek( 'cond1' )->prevTotal );
143
144 $rateLimiter->setCurrentTime( 1109 );
145 // The range goes back to 1009 so the first bucket is interpolated and
146 // rounded down to zero, so the count is 2 and there is room for
147 // another action
148 $this->assertTrue( $rateLimiter->peek( 'cond1' )->isAllowed() );
149 // But not room for 2 actions
150 $this->assertSame( 'LimitActionResult{4/3}',
151 $rateLimiter->tryIncr( 'cond1', null, 2 )->dump() );
152 }
153 }