Use CallableProvider in tests

This commit is contained in:
Joop Schilder 2020-12-14 12:57:07 +01:00
parent dd48e20b8f
commit a6a993db3e
3 changed files with 30 additions and 23 deletions

View File

@ -8,10 +8,13 @@ use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\Timer;
use Toalett\Multiprocessing\Concurrency;
use Toalett\Multiprocessing\Context;
use Toalett\Multiprocessing\Tests\Tools\CallableProvider;
use Toalett\Multiprocessing\Workers;
class ContextTest extends TestCase
{
use CallableProvider;
public function testItEmitsAnEventWhenBooted(): void
{
$concurrency = $this->createMock(Concurrency::class);
@ -52,7 +55,7 @@ class ContextTest extends TestCase
self::assertFalse($congestionRelievedEventHasTakenPlace);
$loop->futureTick(fn() => $context->stop());
$context->submit(static fn() => null);
$context->submit(self::emptyCallable());
$context->run();
self::assertTrue($congestionEventHasTakenPlace);
@ -68,9 +71,7 @@ class ContextTest extends TestCase
$concurrency->method('isReachedBy')->willReturn(false);
$loop->expects(self::once())
->method('futureTick')
->withConsecutive([
static fn() => null,
]);
->with(self::emptyCallable());
$context->submit(static fn() => null);
}
@ -83,11 +84,11 @@ class ContextTest extends TestCase
$loop->expects(self::exactly(2))
->method('addPeriodicTimer')
->withConsecutive(
[Context::INTERVAL_CLEANUP, static fn() => null],
[Context::INTERVAL_GC, static fn() => null]
[Context::INTERVAL_CLEANUP, self::emptyCallable()],
[Context::INTERVAL_GC, self::emptyCallable()]
)->willReturnOnConsecutiveCalls(
new Timer(Context::INTERVAL_CLEANUP, static fn() => null),
new Timer(Context::INTERVAL_GC, static fn() => null),
new Timer(Context::INTERVAL_CLEANUP, self::emptyCallable()),
new Timer(Context::INTERVAL_GC, self::emptyCallable()),
);
$context = new Context($loop, $concurrency);
@ -103,9 +104,9 @@ class ContextTest extends TestCase
$workers->expects(self::exactly(3))
->method('on')
->withConsecutive(
['worker_started', static fn() => null],
['worker_stopped', static fn() => null],
['no_workers_remaining', static fn() => null]
['worker_started', self::emptyCallable()],
['worker_stopped', self::emptyCallable()],
['no_workers_remaining', self::emptyCallable()]
);
new Context($loop, $concurrency, $workers);

View File

@ -8,9 +8,12 @@ use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\Timer;
use Toalett\Multiprocessing\Task\Interval;
use Toalett\Multiprocessing\Task\RepeatedTask;
use Toalett\Multiprocessing\Tests\Tools\CallableProvider;
class RepeatedTaskTest extends TestCase
{
use CallableProvider;
/**
* @param $interval
* @dataProvider dataProvider
@ -20,10 +23,10 @@ class RepeatedTaskTest extends TestCase
$loop = $this->createMock(LoopInterface::class);
$loop->expects(self::once())
->method('addPeriodicTimer')
->with($interval->asFloat(), static fn() => null)
->willReturn(new Timer($interval->asFloat(), static fn() => null, true));
->with($interval->asFloat(), self::emptyCallable())
->willReturn(new Timer($interval->asFloat(), self::emptyCallable(), true));
$task = new RepeatedTask($interval, static fn() => null);
$task = new RepeatedTask($interval, self::emptyCallable());
$task->enable($loop);
}

View File

@ -7,10 +7,13 @@ use ReflectionObject;
use Toalett\Multiprocessing\ProcessControl\Fork;
use Toalett\Multiprocessing\ProcessControl\ProcessControl;
use Toalett\Multiprocessing\ProcessControl\Wait;
use Toalett\Multiprocessing\Tests\Tools\CallableProvider;
use Toalett\Multiprocessing\Workers;
class WorkersTest extends TestCase
{
use CallableProvider;
public function testItSaysItIsEmptyWhenNoWorkers(): void
{
$processControl = $this->createMock(ProcessControl::class);
@ -22,7 +25,7 @@ class WorkersTest extends TestCase
{
$workers = new Workers();
$workers->createWorkerFor(fn() => exit(0), []);
$workers->createWorkerFor(self::emptyCallable(), []);
self::assertCount(1, $workers);
}
@ -30,11 +33,11 @@ class WorkersTest extends TestCase
{
$workers = new Workers();
$workers->createWorkerFor(fn() => exit(0), []);
$workers->createWorkerFor(fn() => exit(0), []);
$workers->createWorkerFor(self::emptyCallable(), []);
$workers->createWorkerFor(self::emptyCallable(), []);
self::assertCount(2, $workers);
$workers->createWorkerFor(fn() => exit(0), []);
$workers->createWorkerFor(self::emptyCallable(), []);
self::assertCount(3, $workers);
$workers->stop();
@ -51,7 +54,7 @@ class WorkersTest extends TestCase
});
self::assertFalse($workerStartedEventHasTakenPlace);
$workers->createWorkerFor(fn() => exit(0), []);
$workers->createWorkerFor(self::emptyCallable(), []);
self::assertTrue($workerStartedEventHasTakenPlace);
}
@ -59,8 +62,8 @@ class WorkersTest extends TestCase
{
$workers = new Workers();
$reflector = new ReflectionObject($workers);
$method = $reflector->getMethod('remove');
$method->setAccessible(true);
$remove = $reflector->getMethod('remove');
$remove->setAccessible(true);
$workerStoppedEventHasTakenPlace = false;
$workers->on('worker_stopped', function () use (&$workerStoppedEventHasTakenPlace) {
@ -68,7 +71,7 @@ class WorkersTest extends TestCase
});
self::assertFalse($workerStoppedEventHasTakenPlace);
$method->invoke($workers, 0);
$remove->invoke($workers, 0);
self::assertTrue($workerStoppedEventHasTakenPlace);
}
@ -94,7 +97,7 @@ class WorkersTest extends TestCase
->willReturn(new Fork(1));
$workers = new Workers($processControl);
$workers->createWorkerFor(fn() => []);
$workers->createWorkerFor(self::emptyCallable());
}
public function testItCallsNonBlockingWaitOnProcessControlWhenPerformingCleanup(): void