-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRateLimiterTest.php
More file actions
73 lines (57 loc) · 2.25 KB
/
RateLimiterTest.php
File metadata and controls
73 lines (57 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
use ostark\AsyncQueue\BackgroundProcess;
use PHPUnit\Framework\TestCase;
use yii\queue\PushEvent;
/**
* @covers \ostark\AsyncQueue\RateLimiter
* @covers \ostark\AsyncQueue\Handlers\BackgroundQueueHandler
*/
class RateLimiterTest extends TestCase
{
public ostark\AsyncQueue\Plugin $plugin;
public function setUp(): void
{
parent::setUp();
$dummyCommand = new \ostark\AsyncQueue\QueueCommand('craft.php', '--sleep');
$this->plugin = new \ostark\AsyncQueue\Plugin('async-queue', null, []);
$this->plugin->set('async_process', new BackgroundProcess($dummyCommand));
}
public function tearDown(): void
{
parent::tearDown();
@unlink(TEST_FILE);
}
public function test_can_setup_handler_and_invoke_event(): void
{
$this->plugin = new \ostark\AsyncQueue\Plugin('async-queue', null, []);
$handler = new \ostark\AsyncQueue\Handlers\BackgroundQueueHandler($this->plugin);
$handler->__invoke(new PushEvent());
$this->assertSame(1, $this->plugin->getRateLimiter()->getInternalCount());
}
public function test_respect_the_limit_when_adding_multiple_jobs_in_one_request(): void
{
$handler = new \ostark\AsyncQueue\Handlers\BackgroundQueueHandler($this->plugin);
$handler->__invoke(new PushEvent());
$handler->__invoke(new PushEvent());
$handler->__invoke(new PushEvent());
$handler->__invoke(new PushEvent());
$this->assertSame(
$this->plugin->getRateLimiter()->maxItems,
$this->plugin->getRateLimiter()->getInternalCount()
);
}
public function test_stop_spawning_processes_when_too_many_jobs_are_reserved(): void
{
// Fake the reserved count
$queue = new class extends \craft\queue\Queue {
public function getTotalReserved(): int
{
return 5;
}
};
$this->plugin->set('async_rate_limiter', new \ostark\AsyncQueue\RateLimiter($queue, $this->plugin->getSettings()));
$handler = new \ostark\AsyncQueue\Handlers\BackgroundQueueHandler($this->plugin);
$handler->__invoke(new PushEvent());
$this->assertSame(0, $this->plugin->getRateLimiter()->getInternalCount());
}
}