Skip to content

Fix e2e tests with actual sub-processes #13

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

Merged
merged 5 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/.php_cs.cache
/.phpunit.result.cache
/composer.lock
/tests/output
/var/
/vendor-bin/*/vendor/
/vendor/
2 changes: 1 addition & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
'no_php4_constructor' => true,
'no_superfluous_elseif' => true,
'no_unset_cast' => true,
'no_unset_on_property' => true,
'no_unset_on_property' => false,
'no_useless_else' => true,
'no_useless_return' => true,
'nullable_type_declaration_for_default_null_value' => true,
Expand Down
17 changes: 17 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

require __DIR__.'/../vendor/autoload.php';

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Webmozarts\Console\Parallelization\ImportMoviesCommand;
use Webmozarts\Console\Parallelization\Kernel;
use Webmozarts\Console\Parallelization\NoSubProcessCommand;

$application = new Application(new Kernel());
$application->add(new ImportMoviesCommand());
$application->add(new NoSubProcessCommand());

$application->run();
4 changes: 3 additions & 1 deletion src/Parallelization.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -348,7 +349,8 @@ function () use ($input) {
$this->getEnvironmentVariables($this->getContainer()),
$numberOfProcesses,
$segmentSize,
$this->getContainer()->get('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
// TODO: offer a way to create the process launcher in a different manner
new ConsoleLogger($output),
function (string $type, string $buffer) use ($progressBar, $output, $terminalWidth) {
$this->processChildOutput($buffer, $progressBar, $output, $terminalWidth);
}
Expand Down
89 changes: 80 additions & 9 deletions tests/ImportMoviesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace Webmozarts\Console\Parallelization;

use function file_get_contents;
use function json_decode;
use const JSON_THROW_ON_ERROR;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -22,35 +25,103 @@ final class ImportMoviesCommand extends ContainerAwareCommand

protected static $defaultName = 'import:movies';

private TestLogger $logger;

/**
* @var array<string, string>
*/
private array $batchMovies;

public function __construct()
{
parent::__construct(self::$defaultName);

$this->logger = new TestLogger();
}

protected function configure(): void
{
self::configureParallelization($this);
}

protected function fetchItems(InputInterface $input): array
{
// open up the file and read movie data...

// return items as strings
return [
'{"id": 1, "name": "Star Wars"}',
'{"id": 2, "name": "Django Unchained"}',
// ...
'movie-1.json',
'movie-2.json',
'movie-3.json',
'movie-4.json',
'movie-5.json',
];
}

protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
protected function getSegmentSize(): int
{
// insert into the database
return 2;
}

protected function runBeforeFirstCommand(InputInterface $input, OutputInterface $output): void
{
$this->logger->recordFirstCommand();
}

protected function runBeforeBatch(
InputInterface $input,
OutputInterface $output,
array $movieFileNames
): void {
$this->logger->recordBeforeBatch();

$this->batchMovies = self::fetchMovieTitles($movieFileNames);
}

protected function runSingleCommand(string $movieFileName, InputInterface $input, OutputInterface $output): void
{
$this->logger->recordSingleCommand(
$movieFileName,
$this->batchMovies[$movieFileName],
);
}

protected function runAfterBatch(InputInterface $input, OutputInterface $output, array $items): void
{
// flush the database and clear the entity manager
$this->logger->recordAfterBatch();

unset($this->batchMovies);
}

protected function runAfterLastCommand(InputInterface $input, OutputInterface $output): void
{
$this->logger->recordLastCommand();
}

protected function getItemName(int $count): string
{
return 1 === $count ? 'movie' : 'movies';
}

/**
* @param list<string> $movieFileNames
*
* @return array<string, string>
*/
private static function fetchMovieTitles(array $movieFileNames): array
{
$movies = [];

foreach ($movieFileNames as $movieFileName) {
$moviePath = __DIR__.'/movies/'.$movieFileName;

$decodedContent = json_decode(
file_get_contents($moviePath),
null,
512,
JSON_THROW_ON_ERROR,
);

$movies[$movieFileName] = $decodedContent->title;
}

return $movies;
}
}
58 changes: 58 additions & 0 deletions tests/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Kernel as HttpKernel;

final class Kernel extends HttpKernel
{
public function __construct()
{
parent::__construct('dev', true);
}

public function registerBundles(): array
{
return [];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
}

protected function build(ContainerBuilder $container): void
{
$eventDispatcherDefinition = new Definition(
EventDispatcher::class,
[]
);
$eventDispatcherDefinition->setPublic(true);

$loggerDefinition = new Definition(
ConsoleLogger::class,
[]
);
$loggerDefinition->setPublic(true);

$container->addDefinitions([
'event_dispatcher' => $eventDispatcherDefinition,
'logger' => $loggerDefinition,
]);
}
}
65 changes: 65 additions & 0 deletions tests/NoSubProcessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization;

use DomainException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class NoSubProcessCommand extends ContainerAwareCommand
{
use Parallelization;

protected static $defaultName = 'test:no-subprocess';

private bool $mainProcess = false;

protected function configure(): void
{
self::configureParallelization($this);
}

protected function fetchItems(InputInterface $input): array
{
return [
'item1',
'item2',
'item3',
'item4',
'item5',
];
}

protected function getSegmentSize(): int
{
return 2;
}

protected function runBeforeFirstCommand(InputInterface $input, OutputInterface $output): void
{
$this->mainProcess = true;
}

protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
{
if (!$this->mainProcess) {
throw new DomainException('Expected to be executed within the main process.');
}
}

protected function getItemName(int $count): string
{
return 0 === $count ? 'item' : 'items';
}
}
Loading