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
|
|
|
|
2017-11-27 06:03:18 +01:00
|
|
|
use Amp\Loop;
|
2017-03-22 05:19:15 +01:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2015-08-28 23:58:15 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
abstract class AbstractParcelTest extends TestCase {
|
2015-08-28 23:58:15 +02:00
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @return \Amp\Parallel\Sync\Parcel
|
2015-08-28 23:58:15 +02:00
|
|
|
*/
|
|
|
|
abstract protected function createParcel($value);
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testConstructor() {
|
2015-08-28 23:58:15 +02:00
|
|
|
$object = $this->createParcel(new \stdClass());
|
|
|
|
$this->assertInternalType('object', $object->unwrap());
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testUnwrapIsOfCorrectType() {
|
2015-08-28 23:58:15 +02:00
|
|
|
$object = $this->createParcel(new \stdClass());
|
|
|
|
$this->assertInstanceOf('stdClass', $object->unwrap());
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +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);
|
|
|
|
$this->assertEquals($object, $shared->unwrap());
|
|
|
|
}
|
|
|
|
|
2015-09-04 01:10:19 +02:00
|
|
|
/**
|
|
|
|
* @depends testUnwrapIsEqual
|
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testSynchronized() {
|
2015-09-04 01:10:19 +02:00
|
|
|
$parcel = $this->createParcel(0);
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$awaitable = $parcel->synchronized(function ($value) {
|
2015-10-18 09:12:46 +02:00
|
|
|
$this->assertSame(0, $value);
|
2016-08-22 06:40:48 +02:00
|
|
|
usleep(10000);
|
2015-10-18 09:12:46 +02:00
|
|
|
return 1;
|
2016-08-19 00:36:58 +02:00
|
|
|
});
|
2015-09-04 01:10:19 +02:00
|
|
|
|
|
|
|
$callback = $this->createCallback(1);
|
|
|
|
$callback->method('__invoke')
|
2016-08-19 00:36:58 +02:00
|
|
|
->with($this->identicalTo(null), $this->identicalTo(1));
|
2015-09-04 01:10:19 +02:00
|
|
|
|
2017-03-22 00:45:23 +01:00
|
|
|
$awaitable->onResolve($callback);
|
2015-09-04 01:10:19 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$awaitable = $parcel->synchronized(function ($value) {
|
2015-10-18 09:12:46 +02:00
|
|
|
$this->assertSame(1, $value);
|
2016-08-22 06:40:48 +02:00
|
|
|
usleep(10000);
|
2015-10-18 09:12:46 +02:00
|
|
|
return 2;
|
2016-08-19 00:36:58 +02:00
|
|
|
});
|
2015-09-04 01:10:19 +02:00
|
|
|
|
|
|
|
$callback = $this->createCallback(1);
|
|
|
|
$callback->method('__invoke')
|
2016-08-19 00:36:58 +02:00
|
|
|
->with($this->identicalTo(null), $this->identicalTo(2));
|
2015-09-04 01:10:19 +02:00
|
|
|
|
2017-03-22 00:45:23 +01:00
|
|
|
$awaitable->onResolve($callback);
|
2015-09-04 01:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-18 09:12:46 +02:00
|
|
|
* @depends testSynchronized
|
2015-09-04 01:10:19 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testCloneIsNewParcel() {
|
2017-11-27 06:03:18 +01:00
|
|
|
Loop::run(function () {
|
|
|
|
$original = $this->createParcel(1);
|
2015-09-04 01:10:19 +02:00
|
|
|
|
2017-11-27 06:03:18 +01:00
|
|
|
$clone = clone $original;
|
2015-09-04 01:10:19 +02:00
|
|
|
|
2017-11-27 06:03:18 +01:00
|
|
|
$this->assertSame(2, yield $clone->synchronized(function () {
|
|
|
|
return 2;
|
|
|
|
}));
|
2015-09-04 01:10:19 +02:00
|
|
|
|
2017-11-27 06:03:18 +01:00
|
|
|
$this->assertSame(1, yield $original->unwrap());
|
|
|
|
$this->assertSame(2, $clone->unwrap());
|
|
|
|
});
|
2015-09-04 01:10:19 +02:00
|
|
|
}
|
2015-08-28 23:58:15 +02:00
|
|
|
}
|