2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-08-17 06:27:10 +02:00
|
|
|
namespace Amp\Test;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Coroutine;
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\InvalidYieldError;
|
|
|
|
use Amp\Loop;
|
2017-05-03 15:21:49 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Success;
|
2017-03-14 22:15:36 +01:00
|
|
|
use React\Promise\FulfilledPromise as FulfilledReactPromise;
|
2017-02-20 21:53:58 +01:00
|
|
|
use React\Promise\Promise as ReactPromise;
|
2019-03-14 22:31:27 +01:00
|
|
|
use function Amp\call;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
class CoroutineTest extends BaseTest
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-08-16 23:39:25 +02:00
|
|
|
const TIMEOUT = 100;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldSuccessfulPromise()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $value) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$yielded = yield new Success($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
new Coroutine($generator());
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldFailedPromise()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $exception) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$yielded = yield new Failure($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$this->assertNull($yielded);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldPendingPromise()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$yielded, $value) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$generator = function () use (&$yielded, $value) {
|
2017-04-28 14:42:02 +02:00
|
|
|
$yielded = yield new Delayed(self::TIMEOUT, $value);
|
2016-07-12 18:20:06 +02:00
|
|
|
};
|
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
new Coroutine($generator());
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldPromiseArray()
|
|
|
|
{
|
2017-03-12 19:38:36 +01:00
|
|
|
Loop::run(function () {
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $value) {
|
|
|
|
list($yielded) = yield [
|
2018-06-18 20:00:01 +02:00
|
|
|
new Success($value),
|
2017-03-12 19:38:36 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
yield new Coroutine($generator());
|
|
|
|
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldNonPromiseArray()
|
|
|
|
{
|
2017-03-12 19:38:36 +01:00
|
|
|
$this->expectException(InvalidYieldError::class);
|
|
|
|
|
|
|
|
Loop::run(function () {
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $value) {
|
|
|
|
list($yielded) = yield [
|
2018-06-18 20:00:01 +02:00
|
|
|
$value,
|
2017-03-12 19:38:36 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
yield new Coroutine($generator());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldPromiseArrayAfterPendingPromise()
|
|
|
|
{
|
2017-03-14 22:32:14 +01:00
|
|
|
Loop::run(function () {
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $value) {
|
2017-04-28 14:42:02 +02:00
|
|
|
yield new Delayed(10);
|
2017-03-14 22:32:14 +01:00
|
|
|
list($yielded) = yield [
|
2018-06-18 20:00:01 +02:00
|
|
|
new Success($value),
|
2017-03-14 22:32:14 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
yield new Coroutine($generator());
|
|
|
|
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldNonPromiseArrayAfterPendingPromise()
|
|
|
|
{
|
2017-03-14 22:32:14 +01:00
|
|
|
$this->expectException(InvalidYieldError::class);
|
|
|
|
|
|
|
|
Loop::run(function () {
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $value) {
|
2017-04-28 14:42:02 +02:00
|
|
|
yield new Delayed(10);
|
2017-03-14 22:32:14 +01:00
|
|
|
list($yielded) = yield [
|
2018-06-18 20:00:01 +02:00
|
|
|
$value,
|
2017-03-14 22:32:14 +01:00
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
yield new Coroutine($generator());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldFailedPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCatchingFailedPromiseException()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$fail = false;
|
|
|
|
$generator = function () use (&$fail, &$result, $exception) {
|
|
|
|
try {
|
|
|
|
yield new Failure($exception);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$result = $exception;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fail = true;
|
|
|
|
};
|
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
new Coroutine($generator());
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-14 00:52:57 +01:00
|
|
|
$this->assertFalse($fail);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInvalidYield()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$generator = function () {
|
|
|
|
yield 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$this->assertInstanceOf(InvalidYieldError::class, $reason);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInvalidYield
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInvalidYieldAfterYieldPromise()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$generator = function () {
|
|
|
|
yield new Success;
|
|
|
|
yield 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$this->assertInstanceOf(InvalidYieldError::class, $reason);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-24 20:05:53 +02:00
|
|
|
/**
|
|
|
|
* @depends testInvalidYield
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInvalidYieldCatchingThrownError()
|
|
|
|
{
|
2017-04-24 20:05:53 +02:00
|
|
|
$value = 42;
|
|
|
|
$generator = function () use ($value) {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} catch (\Error $error) {
|
|
|
|
// No further yields.
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($result, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInvalidYieldCatchingThrownError
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInvalidYieldCatchingThrownErrorAndYieldingAgain()
|
|
|
|
{
|
2017-04-24 20:05:53 +02:00
|
|
|
$value = 42;
|
|
|
|
$generator = function () use ($value) {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} catch (\Error $error) {
|
|
|
|
return yield new Success($value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($result, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInvalidYieldCatchingThrownError
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInvalidYieldCatchingThrownErrorAndThrowing()
|
|
|
|
{
|
2017-04-24 20:05:53 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} catch (\Error $error) {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInvalidYieldCatchingThrownError
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInvalidYieldWithThrowingFinallyBlock()
|
|
|
|
{
|
2017-04-24 20:05:53 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} finally {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
$this->assertInstanceOf(InvalidYieldError::class, $reason->getPrevious());
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldFailedPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCatchingFailedPromiseExceptionWithNoFurtherYields()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
try {
|
|
|
|
yield new Failure($exception);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
// No further yields in generator.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$reason = $exception;
|
2016-07-12 18:20:06 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
$this->assertNull($reason);
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGeneratorThrowingExceptionFailsCoroutine()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
yield;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGeneratorThrowingExceptionFailsCoroutine
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGeneratorThrowingExceptionWithFinallyFailsCoroutine()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = false;
|
|
|
|
$generator = function () use (&$invoked, $exception) {
|
|
|
|
try {
|
|
|
|
throw $exception;
|
|
|
|
yield;
|
|
|
|
} finally {
|
|
|
|
$invoked = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldFailedPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
* @depends testGeneratorThrowingExceptionWithFinallyFailsCoroutine
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGeneratorYieldingFailedPromiseWithFinallyFailsCoroutine()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = false;
|
|
|
|
$generator = function () use (&$invoked, $exception) {
|
|
|
|
try {
|
|
|
|
yield new Failure($exception);
|
|
|
|
} finally {
|
|
|
|
$invoked = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGeneratorThrowingExceptionFailsCoroutine
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGeneratorThrowingExceptionAfterPendingPromiseWithFinallyFailsCoroutine()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
$value = 1;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$yielded, &$invoked, &$reason, $exception, $value) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$invoked = false;
|
|
|
|
$generator = function () use (&$yielded, &$invoked, $exception, $value) {
|
|
|
|
try {
|
2017-04-28 14:42:02 +02:00
|
|
|
$yielded = (yield new Delayed(self::TIMEOUT, $value));
|
2016-07-12 18:20:06 +02:00
|
|
|
throw $exception;
|
|
|
|
} finally {
|
|
|
|
$invoked = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldPendingPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
* @depends testGeneratorThrowingExceptionWithFinallyFailsCoroutine
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGeneratorThrowingExceptionWithFinallyYieldingPendingPromise()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
$value = 1;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$yielded, &$reason, $exception, $value) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$generator = function () use (&$yielded, $exception, $value) {
|
|
|
|
try {
|
|
|
|
throw $exception;
|
|
|
|
} finally {
|
2017-04-28 14:42:02 +02:00
|
|
|
$yielded = yield new Delayed(self::TIMEOUT, $value);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldPendingPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
* @depends testGeneratorThrowingExceptionWithFinallyFailsCoroutine
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGeneratorThrowingExceptionWithFinallyBlockThrowing()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
try {
|
|
|
|
throw new \Exception;
|
|
|
|
} finally {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield; // Unreachable, but makes function a generator.
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception) use (&$reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:31:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testGeneratorThrowingExceptionWithFinallyFailsCoroutine
|
|
|
|
*/
|
|
|
|
public function testGeneratorThrowingExceptionWithFinallyBlockAndReturnThrowing()
|
|
|
|
{
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
yield new Success;
|
|
|
|
|
|
|
|
return call(function () use ($exception) {
|
2019-05-14 21:44:44 +02:00
|
|
|
return new class($exception) {
|
2019-03-14 22:31:27 +01:00
|
|
|
private $exception;
|
|
|
|
|
|
|
|
public function __construct(\Throwable $exception)
|
|
|
|
{
|
|
|
|
$this->exception = $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
throw $this->exception;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
try {
|
|
|
|
new Coroutine($generator());
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$this->fail("Caught exception that shouldn't be thrown at that place.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Promise\wait(new Delayed(0)); // make loop tick once to throw errors from loop
|
|
|
|
} catch (\Error $e) {
|
|
|
|
if ($e->getMessage() === "Loop exceptionally stopped without resolving the promise") {
|
|
|
|
throw $e->getPrevious();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2019-03-14 22:31:27 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:40:56 +02:00
|
|
|
/**
|
|
|
|
* @depends testYieldSuccessfulPromise
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldConsecutiveSucceeded()
|
|
|
|
{
|
2017-05-20 09:40:56 +02:00
|
|
|
$invoked = false;
|
|
|
|
Loop::run(function () use (&$invoked) {
|
|
|
|
$count = 1000;
|
|
|
|
$promise = new Success;
|
|
|
|
|
|
|
|
$generator = function () use ($count, $promise) {
|
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
|
|
|
yield $promise;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->onResolve(function () use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testYieldFailedPromise
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldConsecutiveFailed()
|
|
|
|
{
|
2017-05-20 09:40:56 +02:00
|
|
|
$invoked = false;
|
|
|
|
Loop::run(function () use (&$invoked) {
|
|
|
|
$count = 1000;
|
|
|
|
$promise = new Failure(new \Exception);
|
|
|
|
|
|
|
|
$generator = function () use ($count, $promise) {
|
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
|
|
|
try {
|
|
|
|
yield $promise;
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
// Ignore and continue.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->onResolve(function () use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testFastInvalidGenerator()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$generator = function () {
|
|
|
|
if (false) {
|
|
|
|
yield new Success;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$invoked = false;
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function () use (&$invoked) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCoroutineFunction()
|
|
|
|
{
|
2017-04-23 20:07:06 +02:00
|
|
|
$callable = \Amp\coroutine(function () {
|
2016-07-12 18:20:06 +02:00
|
|
|
yield;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Coroutine::class, $callable());
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-12 23:56:03 +02:00
|
|
|
/**
|
2017-04-23 20:07:06 +02:00
|
|
|
* @depends testCoroutineFunction
|
2016-08-12 23:56:03 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCoroutineFunctionWithCallbackReturningPromise()
|
|
|
|
{
|
2016-08-12 23:56:03 +02:00
|
|
|
$value = 1;
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Success($value);
|
2017-04-23 20:07:06 +02:00
|
|
|
$callable = \Amp\coroutine(function ($value) {
|
2016-08-12 23:56:03 +02:00
|
|
|
return $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
/** @var Promise $promise */
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $callable($promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$reason = $exception;
|
2016-08-12 23:56:03 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
$this->assertNull($reason);
|
2016-08-12 23:56:03 +02:00
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2017-04-23 20:07:06 +02:00
|
|
|
* @depends testCoroutineFunction
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCoroutineFunctionWithNonGeneratorCallback()
|
|
|
|
{
|
2016-08-12 23:56:03 +02:00
|
|
|
$value = 1;
|
2017-04-23 20:07:06 +02:00
|
|
|
$callable = \Amp\coroutine(function ($value) {
|
2016-08-12 23:56:03 +02:00
|
|
|
return $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
/** @var Promise $promise */
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $callable($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$reason = $exception;
|
2016-08-12 23:56:03 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
$this->assertNull($reason);
|
2016-08-12 23:56:03 +02:00
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-12 23:56:03 +02:00
|
|
|
/**
|
2017-04-23 20:07:06 +02:00
|
|
|
* @depends testCoroutineFunction
|
2016-08-12 23:56:03 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCoroutineFunctionWithThrowingCallback()
|
|
|
|
{
|
2016-08-12 23:56:03 +02:00
|
|
|
$exception = new \Exception;
|
2017-04-23 20:07:06 +02:00
|
|
|
$callable = \Amp\coroutine(function () use ($exception) {
|
2016-08-12 23:56:03 +02:00
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
/** @var Promise $promise */
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $callable();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2016-08-12 23:56:03 +02:00
|
|
|
$reason = $exception;
|
2017-01-07 22:55:34 +01:00
|
|
|
$result = $value;
|
2016-08-12 23:56:03 +02:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-12 23:56:03 +02:00
|
|
|
$this->assertSame($exception, $reason);
|
2017-01-07 22:55:34 +01:00
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-23 20:07:06 +02:00
|
|
|
* @depends testCoroutineFunction
|
2017-01-07 22:55:34 +01:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCoroutineFunctionWithSuccessReturnCallback()
|
|
|
|
{
|
2017-04-23 20:07:06 +02:00
|
|
|
$callable = \Amp\coroutine(function () {
|
2017-01-07 22:55:34 +01:00
|
|
|
return new Success(42);
|
|
|
|
});
|
|
|
|
|
|
|
|
/** @var Promise $promise */
|
|
|
|
$promise = $callable();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame(42, $result);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCoroutineFunctionWithReactPromise()
|
|
|
|
{
|
2017-04-23 20:07:06 +02:00
|
|
|
$callable = \Amp\coroutine(function () {
|
2017-03-14 22:15:36 +01:00
|
|
|
return new FulfilledReactPromise(42);
|
|
|
|
});
|
|
|
|
|
|
|
|
/** @var Promise $promise */
|
|
|
|
$promise = $callable();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-03-14 22:15:36 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame(42, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCoroutineResolvedWithReturn()
|
|
|
|
{
|
2016-08-11 21:35:58 +02:00
|
|
|
$value = 1;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$generator = function () use ($value) {
|
|
|
|
return $value;
|
|
|
|
yield; // Unreachable, but makes function a coroutine.
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$coroutine = new Coroutine($generator());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$reason = $exception;
|
2016-08-11 21:35:58 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
$this->assertNull($reason);
|
2016-08-11 21:35:58 +02:00
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
/**
|
|
|
|
* @depends testCoroutineResolvedWithReturn
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldFromGenerator()
|
|
|
|
{
|
2016-08-11 21:35:58 +02:00
|
|
|
$value = 1;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$generator = function () use ($value) {
|
|
|
|
$generator = function () use ($value) {
|
|
|
|
return yield new Success($value);
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
return yield from $generator();
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$coroutine = new Coroutine($generator());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$reason = $exception;
|
2016-08-11 21:35:58 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
$this->assertNull($reason);
|
2016-08-11 21:35:58 +02:00
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
/**
|
|
|
|
* @depends testCoroutineResolvedWithReturn
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testFastReturningGenerator()
|
|
|
|
{
|
2016-08-11 21:35:58 +02:00
|
|
|
$value = 1;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$generator = function () use ($value) {
|
|
|
|
if (true) {
|
|
|
|
return $value;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
yield;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
return -$value;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$coroutine = new Coroutine($generator());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-01-07 22:55:34 +01:00
|
|
|
$reason = $exception;
|
2016-08-11 21:35:58 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-07 22:55:34 +01:00
|
|
|
$this->assertNull($reason);
|
2016-08-11 21:35:58 +02:00
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-02-20 21:53:58 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldingFulfilledReactPromise()
|
|
|
|
{
|
2017-02-20 21:53:58 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = new ReactPromise(function ($resolve, $reject) use ($value) {
|
|
|
|
$resolve($value);
|
|
|
|
});
|
|
|
|
|
|
|
|
$generator = function () use ($promise) {
|
|
|
|
return yield $promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-20 21:53:58 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldingFulfilledReactPromiseAfterInteropPromise()
|
|
|
|
{
|
2017-02-20 21:53:58 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = new ReactPromise(function ($resolve, $reject) use ($value) {
|
|
|
|
$resolve($value);
|
|
|
|
});
|
|
|
|
|
|
|
|
$generator = function () use ($promise) {
|
|
|
|
$value = yield new Success(-1);
|
|
|
|
return yield $promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-20 21:53:58 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldingRejectedReactPromise()
|
|
|
|
{
|
2017-02-20 21:53:58 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new ReactPromise(function ($resolve, $reject) use ($exception) {
|
|
|
|
$reject($exception);
|
|
|
|
});
|
|
|
|
|
|
|
|
$generator = function () use ($promise) {
|
|
|
|
return yield $promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-20 21:53:58 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($reason, $exception);
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testYieldingRejectedReactPromiseAfterInteropPromise()
|
|
|
|
{
|
2017-02-20 21:53:58 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new ReactPromise(function ($resolve, $reject) use ($exception) {
|
|
|
|
$reject($exception);
|
|
|
|
});
|
|
|
|
|
|
|
|
$generator = function () use ($promise) {
|
|
|
|
$value = yield new Success(-1);
|
|
|
|
return yield $promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-20 21:53:58 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($reason, $exception);
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testReturnFulfilledReactPromise()
|
|
|
|
{
|
2017-02-20 21:53:58 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = new ReactPromise(function ($resolve, $reject) use ($value) {
|
|
|
|
$resolve($value);
|
|
|
|
});
|
|
|
|
|
|
|
|
$generator = function () use ($promise) {
|
|
|
|
return $promise;
|
|
|
|
yield; // Unreachable, but makes function a generator.
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-20 21:53:58 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testReturningRejectedReactPromise()
|
|
|
|
{
|
2017-02-20 21:53:58 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new ReactPromise(function ($resolve, $reject) use ($exception) {
|
|
|
|
$reject($exception);
|
|
|
|
});
|
|
|
|
|
|
|
|
$generator = function () use ($promise) {
|
|
|
|
return $promise;
|
|
|
|
yield; // Unreachable, but makes function a generator.
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$coroutine->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-20 21:53:58 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($reason, $exception);
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
2017-05-03 15:21:49 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testAsyncCoroutineFunctionWithFailure()
|
|
|
|
{
|
2017-05-03 15:21:49 +02:00
|
|
|
$coroutine = \Amp\asyncCoroutine(function ($value) {
|
|
|
|
return new Failure(new TestException);
|
|
|
|
});
|
|
|
|
|
|
|
|
$coroutine(42);
|
|
|
|
|
|
|
|
$this->expectException(TestException::class);
|
|
|
|
|
|
|
|
Loop::run();
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|