-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from ostark/test-rate-limiter
Fix & improve RateLimiter
- Loading branch information
Showing
15 changed files
with
204 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ class TestJob extends BaseJob | |
*/ | ||
public function execute($queue): void | ||
{ | ||
sleep(10); | ||
sleep(3); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 $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()); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
Oops, something went wrong.