Extracted Process Control to separate interface to allow better tests
This commit is contained in:
61
src/Tests/ProcessControl/ForkTest.php
Normal file
61
src/Tests/ProcessControl/ForkTest.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\Tests\ProcessControl;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Toalett\Multiprocessing\ProcessControl\Fork;
|
||||
|
||||
class ForkTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param int $pid
|
||||
* @dataProvider positiveIntegerProvider
|
||||
*/
|
||||
public function testItSaysItIsAParentProcessWhenAPositivePidIsProvided(int $pid): void
|
||||
{
|
||||
$fork = new Fork($pid);
|
||||
self::assertTrue($fork->isParent());
|
||||
self::assertFalse($fork->isChild());
|
||||
self::assertFalse($fork->failed());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $pid
|
||||
* @dataProvider negativeIntegerProvider
|
||||
*/
|
||||
public function testItSaysItFailedWhenANegativePidIsProvided(int $pid): void
|
||||
{
|
||||
$fork = new Fork($pid);
|
||||
self::assertTrue($fork->isParent());
|
||||
self::assertFalse($fork->isChild());
|
||||
self::assertTrue($fork->failed());
|
||||
}
|
||||
|
||||
public function testItSaysItIsAChildProcessWhenPidZeroIsProvided(): void
|
||||
{
|
||||
$fork = new Fork(0);
|
||||
self::assertFalse($fork->isParent());
|
||||
self::assertTrue($fork->isChild());
|
||||
self::assertFalse($fork->failed());
|
||||
}
|
||||
|
||||
public function positiveIntegerProvider(): array
|
||||
{
|
||||
return [
|
||||
[1],
|
||||
[10],
|
||||
[1000],
|
||||
[PHP_INT_MAX],
|
||||
];
|
||||
}
|
||||
|
||||
public function negativeIntegerProvider(): array
|
||||
{
|
||||
return [
|
||||
[-1],
|
||||
[-10],
|
||||
[-1000],
|
||||
[PHP_INT_MIN],
|
||||
];
|
||||
}
|
||||
}
|
||||
51
src/Tests/ProcessControl/WaitTest.php
Normal file
51
src/Tests/ProcessControl/WaitTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\Tests\ProcessControl;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Toalett\Multiprocessing\ProcessControl\Wait;
|
||||
|
||||
class WaitTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param int $pid
|
||||
* @dataProvider positiveIntegerProvider
|
||||
*/
|
||||
public function testItSaysAChildStoppedWhenAPositivePidIsProvided(int $pid): void
|
||||
{
|
||||
$wait = new Wait($pid, 0);
|
||||
self::assertTrue($wait->childStopped());
|
||||
self::assertFalse($wait->failed());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $pid
|
||||
* @dataProvider negativeIntegerProvider
|
||||
*/
|
||||
public function testItSaysItFailedWhenANegativePidIsProvided(int $pid): void
|
||||
{
|
||||
$wait = new Wait($pid, 0);
|
||||
self::assertFalse($wait->childStopped());
|
||||
self::assertTrue($wait->failed());
|
||||
}
|
||||
|
||||
public function positiveIntegerProvider(): array
|
||||
{
|
||||
return [
|
||||
[1],
|
||||
[10],
|
||||
[1000],
|
||||
[PHP_INT_MAX],
|
||||
];
|
||||
}
|
||||
|
||||
public function negativeIntegerProvider(): array
|
||||
{
|
||||
return [
|
||||
[-1],
|
||||
[-10],
|
||||
[-1000],
|
||||
[PHP_INT_MIN],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,22 +2,55 @@
|
||||
|
||||
namespace Toalett\Multiprocessing\Tests;
|
||||
|
||||
use ReflectionObject;
|
||||
use Toalett\Multiprocessing\Workers;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionObject;
|
||||
use Toalett\Multiprocessing\ProcessControl\Fork;
|
||||
use Toalett\Multiprocessing\ProcessControl\ProcessControl;
|
||||
use Toalett\Multiprocessing\ProcessControl\Wait;
|
||||
use Toalett\Multiprocessing\Workers;
|
||||
|
||||
class WorkersTest extends TestCase
|
||||
{
|
||||
public function testItSaysItIsEmptyWhenNoWorkers(): void
|
||||
{
|
||||
$processControl = $this->createMock(ProcessControl::class);
|
||||
$workers = new Workers($processControl);
|
||||
self::assertEmpty($workers);
|
||||
}
|
||||
|
||||
public function testItSaysItHasOneWorkerWhenTaskExecutes(): void
|
||||
{
|
||||
$workers = new Workers();
|
||||
|
||||
$workers->createWorkerFor(fn() => exit(0), []);
|
||||
self::assertCount(1, $workers);
|
||||
}
|
||||
|
||||
public function testItGivesTheAmountOfActiveWorkersOnCount(): void
|
||||
{
|
||||
$workers = new Workers();
|
||||
|
||||
$workers->createWorkerFor(fn() => exit(0), []);
|
||||
$workers->createWorkerFor(fn() => exit(0), []);
|
||||
self::assertCount(2, $workers);
|
||||
|
||||
$workers->createWorkerFor(fn() => exit(0), []);
|
||||
self::assertCount(3, $workers);
|
||||
|
||||
$workers->stop();
|
||||
self::assertEmpty($workers);
|
||||
}
|
||||
|
||||
public function testItEmitsAnEventWhenAWorkerIsStarted(): void
|
||||
{
|
||||
$workers = new Workers();
|
||||
|
||||
$workerStartedEventHasTakenPlace = false;
|
||||
$workers->on('worker_started', function() use (&$workerStartedEventHasTakenPlace) {
|
||||
$workers->on('worker_started', function () use (&$workerStartedEventHasTakenPlace) {
|
||||
$workerStartedEventHasTakenPlace = true;
|
||||
});
|
||||
|
||||
self::assertFalse($workerStartedEventHasTakenPlace);
|
||||
|
||||
$workers->createWorkerFor(fn() => exit(0), []);
|
||||
self::assertTrue($workerStartedEventHasTakenPlace);
|
||||
}
|
||||
@@ -30,7 +63,7 @@ class WorkersTest extends TestCase
|
||||
$method->setAccessible(true);
|
||||
|
||||
$workerStoppedEventHasTakenPlace = false;
|
||||
$workers->on('worker_stopped', function() use (&$workerStoppedEventHasTakenPlace) {
|
||||
$workers->on('worker_stopped', function () use (&$workerStoppedEventHasTakenPlace) {
|
||||
$workerStoppedEventHasTakenPlace = true;
|
||||
});
|
||||
|
||||
@@ -38,4 +71,39 @@ class WorkersTest extends TestCase
|
||||
$method->invoke($workers, 0);
|
||||
self::assertTrue($workerStoppedEventHasTakenPlace);
|
||||
}
|
||||
|
||||
public function testItCallsForkOnProcessControlWhenAskedToCreateAWorker(): void
|
||||
{
|
||||
$processControl = $this->createMock(ProcessControl::class);
|
||||
$processControl->expects(self::once())
|
||||
->method('fork')
|
||||
->willReturn(new Fork(1));
|
||||
|
||||
$workers = new Workers($processControl);
|
||||
$workers->createWorkerFor(fn() => []);
|
||||
}
|
||||
|
||||
public function testItCallsNonBlockingWaitOnProcessControlWhenPerformingCleanup(): void
|
||||
{
|
||||
$processControl = $this->createMock(ProcessControl::class);
|
||||
$processControl->expects(self::once())
|
||||
->method('wait')
|
||||
->with(WNOHANG)
|
||||
->willReturn(new Wait(0));
|
||||
|
||||
$workers = new Workers($processControl);
|
||||
$workers->cleanup();
|
||||
}
|
||||
|
||||
public function testItCallsBlockingWaitOnProcessControlWhenAwaitingCongestionRelief(): void
|
||||
{
|
||||
$processControl = $this->createMock(ProcessControl::class);
|
||||
$processControl->expects(self::once())
|
||||
->method('wait')
|
||||
->with(/* no arguments */)
|
||||
->willReturn(new Wait(1));
|
||||
|
||||
$workers = new Workers($processControl);
|
||||
$workers->awaitCongestionRelief();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user