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
|
|
|
|
|
|
|
use Amp;
|
2016-11-14 20:59:21 +01:00
|
|
|
use Amp\{ Coroutine, Failure, InvalidYieldError, Pause, Success };
|
2017-01-07 13:47:45 +01:00
|
|
|
use AsyncInterop\{ Loop, Promise };
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
class CoroutineTest extends \PHPUnit_Framework_TestCase {
|
2016-08-16 23:39:25 +02:00
|
|
|
const TIMEOUT = 100;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testYieldSuccessfulPromise() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $value) {
|
|
|
|
$yielded = (yield new Success($value));
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testYieldFailedPromise() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use (&$yielded, $exception) {
|
|
|
|
$yielded = (yield new Failure($exception));
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$this->assertNull($yielded);
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testYieldPendingPromise() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
Loop::execute(function () use (&$yielded, $value) {
|
|
|
|
$generator = function () use (&$yielded, $value) {
|
|
|
|
$yielded = (yield new Pause(self::TIMEOUT, $value));
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldFailedPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2016-11-14 20:59:21 +01: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
if ($fail) {
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->fail("Failed promise reason not thrown into generator");
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
public function testInvalidYield() {
|
|
|
|
$generator = function () {
|
|
|
|
yield 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$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
|
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testInvalidYieldAfterYieldPromise() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$generator = function () {
|
|
|
|
yield new Success;
|
|
|
|
yield 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$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
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testInvalidYield
|
|
|
|
*/
|
|
|
|
public function testInvalidYieldCatchingThrownException() {
|
|
|
|
$generator = function () {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} catch (\Error $exception) {
|
|
|
|
// No further yields.
|
|
|
|
}
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$coroutine = new Coroutine($generator());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$this->assertInstanceOf(InvalidYieldError::class, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testInvalidYieldCatchingThrownException
|
|
|
|
*/
|
|
|
|
public function testInvalidYieldCatchingThrownExceptionAndYieldingAgain() {
|
|
|
|
$generator = function () {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} catch (\Error $exception) {
|
|
|
|
yield new Success;
|
|
|
|
}
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$coroutine = new Coroutine($generator());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$this->assertInstanceOf(InvalidYieldError::class, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testInvalidYieldCatchingThrownException
|
|
|
|
*/
|
|
|
|
public function testInvalidYieldCatchingThrownExceptionAndThrowing() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} catch (\Error $error) {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$coroutine = new Coroutine($generator());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$this->assertInstanceOf(InvalidYieldError::class, $reason);
|
|
|
|
$this->assertSame($exception, $reason->getPrevious());
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInvalidYield
|
|
|
|
*/
|
|
|
|
public function testCatchesExceptionAfterInvalidYield() {
|
|
|
|
$generator = function () {
|
|
|
|
try {
|
|
|
|
yield 1;
|
|
|
|
} catch (\Exception $exception) {
|
2016-08-11 21:35:58 +02:00
|
|
|
return 1;
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$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
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
|
|
|
* @depends testInvalidYield
|
|
|
|
*/
|
|
|
|
public function testThrowAfterInvalidYield() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
try {
|
|
|
|
yield 1;
|
2016-08-11 21:35:58 +02:00
|
|
|
} catch (\Throwable $reason) {
|
2016-07-12 18:20:06 +02:00
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$this->assertInstanceOf(InvalidYieldError::class, $reason);
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertSame($exception, $reason->getPrevious());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldFailedPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2016-11-14 20:59:21 +01: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());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGeneratorThrowingExceptionFailsCoroutine() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$generator = function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
yield;
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGeneratorThrowingExceptionFailsCoroutine
|
|
|
|
*/
|
|
|
|
public function testGeneratorThrowingExceptionWithFinallyFailsCoroutine() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = false;
|
|
|
|
$generator = function () use (&$invoked, $exception) {
|
|
|
|
try {
|
|
|
|
throw $exception;
|
|
|
|
yield;
|
|
|
|
} finally {
|
|
|
|
$invoked = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$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
|
|
|
|
*/
|
2016-11-14 20:59:21 +01: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());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGeneratorThrowingExceptionFailsCoroutine
|
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testGeneratorThrowingExceptionAfterPendingPromiseWithFinallyFailsCoroutine() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
Loop::execute(function () use (&$yielded, &$invoked, &$reason, $exception, $value) {
|
|
|
|
$invoked = false;
|
|
|
|
$generator = function () use (&$yielded, &$invoked, $exception, $value) {
|
|
|
|
try {
|
|
|
|
$yielded = (yield new Pause(self::TIMEOUT, $value));
|
|
|
|
throw $exception;
|
|
|
|
} finally {
|
|
|
|
$invoked = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value, $yielded);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note that yielding in a finally block is not recommended.
|
|
|
|
*
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldPendingPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
* @depends testGeneratorThrowingExceptionWithFinallyFailsCoroutine
|
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testGeneratorThrowingExceptionWithFinallyYieldingPendingPromise() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
Loop::execute(function () use (&$yielded, &$reason, $exception, $value) {
|
|
|
|
$generator = function () use (&$yielded, $exception, $value) {
|
|
|
|
try {
|
|
|
|
throw $exception;
|
|
|
|
} finally {
|
2016-12-17 15:16:17 +01:00
|
|
|
$yielded = yield new Pause(self::TIMEOUT, $value);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testGeneratorThrowingExceptionWithFinallyBlockThrowing() {
|
|
|
|
$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());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
|
|
|
public function testYieldConsecutiveSucceeded() {
|
|
|
|
$invoked = false;
|
|
|
|
Loop::execute(function () use (&$invoked) {
|
|
|
|
$count = 1000;
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Success;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$generator = function () use ($count, $promise) {
|
2016-07-12 18:20:06 +02:00
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
2016-11-14 20:59:21 +01:00
|
|
|
yield $promise;
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldFailedPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
|
|
|
public function testYieldConsecutiveFailed() {
|
|
|
|
$invoked = false;
|
|
|
|
Loop::execute(function () use (&$invoked) {
|
|
|
|
$count = 1000;
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Failure(new \Exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$generator = function () use ($count, $promise) {
|
2016-07-12 18:20:06 +02:00
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
|
|
|
try {
|
2016-11-14 20:59:21 +01:00
|
|
|
yield $promise;
|
2016-07-12 18:20:06 +02:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
// Ignore and continue.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testYieldSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
|
|
|
public function testFastInvalidGenerator() {
|
|
|
|
$generator = function () {
|
|
|
|
if (false) {
|
|
|
|
yield new Success;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator());
|
|
|
|
|
|
|
|
$invoked = false;
|
|
|
|
$coroutine->when(function ($exception, $value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCoroutineFunction() {
|
|
|
|
$callable = Amp\coroutine(function () {
|
|
|
|
yield;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Coroutine::class, $callable());
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-12 23:56:03 +02:00
|
|
|
/**
|
|
|
|
* @depends testCoroutineFunction
|
|
|
|
*/
|
2016-11-14 20:59:21 +01: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);
|
2016-08-12 23:56:03 +02:00
|
|
|
$callable = Amp\coroutine(function ($value) {
|
|
|
|
return $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
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
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->when(function ($exception, $value) use (&$result) {
|
2016-08-12 23:56:03 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @depends testCoroutineFunction
|
|
|
|
*/
|
|
|
|
public function testCoroutineFunctionWithNonGeneratorCallback() {
|
2016-08-12 23:56:03 +02:00
|
|
|
$value = 1;
|
|
|
|
$callable = Amp\coroutine(function ($value) {
|
|
|
|
return $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
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
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->when(function ($exception, $value) use (&$result) {
|
2016-08-12 23:56:03 +02:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @depends testCoroutineFunction
|
|
|
|
*/
|
|
|
|
public function testCoroutineFunctionWithThrowingCallback() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
$callable = Amp\coroutine(function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
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
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->when(function ($exception, $value) use (&$reason) {
|
2016-08-12 23:56:03 +02:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-12 23:56:03 +02:00
|
|
|
$this->assertSame($exception, $reason);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
public function testCoroutineResolvedWithReturn() {
|
|
|
|
$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
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$coroutine->when(function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function testYieldFromGenerator() {
|
|
|
|
$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
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$coroutine->when(function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function testFastReturningGenerator()
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$coroutine->when(function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-08-11 21:35:58 +02:00
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|