2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\Internal\Placeholder;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2020-10-07 06:40:14 +02:00
|
|
|
use function Amp\delay;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class InternalPlaceholderTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
private Placeholder $placeholder;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function setUp(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder = new Placeholder;
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveOnSuccess()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $value;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveOnSuccess
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testMultipleOnResolvesOnSuccess()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $value;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveOnSuccess
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveAfterSuccess()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $value;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveAfterSuccess
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testMultipleOnResolveAfterSuccess()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $value;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveOnSuccess
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveThrowingForwardsToLoopHandlerOnSuccess()
|
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = 0;
|
|
|
|
$expected = new \Exception;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
|
|
|
|
$this->assertSame($expected, $exception);
|
|
|
|
++$invoked;
|
|
|
|
});
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$callback = function () use ($expected) {
|
|
|
|
throw $expected;
|
|
|
|
};
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->resolve($expected);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveAfterSuccess
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testOnResolveThrowingForwardsToLoopHandlerAfterSuccess(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = 0;
|
|
|
|
$expected = new \Exception;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
|
|
|
|
$this->assertSame($expected, $exception);
|
|
|
|
++$invoked;
|
|
|
|
});
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$callback = function () use ($expected) {
|
|
|
|
throw $expected;
|
|
|
|
};
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->resolve($expected);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveOnFail()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $exception;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveOnFail
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testMultipleOnResolvesOnFail()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $exception;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveOnFail
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveAfterFail()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $exception;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveAfterFail
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testMultipleOnResolvesAfterFail()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
$result = $exception;
|
2019-05-14 21:37:08 +02:00
|
|
|
++$invoked;
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveOnSuccess
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveThrowingForwardsToLoopHandlerOnFail()
|
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = 0;
|
|
|
|
$expected = new \Exception;
|
|
|
|
|
|
|
|
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
|
|
|
|
$this->assertSame($expected, $exception);
|
|
|
|
++$invoked;
|
|
|
|
});
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$callback = function () use ($expected) {
|
|
|
|
throw $expected;
|
|
|
|
};
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->fail(new \Exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolveOnSuccess
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveThrowingForwardsToLoopHandlerAfterFail()
|
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = 0;
|
|
|
|
$expected = new \Exception;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
|
|
|
|
$this->assertSame($expected, $exception);
|
|
|
|
++$invoked;
|
|
|
|
});
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$callback = function () use ($expected) {
|
|
|
|
throw $expected;
|
|
|
|
};
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->fail(new \Exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->onResolve($callback);
|
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testResolveWithPromiseBeforeOnResolve()
|
|
|
|
{
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $this->getMockBuilder(Promise::class)->getMock();
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->expects($this->once())
|
2017-03-21 17:23:37 +01:00
|
|
|
->method("onResolve")
|
2016-07-12 18:20:06 +02:00
|
|
|
->with($this->callback("is_callable"));
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->placeholder->resolve($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
$this->placeholder->onResolve(function () {
|
|
|
|
});
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testResolveWithPromiseAfterOnResolve()
|
|
|
|
{
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $this->getMockBuilder(Promise::class)->getMock();
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->expects($this->once())
|
2017-03-21 17:23:37 +01:00
|
|
|
->method("onResolve")
|
2016-07-12 18:20:06 +02:00
|
|
|
->with($this->callback("is_callable"));
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
$this->placeholder->onResolve(function () {
|
|
|
|
});
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->placeholder->resolve($promise);
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke callbacks.
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testDoubleResolve()
|
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->expectException(\Error::class);
|
|
|
|
$this->expectExceptionMessage("Promise has already been resolved");
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve();
|
|
|
|
$this->placeholder->resolve();
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2016-12-29 21:09:49 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testResolveAgainWithinOnResolveCallback()
|
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->placeholder->onResolve(function () {
|
2016-12-29 21:09:49 +01:00
|
|
|
$this->placeholder->resolve();
|
|
|
|
});
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$this->placeholder->resolve();
|
|
|
|
|
|
|
|
Loop::setErrorHandler(function (\Throwable $exception) use (&$reason): void {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke error callback.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$this->assertInstanceOf(\Error::class, $reason);
|
|
|
|
$this->assertStringContainsString("Promise has already been resolved", $reason->getMessage());
|
2016-12-29 21:09:49 +01:00
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|