1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00

Fix 212 and ignore unreference on invalid watchers (#216)

Fix #212 & Ignore unreference on invalid watchers
This commit is contained in:
Aaron Piotrowski 2018-04-12 02:46:52 -05:00 committed by Bob Weinand
parent 9b2fb76442
commit 242d78a6ec
3 changed files with 24 additions and 13 deletions

View File

@ -247,6 +247,12 @@ final class Loop {
* @return void
*/
public static function disable(string $watcherId) {
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
// See https://github.com/amphp/amp/issues/212.
return;
}
self::$driver->disable($watcherId);
}
@ -261,6 +267,12 @@ final class Loop {
* @return void
*/
public static function cancel(string $watcherId) {
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
// See https://github.com/amphp/amp/issues/212.
return;
}
self::$driver->cancel($watcherId);
}
@ -289,10 +301,14 @@ final class Loop {
* @param string $watcherId The watcher identifier.
*
* @return void
*
* @throws InvalidWatcherError If the watcher identifier is invalid.
*/
public static function unreference(string $watcherId) {
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
// See https://github.com/amphp/amp/issues/212.
return;
}
self::$driver->unreference($watcherId);
}

View File

@ -487,12 +487,10 @@ abstract class Driver {
* @param string $watcherId The watcher identifier.
*
* @return void
*
* @throws InvalidWatcherError If the watcher identifier is invalid.
*/
public function unreference(string $watcherId) {
if (!isset($this->watchers[$watcherId])) {
throw new InvalidWatcherError($watcherId, "Cannot unreference an invalid watcher identifier: '{$watcherId}'");
return;
}
$this->watchers[$watcherId]->referenced = false;

View File

@ -754,14 +754,11 @@ abstract class DriverTest extends TestCase {
}
}
/** @expectedException \Amp\Loop\InvalidWatcherError */
public function testExceptionOnUnreferenceNonexistentWatcher() {
try {
$this->loop->unreference("nonexistentWatcher");
} catch (InvalidWatcherError $e) {
$this->assertSame("nonexistentWatcher", $e->getWatcherId());
throw $e;
}
public function testSuccessOnUnreferenceNonexistentWatcher() {
$this->loop->unreference("nonexistentWatcher");
// Otherwise risky, throwing fails the test
$this->assertTrue(true);
}
/** @expectedException \Amp\Loop\InvalidWatcherError */