Skip to content

Commit

Permalink
Added tests for testing scheduler and process manager
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Feb 12, 2024
1 parent d33bf2d commit c6f554a
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 13 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
</source>
<php>
<env name="KERNEL_CLASS" value="Luzrain\PhpRunnerBundle\Test\App\Kernel" />
<env name="APP_RUNTIME" value="Luzrain\PhpRunnerBundle\Runtime" />
<env name="APP_ENV" value="test" />
<env name="SHELL_VERBOSITY" value="1" />
</php>
</phpunit>
16 changes: 16 additions & 0 deletions tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,25 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
'processes' => 1,
],
],
'tasks' => [
[
'command' => 'app:test_task',
'schedule' => '1 second',
],
],
'processes' => [
[
'command' => 'app:test_process',
],
],
]);

$container->setParameter('task_status_file', '%kernel.project_dir%/var/task_status.log');
$container->setParameter('process_status_file', '%kernel.project_dir%/var/process_status.log');

$container->autowire(RequestTestController::class)->setAutoconfigured(true);
$container->autowire(TestTask::class)->setAutoconfigured(true);
$container->autowire(TestProcess::class)->setAutoconfigured(true);
});
}

Expand Down
21 changes: 15 additions & 6 deletions tests/App/TestProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@

namespace Luzrain\PhpRunnerBundle\Test\App;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

//#[AsProcess(name: 'Test process')]
final class TestProcess
#[AsCommand(
name: 'app:test_process',
description: 'test process',
)]
final class TestProcess extends Command
{
public function __construct(
#[Autowire(value: '%kernel.project_dir%/var/process_status.log')]
#[Autowire(param: 'process_status_file')]
private string $statusFile,
) {
parent::__construct();
}

public function __invoke(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
\file_put_contents($this->statusFile, \time());
\sleep(1);
exit;
\sleep(300);

return Command::SUCCESS;
}
}
18 changes: 14 additions & 4 deletions tests/App/TestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@

namespace Luzrain\PhpRunnerBundle\Test\App;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

//#[AsTask(name: 'Test task', schedule: '1 second')]
final class TestTask
#[AsCommand(
name: 'app:test_task',
description: 'test task',
)]
final class TestTask extends Command
{
public function __construct(
#[Autowire(value: '%kernel.project_dir%/var/task_status.log')]
#[Autowire(param: 'task_status_file')]
private string $statusFile,
) {
parent::__construct();
}

public function __invoke(): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
\file_put_contents($this->statusFile, \time());

return Command::SUCCESS;
}
}
5 changes: 5 additions & 0 deletions tests/App/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

\startServer();
\register_shutdown_function(stopServer(...));
\register_shutdown_function(static function () {
if (\is_dir($dir = \realpath(__DIR__ . '/../../var/cache/test'))) {
\exec("rm -rf $dir");
}
});

function startServer(): void
{
Expand Down
3 changes: 0 additions & 3 deletions tests/App/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

require_once \dirname(__DIR__, 2) . '/vendor/autoload_runtime.php';

$_SERVER['APP_RUNTIME'] = 'Luzrain\PhpRunnerBundle\Runtime';
$_SERVER['SHELL_VERBOSITY'] = -1;

return function (array $context) {
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};
40 changes: 40 additions & 0 deletions tests/ProcessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Luzrain\PhpRunnerBundle\Test;

use Revolt\EventLoop\Driver\StreamSelectDriver;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class ProcessTest extends KernelTestCase
{
protected function tearDown(): void
{
if (\is_file($file = $this->getContainer()->getParameter('process_status_file'))) {
\unlink($file);
}
}

public function testProcessIsLive(): void
{
$content = $this->getProcessStatusFileContent() ?? $this->fail('Process status file is not found');

$this->assertTrue((int) $content > \time() - 4, 'Process started more than 4 seconds ago');
}

private function getProcessStatusFileContent(): string|null
{
$file = $this->getContainer()->getParameter('process_status_file');
$eventLoop = new StreamSelectDriver();
$suspension = $eventLoop->getSuspension();
$eventLoop->delay(3, fn() => $suspension->resume());
$eventLoop->repeat(0.5, function () use ($file, $suspension) {
if ((\file_exists($file) && $content = @\file_get_contents($file))) {
$suspension->resume($content);
}
});

return $suspension->suspend();
}
}
40 changes: 40 additions & 0 deletions tests/TaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Luzrain\PhpRunnerBundle\Test;

use Revolt\EventLoop\Driver\StreamSelectDriver;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class TaskTest extends KernelTestCase
{
protected function tearDown(): void
{
if (\is_file($file = $this->getContainer()->getParameter('task_status_file'))) {
\unlink($file);
}
}

public function testTaskIsRunning(): void
{
$content = $this->getTaskStatusFileContent() ?? $this->fail('Task status file is not found');

$this->assertTrue((int) $content > \time() - 4, 'Task was called more than 4 seconds ago');
}

private function getTaskStatusFileContent(): string|null
{
$file = $this->getContainer()->getParameter('task_status_file');
$eventLoop = new StreamSelectDriver();
$suspension = $eventLoop->getSuspension();
$eventLoop->delay(3, fn() => $suspension->resume());
$eventLoop->repeat(0.5, function () use ($file, $suspension) {
if ((\file_exists($file) && $content = @\file_get_contents($file))) {
$suspension->resume($content);
}
});

return $suspension->suspend();
}
}

0 comments on commit c6f554a

Please sign in to comment.