Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
fix(tests): refactor feature tests with server start command
Browse files Browse the repository at this point in the history
  • Loading branch information
k911 committed Apr 17, 2021
1 parent e738805 commit b3d4eef
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 202 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"phpunit tests/Unit --coverage-php cov/unit-tests.cov --colors=always"
],
"feature-code-coverage": [
"COVERAGE=1 phpunit tests/Feature --coverage-php cov/feature-tests.cov --colors=always --process-isolation"
"COVERAGE=1 phpunit tests/Feature --coverage-php cov/feature-tests.cov --colors=always --process-isolation -d memory_limit=512M"
],
"merge-code-coverage": [
"phpcov merge cov --clover=cov/clover.xml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ final protected function execute(InputInterface $input, OutputInterface $output)

if ($this->server->isRunning()) {
$io->error('Swoole HTTP Server is already running');
exit(1);

return 1;
}

$swooleServer = $this->makeSwooleHttpServer();
Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/Symfony/Bundle/Command/ServerReloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->server->reload();
} catch (Throwable $ex) {
$io->error($ex->getMessage());
exit(1);

return 1;
}

$io->success('Swoole HTTP Server\'s workers reloaded successfully');
Expand Down
35 changes: 0 additions & 35 deletions src/Bridge/Symfony/Bundle/Command/ServerStartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

use K911\Swoole\Bridge\Symfony\Bundle\Exception\CouldNotCreatePidFileException;
use K911\Swoole\Bridge\Symfony\Bundle\Exception\PidFileNotAccessibleException;
use function K911\Swoole\get_object_property;
use K911\Swoole\Server\HttpServer;
use K911\Swoole\Server\HttpServerConfiguration;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Style\OutputStyle;
use Symfony\Component\Console\Style\SymfonyStyle;

final class ServerStartCommand extends AbstractServerStartCommand
Expand Down Expand Up @@ -57,37 +53,6 @@ protected function startServer(HttpServerConfiguration $serverConfiguration, Htt
throw CouldNotCreatePidFileException::forPath($pidFile);
}

$this->closeSymfonyStyle($io);

$server->start();
}

private function closeSymfonyStyle(SymfonyStyle $io): void
{
$output = get_object_property($io, 'output', OutputStyle::class);
if ($output instanceof ConsoleOutput) {
$this->closeConsoleOutput($output);
} elseif ($output instanceof StreamOutput) {
$this->closeStreamOutput($output);
}
}

/**
* Prevents usage of php://stdout or php://stderr while running in background.
*/
private function closeConsoleOutput(ConsoleOutput $output): void
{
\fclose($output->getStream());

/** @var StreamOutput $streamOutput */
$streamOutput = $output->getErrorOutput();

$this->closeStreamOutput($streamOutput);
}

private function closeStreamOutput(StreamOutput $output): void
{
$output->setVerbosity(\PHP_INT_MIN);
\fclose($output->getStream());
}
}
3 changes: 2 additions & 1 deletion src/Bridge/Symfony/Bundle/Command/ServerStopCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->server->shutdown();
} catch (Throwable $ex) {
$io->error($ex->getMessage());
exit(1);

return 1;
}

$io->success('Swoole server shutdown successfully');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Bridge\Symfony\Bundle\EventListener;

use Symfony\Component\Console\Event\ConsoleTerminateEvent;

final class SwooleErrorOnConsoleTerminateEvent
{
public function onConsoleTerminate(ConsoleTerminateEvent $event): void
{
$swooleLastErrorNo = \swoole_last_error();
if (0 !== $swooleLastErrorNo) {
echo 'Swoole Error: '.\swoole_strerror($swooleLastErrorNo, 9).\PHP_EOL;
}
}
}
5 changes: 5 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,8 @@ services:
autoconfigure: false
arguments:
$handler: '@K911\Swoole\Server\Runtime\ServerShutdown\SignalServerShutdownHandlerOnServerStart'

K911\Swoole\Bridge\Symfony\Bundle\EventListener\SwooleErrorOnConsoleTerminateEvent:
tags:
- name: kernel.event_listener
event: console.terminate
6 changes: 1 addition & 5 deletions tests/Feature/SwooleServerCustomPidFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ public function testStartServerOnCustomPidFileLocation(): void

self::assertFileDoesNotExist($pidFile);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function () use ($pidFile): void {
$this->deferServerStop(\sprintf('--pid-file=%s', $pidFile));
Expand Down
48 changes: 8 additions & 40 deletions tests/Feature/SwooleServerExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ public function testCatchException(): void
'--port=9999',
]);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -62,11 +58,7 @@ public function testCatchExceptionOnReactorRunningMode(): void
'--port=9999',
], ['APP_ENV' => 'reactor']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -102,11 +94,7 @@ public function testCatchExceptionViaProductionExceptionHandler(): void
'--port=9999',
], ['APP_ENV' => 'prod', 'APP_DEBUG' => '0']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -135,11 +123,7 @@ public function testCatchExceptionWithNoTrace(): void
'--port=9999',
], ['APP_DEBUG' => '0']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -168,11 +152,7 @@ public function testExceptionHandlerJsonDefaultVerbosity(): void
'--port=9999',
], ['APP_ENV' => 'exception_handler_json']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -204,11 +184,7 @@ public function testCatchExceptionViaSymfonyExceptionHandler(): void
'--port=9999',
], ['APP_ENV' => 'exception_handler_symfony', 'APP_DEBUG' => '0']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -237,11 +213,7 @@ public function testCatchExceptionViaSymfonyExceptionHandlerWithDebug(): void
'--port=9999',
], ['APP_ENV' => 'exception_handler_symfony', 'APP_DEBUG' => '1']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -270,11 +242,7 @@ public function testCustomExceptionHandler(): void
'--port=9999',
], ['APP_ENV' => 'exception_handler_custom']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down
12 changes: 2 additions & 10 deletions tests/Feature/SwooleServerHMRTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ public function testStartCallHMRCallStopWithAutoRegistration(): void
'--port=9999',
], ['APP_ENV' => 'auto']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -70,11 +66,7 @@ public function testHMRDisabledByDefaultOnProduction(): void
'--port=9999',
], ['APP_ENV' => 'prod']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down
6 changes: 1 addition & 5 deletions tests/Feature/SwooleServerReloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public function testStartCallReloadCallStop(): void
'--port=9999',
]);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down
6 changes: 1 addition & 5 deletions tests/Feature/SwooleServerReloadViaHttpApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public function testStartRequestApiToReloadCallStop(): void
'--api-port=9998',
]);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down
12 changes: 2 additions & 10 deletions tests/Feature/SwooleServerStartStopCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ public function testStartCallStop(): void
'--port=9999',
]);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand All @@ -45,11 +41,7 @@ public function testStartCallStopOnReactorRunningMode(): void
'--port=9999',
], ['APP_ENV' => 'reactor']);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down
12 changes: 2 additions & 10 deletions tests/Feature/SwooleServerStatusCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ public function testCheckServerStatusViaProcess(): void
'--api-port=9998',
]);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$this->runAsCoroutineAndWait(function (): void {
$this->deferServerStop();
Expand Down Expand Up @@ -68,11 +64,7 @@ public function testCheckServerStatusViaCommandTester(): void
'--api-port=9998',
]);

$serverStart->setTimeout(3);
$serverStart->disableOutput();
$serverStart->run();

$this->assertProcessSucceeded($serverStart);
$this->assertStartServerSucceeded($serverStart);

$kernel = static::createKernel();
$application = new Application($kernel);
Expand Down
Loading

0 comments on commit b3d4eef

Please sign in to comment.