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

Commit

Permalink
test(code-coverage): Gather code coverage in server processes
Browse files Browse the repository at this point in the history
  • Loading branch information
k911 committed Mar 19, 2019
1 parent ca86606 commit 0591ece
Show file tree
Hide file tree
Showing 22 changed files with 410 additions and 29 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ matrix:
after_success:
- composer merge-code-coverage
- ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT
- bash <(curl -s https://codecov.io/bash)
env:
- COVERAGE=1
- DEPLOY=1
Expand Down
45 changes: 35 additions & 10 deletions src/Bridge/Symfony/Bundle/DependencyInjection/SwooleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
use K911\Swoole\Server\Config\Socket;
use K911\Swoole\Server\Config\Sockets;
use K911\Swoole\Server\Configurator\ConfiguratorInterface;
use K911\Swoole\Server\Configurator\WithServerShutdownHandler;
use K911\Swoole\Server\Configurator\WithServerStartHandler;
use K911\Swoole\Server\Configurator\WithWorkerStartHandler;
use K911\Swoole\Server\Configurator\WithWorkerStopHandler;
use K911\Swoole\Server\HttpServerConfiguration;
use K911\Swoole\Server\LifecycleHandler\ServerShutdownHandlerInterface;
use K911\Swoole\Server\LifecycleHandler\ServerStartHandlerInterface;
use K911\Swoole\Server\RequestHandler\AdvancedStaticFilesServer;
use K911\Swoole\Server\RequestHandler\RequestHandlerInterface;
use K911\Swoole\Server\Runtime\BootableInterface;
use K911\Swoole\Server\Runtime\HMR\HotModuleReloaderInterface;
use K911\Swoole\Server\Runtime\HMR\InotifyHMR;
use K911\Swoole\Server\WorkerHandler\HMRWorkerStartHandler;
use K911\Swoole\Server\WorkerHandler\WorkerStartHandlerInterface;
use K911\Swoole\Server\WorkerHandler\WorkerStopHandlerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
Expand Down Expand Up @@ -131,9 +138,29 @@ private function registerHttpServerConfiguration(array $config, ContainerBuilder

$this->registerHttpServerHMR($hmr, $container);

if ($container->has(ServerStartHandlerInterface::class)) {
$container->autowire(WithServerStartHandler::class)
->setAutoconfigured(true)
->setPublic(false);

$container->getDefinition('swoole_bundle.server.http_server.configurator.with_sigint_handler')
->addArgument(new Reference(ServerStartHandlerInterface::class));
}

if ($container->has(ServerShutdownHandlerInterface::class)) {
$container->autowire(WithServerShutdownHandler::class)
->setAutoconfigured(true)
->setPublic(false);
}

if ($container->has(WorkerStartHandlerInterface::class)) {
$container->register(WithWorkerStartHandler::class)
->setAutowired(true)
$container->autowire(WithWorkerStartHandler::class)
->setAutoconfigured(true)
->setPublic(false);
}

if ($container->has(WorkerStopHandlerInterface::class)) {
$container->autowire(WithWorkerStopHandler::class)
->setAutoconfigured(true)
->setPublic(false);
}
Expand All @@ -146,18 +173,16 @@ private function registerHttpServerHMR(string $hmr, ContainerBuilder $container)
}

if ('inotify' === $hmr) {
$container->register(HotModuleReloaderInterface::class, InotifyHMR::class)
->setAutowired(true)
$container->autowire(HotModuleReloaderInterface::class, InotifyHMR::class)
->setAutoconfigured(true)
->setPublic(false);
}

if (!$container->has(WorkerStartHandlerInterface::class)) {
$container->register(WorkerStartHandlerInterface::class, HMRWorkerStartHandler::class)
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false);
}
$container->autowire(HMRWorkerStartHandler::class)
->setAutoconfigured(false)
->setPublic(false)
->setArgument('$decorated', new Reference(HMRWorkerStartHandler::class.'.inner', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
->setDecoratedService(WorkerStartHandlerInterface::class);
}

private function resolveAutoHMR(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ services:

'K911\Swoole\Server\Config\Sockets':

'K911\Swoole\Server\WorkerHandler\WorkerStartHandlerInterface':
class: K911\Swoole\Server\WorkerHandler\NoOpWorkerStartHandler

'K911\Swoole\Server\WorkerHandler\WorkerStopHandlerInterface':
class: K911\Swoole\Server\WorkerHandler\NoOpWorkerStopHandler

'K911\Swoole\Server\HttpServerConfiguration':

'K911\Swoole\Server\Configurator\WithHttpServerConfiguration':
Expand Down
11 changes: 10 additions & 1 deletion src/Server/Api/ApiServerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ private function sendRequest(string $path, string $method = ApiServerInterface::
$headers = ['accept' => 'application/json'];
if (!empty($data)) {
$headers['content-type'] = 'application/json';
$client->setData(\json_encode($data, JSON_THROW_ON_ERROR));

$options = \defined('JSON_THROW_ON_ERROR') ? \JSON_THROW_ON_ERROR : 0;
$json = \json_encode($data, $options);

// TODO: Drop on PHP 7.3 Migration
if (!\defined('JSON_THROW_ON_ERROR') && false === $json) {
throw new \RuntimeException(\json_last_error_msg(), \json_last_error());
}

$client->setData($json);
}

$client->setHeaders($headers);
Expand Down
14 changes: 6 additions & 8 deletions src/Server/Api/ApiServerRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,12 @@ private function sendResponse(Response $response, int $statusCode = 200, ?array
$response->header('Content-Type', 'application/json');
$response->status($statusCode);

if (\defined('JSON_THROW_ON_ERROR')) {
$json = \json_encode($data, \JSON_THROW_ON_ERROR);
} else {
// TODO: Drop on PHP 7.3 Migration
$json = \json_encode($data);
if (false === $json) {
throw new \RuntimeException(\json_last_error_msg(), \json_last_error());
}
$options = \defined('JSON_THROW_ON_ERROR') ? \JSON_THROW_ON_ERROR : 0;
$json = \json_encode($data, $options);

// TODO: Drop on PHP 7.3 Migration
if (!\defined('JSON_THROW_ON_ERROR') && false === $json) {
throw new \RuntimeException(\json_last_error_msg(), \json_last_error());
}

$response->end($json);
Expand Down
26 changes: 26 additions & 0 deletions src/Server/Configurator/WithServerShutdownHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\Configurator;

use K911\Swoole\Server\LifecycleHandler\ServerShutdownHandlerInterface;
use Swoole\Http\Server;

final class WithServerShutdownHandler implements ConfiguratorInterface
{
private $handler;

public function __construct(ServerShutdownHandlerInterface $handler)
{
$this->handler = $handler;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$server->on('shutdown', [$this->handler, 'handle']);
}
}
26 changes: 26 additions & 0 deletions src/Server/Configurator/WithWorkerStopHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\Configurator;

use K911\Swoole\Server\WorkerHandler\WorkerStopHandlerInterface;
use Swoole\Http\Server;

final class WithWorkerStopHandler implements ConfiguratorInterface
{
private $handler;

public function __construct(WorkerStopHandlerInterface $handler)
{
$this->handler = $handler;
}

/**
* {@inheritdoc}
*/
public function configure(Server $server): void
{
$server->on('close', [$this->handler, 'handle']);
}
}
17 changes: 17 additions & 0 deletions src/Server/LifecycleHandler/ServerShutdownHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\LifecycleHandler;

use Swoole\Server;

interface ServerShutdownHandlerInterface
{
/**
* Handle "OnShutdown" event.
*
* @param Server $server
*/
public function handle(Server $server): void;
}
11 changes: 11 additions & 0 deletions src/Server/LifecycleHandler/SigIntHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@

final class SigIntHandler implements ServerStartHandlerInterface
{
private $decorated;

public function __construct(?ServerStartHandlerInterface $decorated = null)
{
$this->decorated = $decorated;
}

/**
* {@inheritdoc}
*/
public function handle(Server $server): void
{
// 2 => SIGINT
Process::signal(2, [$server, 'shutdown']);

if ($this->decorated instanceof ServerStartHandlerInterface) {
$this->decorated->handle($server);
}
}
}
8 changes: 7 additions & 1 deletion src/Server/WorkerHandler/HMRWorkerStartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ final class HMRWorkerStartHandler implements WorkerStartHandlerInterface
{
private $hmr;
private $interval;
private $decorated;

public function __construct(HotModuleReloaderInterface $hmr, int $interval = 2000)
public function __construct(HotModuleReloaderInterface $hmr, int $interval = 2000, ?WorkerStartHandlerInterface $decorated = null)
{
$this->hmr = $hmr;
$this->interval = $interval;
$this->decorated = $decorated;
}

/**
* {@inheritdoc}
*/
public function handle(Server $worker, int $workerId): void
{
if ($this->decorated instanceof WorkerStartHandlerInterface) {
$this->decorated->handle($worker, $workerId);
}

if ($worker->taskworker) {
return;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Server/WorkerHandler/NoOpWorkerStartHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\WorkerHandler;

use Swoole\Server;

class NoOpWorkerStartHandler implements WorkerStartHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $worker, int $workerId): void
{
// noop
}
}
18 changes: 18 additions & 0 deletions src/Server/WorkerHandler/NoOpWorkerStopHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\WorkerHandler;

use Swoole\Server;

class NoOpWorkerStopHandler implements WorkerStopHandlerInterface
{
/**
* {@inheritdoc}
*/
public function handle(Server $worker, int $workerId): void
{
// noop
}
}
10 changes: 5 additions & 5 deletions src/Server/WorkerHandler/WorkerStartHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ interface WorkerStartHandlerInterface
*
* To differentiate between server worker and task worker use snippet:
*
* ```php
* if($server->taskworker) {
* echo "Hello from task worker process";
* }
* ```
* ```php
* if($server->taskworker) {
* echo "Hello from task worker process";
* }
* ```
*
* @param Server $worker
* @param int $workerId
Expand Down
20 changes: 20 additions & 0 deletions src/Server/WorkerHandler/WorkerStopHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace K911\Swoole\Server\WorkerHandler;

use Swoole\Server;

interface WorkerStopHandlerInterface
{
/**
* Handle onWorkerStop event.
* Info: Function will be executed in worker process
* Warning: The abnormal stop of worker process will not trigger the event workerstop, for example, fatal error, core dump.
*
* @param Server $worker
* @param int $workerId
*/
public function handle(Server $worker, int $workerId): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class CodeCoverageManager
private $codeCoverage;
private $writer;

private $finished = false;

public function __construct(ParameterBagInterface $parameterBag, CodeCoverage $codeCoverage, PHP $writer)
{
$this->codeCoverage = $codeCoverage;
Expand Down Expand Up @@ -60,7 +62,7 @@ public function start(?string $testName = null): void

public function stop(): void
{
if (!$this->enabled) {
if (!$this->enabled || $this->finished) {
return;
}

Expand All @@ -69,7 +71,7 @@ public function stop(): void

public function finish(?string $fileName = null, ?string $path = null): void
{
if (!$this->enabled) {
if (!$this->enabled || $this->finished) {
return;
}

Expand All @@ -78,6 +80,7 @@ public function finish(?string $fileName = null, ?string $path = null): void

$this->writer->process($this->codeCoverage, \sprintf('%s/%s', $path ?? $this->coveragePath, $fileName));
$this->codeCoverage->clear();
$this->finished = true;
}

private function initalizeCodeCoverage(ParameterBagInterface $parameterBag, CodeCoverage $codeCoverage): void
Expand Down
Loading

0 comments on commit 0591ece

Please sign in to comment.