2020-12-11 01:25:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Toalett\Multiprocessing\Tests;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use React\EventLoop\LoopInterface;
|
2020-12-12 13:05:48 +01:00
|
|
|
use Toalett\Multiprocessing\Concurrency;
|
2020-12-11 01:25:38 +01:00
|
|
|
use Toalett\Multiprocessing\ContextBuilder;
|
|
|
|
use Toalett\Multiprocessing\Tests\Tools\PropertyInspector;
|
2020-12-12 02:11:05 +01:00
|
|
|
use Toalett\Multiprocessing\Workers;
|
2020-12-11 01:25:38 +01:00
|
|
|
|
|
|
|
class ContextBuilderTest extends TestCase
|
|
|
|
{
|
2020-12-12 13:05:48 +01:00
|
|
|
use PropertyInspector;
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
public function testItIsImmutable(): void
|
|
|
|
{
|
|
|
|
$builder = ContextBuilder::create();
|
|
|
|
$eventLoop = $this->createMock(LoopInterface::class);
|
|
|
|
$concurrency = $this->createMock(Concurrency::class);
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
self::assertNotSame($builder->withEventLoop($eventLoop), $builder);
|
|
|
|
self::assertNotSame($builder->withConcurrency($concurrency), $builder);
|
|
|
|
}
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
public function testItBuildsANewContextEveryTime(): void
|
|
|
|
{
|
|
|
|
$builder = ContextBuilder::create();
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
self::assertNotSame($builder->build(), $builder->build());
|
|
|
|
}
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
public function testTheDefaultConcurrencyIsUnlimited(): void
|
|
|
|
{
|
|
|
|
$builder = ContextBuilder::create();
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
$context = $builder->build();
|
|
|
|
self::assertIsObject($context);
|
|
|
|
self::assertInstanceOf(LoopInterface::class, $this->getProperty($context, 'eventLoop'));
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
/** @var Concurrency|null $concurrency */
|
|
|
|
$concurrency = $this->getProperty($context, 'concurrency');
|
|
|
|
self::assertIsObject($concurrency);
|
|
|
|
self::assertInstanceOf(Concurrency::class, $concurrency);
|
|
|
|
self::assertTrue($concurrency->isUnlimited());
|
|
|
|
}
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
public function testWhenGivenAnEventLoopItUsesThatLoop(): void
|
|
|
|
{
|
|
|
|
$builder = ContextBuilder::create();
|
|
|
|
$eventLoop = $this->createMock(LoopInterface::class);
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
$context = $builder->withEventLoop($eventLoop)->build();
|
|
|
|
$usedEventLoop = $this->getProperty($context, 'eventLoop');
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
self::assertSame($eventLoop, $usedEventLoop);
|
|
|
|
}
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
public function testWhenGivenAConcurrencyItUsesThatConcurrency(): void
|
|
|
|
{
|
|
|
|
$builder = ContextBuilder::create();
|
|
|
|
$concurrency = $this->createMock(Concurrency::class);
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
$context = $builder->withConcurrency($concurrency)->build();
|
|
|
|
$usedConcurrency = $this->getProperty($context, 'concurrency');
|
2020-12-11 01:25:38 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
self::assertSame($concurrency, $usedConcurrency);
|
|
|
|
}
|
2020-12-12 02:11:05 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
public function testWhenGivenWorkersItUsesThatWorkers(): void
|
|
|
|
{
|
|
|
|
$builder = ContextBuilder::create();
|
|
|
|
$workers = $this->createMock(Workers::class);
|
2020-12-12 02:11:05 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
$context = $builder->withWorkers($workers)->build();
|
|
|
|
$usedWorkers = $this->getProperty($context, 'workers');
|
2020-12-12 02:11:05 +01:00
|
|
|
|
2020-12-12 13:05:48 +01:00
|
|
|
self::assertSame($workers, $usedWorkers);
|
|
|
|
}
|
2020-12-11 01:25:38 +01:00
|
|
|
}
|