mirror of
https://github.com/danog/amp.git
synced 2024-12-04 18:38:17 +01:00
Provide useful exception trace in TimeoutCancellationToken (#303)
Without this, the exception trace is pretty useless, because it only includes Loop::run() and other internal loop calls, giving absolutely no indication which kind of thing had a timeout. Use debug_backtrace instead of creating the exception early, because it helps with the changes to GC behavior such a change might introduce. Co-authored-by: Aaron Piotrowski <aaron@trowski.com>
This commit is contained in:
parent
8c486b40a8
commit
feca077369
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Amp;
|
namespace Amp;
|
||||||
|
|
||||||
|
use function Amp\Internal\formatStacktrace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
|
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
|
||||||
*/
|
*/
|
||||||
@ -10,11 +12,11 @@ final class TimeoutCancellationToken implements CancellationToken
|
|||||||
/** @var string */
|
/** @var string */
|
||||||
private $watcher;
|
private $watcher;
|
||||||
|
|
||||||
/** @var \Amp\CancellationToken */
|
/** @var CancellationToken */
|
||||||
private $token;
|
private $token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $timeout Milliseconds until cancellation is requested.
|
* @param int $timeout Milliseconds until cancellation is requested.
|
||||||
* @param string $message Message for TimeoutException. Default is "Operation timed out".
|
* @param string $message Message for TimeoutException. Default is "Operation timed out".
|
||||||
*/
|
*/
|
||||||
public function __construct(int $timeout, string $message = "Operation timed out")
|
public function __construct(int $timeout, string $message = "Operation timed out")
|
||||||
@ -22,9 +24,12 @@ final class TimeoutCancellationToken implements CancellationToken
|
|||||||
$source = new CancellationTokenSource;
|
$source = new CancellationTokenSource;
|
||||||
$this->token = $source->getToken();
|
$this->token = $source->getToken();
|
||||||
|
|
||||||
$this->watcher = Loop::delay($timeout, static function () use ($source, $message) {
|
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||||
$source->cancel(new TimeoutException($message));
|
$this->watcher = Loop::delay($timeout, static function () use ($source, $message, $trace) {
|
||||||
|
$trace = formatStacktrace($trace);
|
||||||
|
$source->cancel(new TimeoutException("$message\r\nTimeoutCancellationToken was created here:\r\n$trace"));
|
||||||
});
|
});
|
||||||
|
|
||||||
Loop::unreference($this->watcher);
|
Loop::unreference($this->watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,26 +3,31 @@
|
|||||||
namespace Amp\Test;
|
namespace Amp\Test;
|
||||||
|
|
||||||
use Amp\CancelledException;
|
use Amp\CancelledException;
|
||||||
use Amp\Delayed;
|
|
||||||
use Amp\Loop;
|
use Amp\Loop;
|
||||||
use Amp\TimeoutCancellationToken;
|
use Amp\TimeoutCancellationToken;
|
||||||
use Amp\TimeoutException;
|
use Amp\TimeoutException;
|
||||||
|
use function Amp\delay;
|
||||||
|
|
||||||
class TimeoutCancellationTokenTest extends BaseTest
|
class TimeoutCancellationTokenTest extends BaseTest
|
||||||
{
|
{
|
||||||
public function testTimeout()
|
public function testTimeout()
|
||||||
{
|
{
|
||||||
Loop::run(function () {
|
Loop::run(function () {
|
||||||
|
$line = __LINE__ + 1;
|
||||||
$token = new TimeoutCancellationToken(10);
|
$token = new TimeoutCancellationToken(10);
|
||||||
|
|
||||||
$this->assertFalse($token->isRequested());
|
$this->assertFalse($token->isRequested());
|
||||||
yield new Delayed(20);
|
yield delay(20);
|
||||||
$this->assertTrue($token->isRequested());
|
$this->assertTrue($token->isRequested());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$token->throwIfRequested();
|
$token->throwIfRequested();
|
||||||
} catch (CancelledException $exception) {
|
} catch (CancelledException $exception) {
|
||||||
$this->assertInstanceOf(TimeoutException::class, $exception->getPrevious());
|
$this->assertInstanceOf(TimeoutException::class, $exception->getPrevious());
|
||||||
|
|
||||||
|
$message = $exception->getPrevious()->getMessage();
|
||||||
|
$this->assertContains('TimeoutCancellationToken was created here', $message);
|
||||||
|
$this->assertContains('TimeoutCancellationTokenTest.php:' . $line, $message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user