1
0
mirror of https://github.com/danog/amp.git synced 2024-12-13 09:57:25 +01:00
amp/test/CoroutineTest.php

705 lines
17 KiB
PHP
Raw Normal View History

<?php
2016-08-17 06:27:10 +02:00
namespace Amp\Test;
2016-07-12 18:20:06 +02:00
use Amp\Coroutine;
use Amp\Delayed;
use Amp\Failure;
use Amp\Loop;
2020-09-28 05:19:52 +02:00
use Amp\PHPUnit\AsyncTestCase;
2017-05-03 15:21:49 +02:00
use Amp\PHPUnit\TestException;
use Amp\Promise;
2017-04-23 14:39:19 +02:00
use Amp\Success;
2020-09-28 05:19:52 +02:00
use function Amp\await;
2019-03-14 22:31:27 +01:00
use function Amp\call;
use function Amp\delay;
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
class CoroutineTest extends AsyncTestCase
2018-06-18 20:00:01 +02:00
{
const TIMEOUT = 100;
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
public function testYieldSuccessfulPromise(): void
2018-06-18 20:00:01 +02:00
{
2016-07-12 18:20:06 +02:00
$value = 1;
$generator = function () use (&$yielded, $value) {
$yielded = yield new Success($value);
2016-07-12 18:20:06 +02:00
};
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
2016-07-12 18:20:06 +02:00
$this->assertSame($value, $yielded);
}
2020-09-28 05:19:52 +02:00
public function testYieldFailedPromise(): void
2018-06-18 20:00:01 +02:00
{
2016-07-12 18:20:06 +02:00
$exception = new \Exception;
$generator = function () use (&$yielded, $exception) {
$yielded = yield new Failure($exception);
2016-07-12 18:20:06 +02:00
};
$coroutine = new Coroutine($generator());
delay(1); // Force loop to tick once.
2020-09-28 05:19:52 +02:00
2016-07-12 18:20:06 +02:00
$this->assertNull($yielded);
$coroutine->onResolve(function ($exception) use (&$reason) {
2016-07-12 18:20:06 +02:00
$reason = $exception;
});
delay(1); // Force loop to tick once.
2020-09-28 05:19:52 +02:00
2016-07-12 18:20:06 +02:00
$this->assertSame($exception, $reason);
}
/**
2016-11-14 20:59:21 +01:00
* @depends testYieldSuccessfulPromise
2016-07-12 18:20:06 +02:00
*/
2020-09-28 05:19:52 +02:00
public function testYieldPendingPromise(): void
2018-06-18 20:00:01 +02:00
{
2016-07-12 18:20:06 +02:00
$value = 1;
2020-09-28 05:19:52 +02:00
$generator = function () use (&$yielded, $value) {
$yielded = yield new Delayed(self::TIMEOUT, $value);
};
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
2016-07-12 18:20:06 +02:00
$this->assertSame($value, $yielded);
}
2020-09-28 05:19:52 +02:00
public function testYieldPromiseArray(): void
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$value = 1;
2020-09-28 05:19:52 +02:00
$generator = function () use (&$yielded, $value) {
list($yielded) = yield [
new Success($value),
];
};
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
2020-09-28 05:19:52 +02:00
$this->assertSame($value, $yielded);
}
2018-06-18 20:00:01 +02:00
public function testYieldNonPromiseArray()
{
2020-09-28 05:19:52 +02:00
$this->expectException(\TypeError::class);
2020-09-28 05:19:52 +02:00
$value = 1;
2020-09-28 05:19:52 +02:00
$generator = function () use (&$yielded, $value) {
list($yielded) = yield [
$value,
];
};
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
}
2018-06-18 20:00:01 +02:00
public function testYieldPromiseArrayAfterPendingPromise()
{
2020-09-28 05:19:52 +02:00
$value = 1;
2017-03-14 22:32:14 +01:00
2020-09-28 05:19:52 +02:00
$generator = function () use (&$yielded, $value) {
yield new Delayed(10);
list($yielded) = yield [
new Success($value),
];
};
2017-03-14 22:32:14 +01:00
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
2017-03-14 22:32:14 +01:00
2020-09-28 05:19:52 +02:00
$this->assertSame($value, $yielded);
2017-03-14 22:32:14 +01:00
}
2018-06-18 20:00:01 +02:00
public function testYieldNonPromiseArrayAfterPendingPromise()
{
2020-09-28 05:19:52 +02:00
$this->expectException(\TypeError::class);
2017-03-14 22:32:14 +01:00
2020-09-28 05:19:52 +02:00
$value = 1;
2017-03-14 22:32:14 +01:00
2020-09-28 05:19:52 +02:00
$generator = function () use (&$yielded, $value) {
yield new Delayed(10);
list($yielded) = yield [
$value,
];
};
2017-03-14 22:32:14 +01:00
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
2017-03-14 22:32:14 +01:00
}
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;
};
2020-09-28 05:19:52 +02:00
await(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
}
2018-06-18 20:00:01 +02:00
public function testInvalidYield()
{
2020-09-28 05:19:52 +02:00
$this->expectException(\TypeError::class);
2016-07-12 18:20:06 +02:00
$generator = function () {
yield 1;
};
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
2016-07-12 18:20:06 +02:00
}
/**
* @depends testInvalidYield
*/
2018-06-18 20:00:01 +02:00
public function testInvalidYieldAfterYieldPromise()
{
2020-09-28 05:19:52 +02:00
$this->expectException(\TypeError::class);
2016-07-12 18:20:06 +02:00
$generator = function () {
yield new Success;
yield 1;
};
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
2016-07-12 18:20:06 +02:00
}
/**
* @depends testInvalidYield
*/
2018-06-18 20:00:01 +02:00
public function testInvalidYieldCatchingThrownError()
{
$value = 42;
$generator = function () use ($value) {
try {
yield 1;
} catch (\Error $error) {
// No further yields.
}
return $value;
};
2020-09-28 05:19:52 +02:00
$result = await(new Coroutine($generator()));
$this->assertSame($result, $value);
}
/**
* @depends testInvalidYieldCatchingThrownError
*/
2018-06-18 20:00:01 +02:00
public function testInvalidYieldCatchingThrownErrorAndYieldingAgain()
{
$value = 42;
$generator = function () use ($value) {
try {
yield 1;
} catch (\Error $error) {
return yield new Success($value);
}
};
2020-09-28 05:19:52 +02:00
$result = await(new Coroutine($generator()));
$this->assertSame($result, $value);
}
/**
* @depends testInvalidYieldCatchingThrownError
*/
2018-06-18 20:00:01 +02:00
public function testInvalidYieldCatchingThrownErrorAndThrowing()
{
$exception = new \Exception;
$generator = function () use ($exception) {
try {
yield 1;
} catch (\Error $error) {
throw $exception;
}
};
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
return;
}
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
}
/**
* @depends testInvalidYieldCatchingThrownError
*/
2018-06-18 20:00:01 +02:00
public function testInvalidYieldWithThrowingFinallyBlock()
{
$exception = new \Exception;
$generator = function () use ($exception) {
try {
yield 1;
} finally {
throw $exception;
}
};
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
$this->assertInstanceOf(\TypeError::class, $reason->getPrevious());
return;
}
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
}
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.
}
};
2020-09-28 05:19:52 +02:00
$result = await(new Coroutine($generator()));
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;
};
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
return;
}
2016-07-12 18:20:06 +02:00
}
/**
* @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;
}
};
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
$this->assertTrue($invoked);
return;
}
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
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
* @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;
}
};
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
$this->assertTrue($invoked);
return;
}
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
2016-07-12 18:20:06 +02:00
}
/**
* @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;
2020-09-28 05:19:52 +02:00
$invoked = false;
$generator = function () use (&$yielded, &$invoked, $exception, $value) {
try {
$yielded = (yield new Delayed(self::TIMEOUT, $value));
throw $exception;
} finally {
$invoked = true;
}
};
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
$this->assertTrue($invoked);
$this->assertSame($value, $yielded);
return;
}
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
2016-07-12 18:20:06 +02:00
}
/**
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;
2020-09-28 05:19:52 +02:00
$generator = function () use (&$yielded, $exception, $value) {
try {
throw $exception;
} finally {
$yielded = yield new Delayed(self::TIMEOUT, $value);
}
};
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
$this->assertSame($value, $yielded);
return;
}
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
2016-07-12 18:20:06 +02:00
}
/**
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.
};
2020-09-28 05:19:52 +02:00
try {
await(new Coroutine($generator()));
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
return;
}
2016-07-12 18:20:06 +02:00
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
2016-07-12 18:20:06 +02:00
}
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;
}
};
});
};
try {
2020-09-28 05:19:52 +02:00
await(new Coroutine($generator()));
} catch (\Throwable $e) {
$this->fail("Caught exception that shouldn't be thrown at that place.");
}
2020-09-28 05:19:52 +02:00
Loop::setErrorHandler(function (\Throwable $exception) use (&$reason): void {
$reason = $exception;
});
delay(0); // Tick event loop to invoke error callback.
2020-09-28 05:19:52 +02:00
$this->assertSame($exception, $reason);
2019-03-14 22:31:27 +01:00
}
/**
* @depends testYieldSuccessfulPromise
*/
2018-06-18 20:00:01 +02:00
public function testYieldConsecutiveSucceeded()
{
2020-09-28 05:19:52 +02:00
$count = 1000;
$promise = new Success;
2020-09-28 05:19:52 +02:00
$generator = function () use ($count, $promise) {
for ($i = 0; $i < $count; ++$i) {
yield $promise;
}
return $count;
};
2020-09-28 05:19:52 +02:00
$this->assertSame($count, await(new Coroutine($generator())));
}
/**
* @depends testYieldFailedPromise
*/
2018-06-18 20:00:01 +02:00
public function testYieldConsecutiveFailed()
{
2020-09-28 05:19:52 +02:00
$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.
}
2020-09-28 05:19:52 +02:00
}
2020-09-28 05:19:52 +02:00
return $count;
};
2020-09-28 05:19:52 +02:00
$this->assertSame($count, await(new Coroutine($generator())));
}
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;
}
};
2020-09-28 05:19:52 +02:00
$this->assertNull(await(new Coroutine($generator())));
2016-07-12 18:20:06 +02:00
}
2018-06-18 20:00:01 +02:00
public function testCoroutineFunction()
{
$callable = \Amp\coroutine(function () {
2016-07-12 18:20:06 +02:00
yield;
});
$this->assertInstanceOf(Coroutine::class, $callable());
}
2016-08-12 23:56:03 +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);
$callable = \Amp\coroutine(function ($value) {
2016-08-12 23:56:03 +02:00
return $value;
});
/** @var Promise $promise */
2016-11-14 20:59:21 +01:00
$promise = $callable($promise);
2016-11-14 20:59:21 +01:00
$this->assertInstanceOf(Promise::class, $promise);
2020-09-28 05:19:52 +02:00
$this->assertSame($value, await($promise));
2016-08-12 23:56:03 +02:00
}
2016-07-12 18:20: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;
$callable = \Amp\coroutine(function ($value) {
2016-08-12 23:56:03 +02:00
return $value;
});
/** @var Promise $promise */
2016-11-14 20:59:21 +01:00
$promise = $callable($value);
2016-11-14 20:59:21 +01:00
$this->assertInstanceOf(Promise::class, $promise);
2020-09-28 05:19:52 +02:00
$this->assertSame($value, await($promise));
2016-08-12 23:56:03 +02:00
}
2016-08-12 23:56:03 +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;
$callable = \Amp\coroutine(function () use ($exception) {
2016-08-12 23:56:03 +02:00
throw $exception;
});
/** @var Promise $promise */
2016-11-14 20:59:21 +01:00
$promise = $callable();
2016-11-14 20:59:21 +01:00
$this->assertInstanceOf(Promise::class, $promise);
2020-09-28 05:19:52 +02:00
try {
await($promise);
} catch (\Throwable $reason) {
$this->assertSame($exception, $reason);
return;
}
2020-09-28 05:19:52 +02:00
$this->fail("Coroutine should have failed");
}
/**
* @depends testCoroutineFunction
*/
2018-06-18 20:00:01 +02:00
public function testCoroutineFunctionWithSuccessReturnCallback()
{
$callable = \Amp\coroutine(function () {
return new Success(42);
});
/** @var Promise $promise */
$promise = $callable();
$this->assertInstanceOf(Promise::class, $promise);
2020-09-28 05:19:52 +02:00
$this->assertSame(42, await($promise));
2016-07-12 18:20:06 +02:00
}
2018-06-18 20:00:01 +02:00
public function testCoroutineResolvedWithReturn()
{
2016-08-11 21:35:58 +02:00
$value = 1;
2016-08-11 21:35:58 +02:00
$generator = function () use ($value) {
return $value;
yield; // Unreachable, but makes function a coroutine.
};
2020-09-28 05:19:52 +02:00
$this->assertSame($value, await(new Coroutine($generator())));
2016-08-11 21:35:58 +02: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;
2016-08-11 21:35:58 +02:00
$generator = function () use ($value) {
$generator = function () use ($value) {
return yield new Success($value);
};
2016-08-11 21:35:58 +02:00
return yield from $generator();
};
2020-09-28 05:19:52 +02:00
$this->assertSame($value, await(new Coroutine($generator())));
2016-08-11 21:35:58 +02: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;
2016-08-11 21:35:58 +02:00
$generator = function () use ($value) {
if (true) {
return $value;
}
2016-08-11 21:35:58 +02:00
yield;
2016-08-11 21:35:58 +02:00
return -$value;
};
2020-09-28 05:19:52 +02:00
$this->assertSame($value, await(new Coroutine($generator())));
2016-08-11 21:35:58 +02:00
}
2017-02-20 21:53:58 +01: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);
2020-09-28 05:19:52 +02:00
Loop::setErrorHandler(function (\Throwable $exception) use (&$reason): void {
$reason = $exception;
});
delay(0); // Tick event loop to invoke error callback.
2017-05-03 15:21:49 +02:00
2020-09-28 05:19:52 +02:00
$this->assertInstanceOf(TestException::class, $reason);
2017-05-03 15:21:49 +02:00
}
2016-07-12 18:20:06 +02:00
}