-
-
Notifications
You must be signed in to change notification settings - Fork 27
Tests #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
viktorprogger
wants to merge
16
commits into
master
Choose a base branch
from
tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Tests #248
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
43aada9
Add Signal Loop test
viktorprogger 539fa00
Improve WorkerTest
viktorprogger 56e8450
Apply fixes from StyleCI
StyleCIBot 9540389
Improve QueueProviderInterfaceProxyTest
viktorprogger f5c5d19
Improve SignalLoopTest
viktorprogger acfbff3
Add QueueWorkerInterfaceProxyTest
viktorprogger 07dd1b3
Improve envelope tests
viktorprogger 020e256
Apply fixes from StyleCI
StyleCIBot 8cbf0a1
Apply Rector changes (CI)
viktorprogger 9eb07c8
Improve FailureHandlingRequestTest
viktorprogger bca11d3
Add CallableFactoryTest
viktorprogger f97ccd7
Apply fixes from StyleCI
StyleCIBot 15fd74c
Add AdapterPushHandlerTest
viktorprogger 6ba6a5c
Fix Queue::withAdapter() return type
viktorprogger 30ea145
Update phpunit to version 12
viktorprogger e1e3bd6
Fix phpunit version
viktorprogger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Queue\Tests\Unit\Cli; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
| use Yiisoft\Queue\Cli\SignalLoop; | ||
|
|
||
| #[RequiresPhpExtension('pcntl')] | ||
| final class SignalLoopTest extends TestCase | ||
| { | ||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| pcntl_async_signals(true); | ||
| } | ||
|
|
||
| public function testMemoryLimitReached(): void | ||
| { | ||
| $loop = new SignalLoop(1); | ||
| self::assertFalse($loop->canContinue()); | ||
| } | ||
|
|
||
| public function testSuspendAndResume(): void | ||
| { | ||
| $loop = new SignalLoop(0); | ||
| pcntl_signal(\SIGALRM, static function (): void { | ||
| posix_kill(getmypid(), \SIGCONT); | ||
| }); | ||
|
|
||
| posix_kill(getmypid(), \SIGTSTP); | ||
| pcntl_alarm(1); | ||
|
|
||
| $start = microtime(true); | ||
| $result = $loop->canContinue(); | ||
| $elapsed = microtime(true) - $start; | ||
|
|
||
| self::assertTrue($result); | ||
| self::assertGreaterThan(0.5, $elapsed); | ||
| } | ||
|
|
||
| #[DataProvider('exitSignalProvider')] | ||
| public function testExitSignals(int $signal): void | ||
| { | ||
| $loop = new SignalLoop(0); | ||
|
|
||
| self::assertTrue($loop->canContinue(), 'Loop should continue'); | ||
| posix_kill(getmypid(), $signal); | ||
|
|
||
| self::assertFalse($loop->canContinue(), "Loop should not continue after receiving signal {$signal}"); | ||
| } | ||
|
|
||
| public static function exitSignalProvider(): iterable | ||
| { | ||
| yield 'SIGHUP' => [\SIGHUP]; | ||
| yield 'SIGINT' => [\SIGINT]; | ||
| yield 'SIGTERM' => [\SIGTERM]; | ||
| } | ||
|
|
||
| public function testResumeSignal(): void | ||
| { | ||
| $loop = new SignalLoop(0); | ||
|
|
||
| // First suspend the loop | ||
| posix_kill(getmypid(), \SIGTSTP); | ||
|
|
||
| // Then immediately resume | ||
| posix_kill(getmypid(), \SIGCONT); | ||
|
|
||
| $start = microtime(true); | ||
| $result = $loop->canContinue(); | ||
| $elapsed = microtime(true) - $start; | ||
|
|
||
| self::assertTrue($result); | ||
| self::assertLessThan(0.1, $elapsed, 'Loop should resume quickly without waiting'); | ||
| } | ||
|
|
||
| public function testMultipleExitSignals(): void | ||
| { | ||
| $loop = new SignalLoop(0); | ||
|
|
||
| // Send multiple exit signals | ||
| posix_kill(getmypid(), \SIGINT); | ||
| posix_kill(getmypid(), \SIGTERM); | ||
|
|
||
| $result = $loop->canContinue(); | ||
|
|
||
| self::assertFalse($result, 'Loop should not continue after receiving any exit signal'); | ||
| } | ||
|
|
||
| public function testSuspendOverridesResume(): void | ||
| { | ||
| $loop = new SignalLoop(0); | ||
|
|
||
| // Resume first | ||
| posix_kill(getmypid(), \SIGCONT); | ||
| // Then suspend | ||
| posix_kill(getmypid(), \SIGTSTP); | ||
|
|
||
| // Set up alarm to resume after 1 second | ||
| pcntl_signal(\SIGALRM, static function (): void { | ||
| posix_kill(getmypid(), \SIGCONT); | ||
| }); | ||
| pcntl_alarm(1); | ||
|
|
||
| $start = microtime(true); | ||
| $result = $loop->canContinue(); | ||
| $elapsed = microtime(true) - $start; | ||
|
|
||
| self::assertTrue($result); | ||
| self::assertGreaterThan(0.5, $elapsed, 'Loop should wait for resume after suspend'); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Queue\Tests\Unit\Debug; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Yiisoft\Queue\Debug\QueueCollector; | ||
| use Yiisoft\Queue\Debug\QueueWorkerInterfaceProxy; | ||
| use Yiisoft\Queue\Message\Message; | ||
| use Yiisoft\Queue\Tests\App\DummyQueue; | ||
| use Yiisoft\Queue\Stubs\StubWorker; | ||
|
|
||
| final class QueueWorkerInterfaceProxyTest extends TestCase | ||
| { | ||
| public function testProcessDelegatesToWorker(): void | ||
| { | ||
| $message = new Message('handler', 'data'); | ||
| $collector = new QueueCollector(); | ||
| $collector->startup(); | ||
| $proxy = new QueueWorkerInterfaceProxy(new StubWorker(), $collector); | ||
|
|
||
| $result = $proxy->process($message, new DummyQueue('chan')); | ||
|
|
||
| self::assertSame($message, $result); | ||
|
|
||
| $collected = $collector->getCollected(); | ||
| self::assertArrayHasKey('processingMessages', $collected); | ||
| self::assertArrayHasKey('chan', $collected['processingMessages']); | ||
| self::assertCount(1, $collected['processingMessages']['chan']); | ||
| self::assertSame($message, $collected['processingMessages']['chan'][0]); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Queue\Tests\Unit\Middleware; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Yiisoft\Queue\Middleware\CallableFactory; | ||
| use Yiisoft\Queue\Middleware\InvalidCallableConfigurationException; | ||
| use Yiisoft\Test\Support\Container\SimpleContainer; | ||
|
|
||
| final class CallableFactoryTest extends TestCase | ||
| { | ||
| public function testCreateFromContainerStringInvokable(): void | ||
| { | ||
| $invokable = new class () { | ||
| public function __invoke(): string | ||
| { | ||
| return 'ok'; | ||
| } | ||
| }; | ||
| $container = new SimpleContainer([ | ||
| 'invokable' => $invokable, | ||
| ]); | ||
|
|
||
| $factory = new CallableFactory($container); | ||
| $callable = $factory->create('invokable'); | ||
|
|
||
| self::assertIsCallable($callable); | ||
| self::assertSame('ok', $callable()); | ||
| } | ||
|
|
||
| public function testCreateFromStaticMethodArray(): void | ||
| { | ||
| $class = new class () { | ||
| public static function ping(): string | ||
| { | ||
| return 'pong'; | ||
| } | ||
| }; | ||
| $className = $class::class; | ||
| $container = new SimpleContainer(); | ||
|
|
||
| $factory = new CallableFactory($container); | ||
| $callable = $factory->create([$className, 'ping']); | ||
|
|
||
| self::assertIsCallable($callable); | ||
| self::assertSame('pong', $callable()); | ||
| } | ||
|
|
||
| public function testCreateFromContainerObjectMethod(): void | ||
| { | ||
| $service = new class () { | ||
| public function go(): string | ||
| { | ||
| return 'ok'; | ||
| } | ||
| }; | ||
| $className = $service::class; | ||
| $container = new SimpleContainer([ | ||
| $className => $service, | ||
| ]); | ||
|
|
||
| $factory = new CallableFactory($container); | ||
| $callable = $factory->create([$className, 'go']); | ||
|
|
||
| self::assertIsCallable($callable); | ||
| self::assertSame('ok', $callable()); | ||
| } | ||
|
|
||
| public function testFriendlyException(): void | ||
| { | ||
| $e = new InvalidCallableConfigurationException(); | ||
| self::assertSame('Invalid callable configuration.', $e->getName()); | ||
| self::assertNotNull($e->getSolution()); | ||
| self::assertStringContainsString('callable', (string)$e->getSolution()); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is PHPUnit 10 not enough?