|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Governator\Tests\Integration; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Zenstruck\Governator\Store\UnlimitedStore; |
|
7
|
|
|
use Zenstruck\Governator\ThrottleFactory; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @group time-sensitive |
|
11
|
|
|
* |
|
12
|
|
|
* @author Kevin Bond <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class UnlimitedThrottleTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @test |
|
18
|
|
|
*/ |
|
19
|
|
|
public function always_allows_hit(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$throttle = (new ThrottleFactory(new UnlimitedStore()))->create('foo', 5, 60); |
|
22
|
|
|
|
|
23
|
|
|
$quota = $throttle->acquire(); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertSame(1, $quota->hits()); |
|
26
|
|
|
$this->assertSame(4, $quota->remaining()); |
|
27
|
|
|
$this->assertSame(60, $quota->resetsIn()); |
|
28
|
|
|
|
|
29
|
|
|
$throttle->reset(); |
|
30
|
|
|
$throttle->acquire(); |
|
31
|
|
|
$throttle->acquire(); |
|
32
|
|
|
$throttle->acquire(); |
|
33
|
|
|
$quota = $throttle->acquire(); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertSame(1, $quota->hits()); |
|
36
|
|
|
$this->assertSame(4, $quota->remaining()); |
|
37
|
|
|
$this->assertSame(60, $quota->resetsIn()); |
|
38
|
|
|
|
|
39
|
|
|
sleep(4); |
|
40
|
|
|
|
|
41
|
|
|
$quota = $throttle->acquire(); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertSame(1, $quota->hits()); |
|
44
|
|
|
$this->assertSame(4, $quota->remaining()); |
|
45
|
|
|
$this->assertSame(60, $quota->resetsIn()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @test |
|
50
|
|
|
*/ |
|
51
|
|
|
public function never_blocks(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$throttle = (new ThrottleFactory(new UnlimitedStore()))->create('foo', 5, 60); |
|
54
|
|
|
$start = time(); |
|
55
|
|
|
|
|
56
|
|
|
$quota = $throttle->acquire(5); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertSame(time(), $start); |
|
59
|
|
|
$this->assertSame(1, $quota->hits()); |
|
60
|
|
|
$this->assertSame(4, $quota->remaining()); |
|
61
|
|
|
$this->assertSame(60, $quota->resetsIn()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @test |
|
66
|
|
|
*/ |
|
67
|
|
|
public function status_always_returns_empty_quota(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$throttle = (new ThrottleFactory(new UnlimitedStore()))->create('foo', 5, 60); |
|
70
|
|
|
|
|
71
|
|
|
$throttle->acquire(); |
|
72
|
|
|
$throttle->acquire(); |
|
73
|
|
|
$throttle->acquire(); |
|
74
|
|
|
$throttle->acquire(); |
|
75
|
|
|
|
|
76
|
|
|
$quota = $throttle->status(); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertSame(0, $quota->hits()); |
|
79
|
|
|
$this->assertSame(5, $quota->remaining()); |
|
80
|
|
|
$this->assertSame(60, $quota->resetsIn()); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|