1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-12 09:09:35 +01:00
parallel/test/Sync/AbstractParcelTest.php

57 lines
1.4 KiB
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
2015-08-28 23:58:15 +02:00
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Test\Sync;
2016-08-18 18:04:48 +02:00
2019-08-27 19:17:41 +02:00
use Amp\Parallel\Sync\Parcel;
use Amp\PHPUnit\AsyncTestCase;
2015-08-28 23:58:15 +02:00
2019-08-27 19:17:41 +02:00
abstract class AbstractParcelTest extends AsyncTestCase
2018-10-07 16:50:45 +02:00
{
2019-08-27 19:17:41 +02:00
abstract protected function createParcel($value): Parcel;
2015-08-28 23:58:15 +02:00
2018-10-07 16:50:45 +02:00
public function testUnwrapIsOfCorrectType()
{
2017-11-29 21:40:07 +01:00
$object = $this->createParcel(new \stdClass);
2019-08-27 19:17:41 +02:00
$this->assertInstanceOf('stdClass', yield $object->unwrap());
2015-08-28 23:58:15 +02:00
}
2018-10-07 16:50:45 +02:00
public function testUnwrapIsEqual()
{
2016-08-23 01:25:19 +02:00
$object = new \stdClass;
2015-08-28 23:58:15 +02:00
$shared = $this->createParcel($object);
2019-08-27 19:17:41 +02:00
$this->assertEquals($object, yield $shared->unwrap());
2015-08-28 23:58:15 +02:00
}
/**
* @depends testUnwrapIsEqual
*/
2018-10-07 16:50:45 +02:00
public function testSynchronized()
{
$parcel = $this->createParcel(0);
2016-08-19 00:36:58 +02:00
$awaitable = $parcel->synchronized(function ($value) {
$this->assertSame(0, $value);
2018-10-07 16:50:45 +02:00
\usleep(10000);
return 1;
2016-08-19 00:36:58 +02:00
});
$callback = $this->createCallback(1);
$callback->method('__invoke')
2016-08-19 00:36:58 +02:00
->with($this->identicalTo(null), $this->identicalTo(1));
2017-03-22 00:45:23 +01:00
$awaitable->onResolve($callback);
2016-08-19 00:36:58 +02:00
$awaitable = $parcel->synchronized(function ($value) {
$this->assertSame(1, $value);
2018-10-07 16:50:45 +02:00
\usleep(10000);
return 2;
2016-08-19 00:36:58 +02:00
});
$callback = $this->createCallback(1);
$callback->method('__invoke')
2016-08-19 00:36:58 +02:00
->with($this->identicalTo(null), $this->identicalTo(2));
2017-03-22 00:45:23 +01:00
$awaitable->onResolve($callback);
}
2015-08-28 23:58:15 +02:00
}