mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
misc + testing
This commit is contained in:
parent
2cea028b1c
commit
79a33c09fe
@ -48,6 +48,34 @@ class EvReactor implements ExtensionReactor {
|
||||
}
|
||||
}
|
||||
|
||||
public function __debugInfo() {
|
||||
$timers = $readers = $writers = $signals = 0;
|
||||
foreach ($this->enabledWatchers as $evHandle) {
|
||||
switch (get_class($evHandle)) {
|
||||
case "EvTimer":
|
||||
$timers++;
|
||||
break;
|
||||
case "EvIo":
|
||||
if ($evHandle->events === Ev::READ) { $readers++; } else { $writers++; }
|
||||
break;
|
||||
case "EvSignal":
|
||||
$signals++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
"immediates" => count($this->enabledImmediates),
|
||||
"timers" => $timers,
|
||||
"io_readers" => $readers,
|
||||
"io_writers" => $writers,
|
||||
"signals" => $signals,
|
||||
"disabled" => count($this->disabledWatchers) + count($this->disabledImmediates),
|
||||
"last_watcher_id" => $this->lastWatcherId,
|
||||
"instances" => self::$instanceCount,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -306,8 +334,6 @@ class EvReactor implements ExtensionReactor {
|
||||
} elseif (isset($this->disabledImmediates[$watcherId])) {
|
||||
$watcher = $this->disabledImmediates[$watcherId];
|
||||
unset($this->disabledImmediates[$watcherId]);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -451,6 +451,11 @@ class LibeventReactor implements ExtensionReactor {
|
||||
public function __debugInfo() {
|
||||
$immediates = $timers = $readers = $writers = $signals = $disabled = 0;
|
||||
foreach ($this->watchers as $watcher) {
|
||||
if (!$watcher->isEnabled) {
|
||||
$disabled++;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($watcher->type) {
|
||||
case Watcher::IMMEDIATE:
|
||||
$immediates++;
|
||||
@ -473,20 +478,17 @@ class LibeventReactor implements ExtensionReactor {
|
||||
"Unexpected watcher type: {$watcher->type}"
|
||||
);
|
||||
}
|
||||
|
||||
$disabled += !$watcher->isEnabled;
|
||||
}
|
||||
|
||||
return [
|
||||
'enabled' => $this->enabledWatcherCount,
|
||||
'timers' => $timers,
|
||||
'immediates' => $immediates,
|
||||
'io_readers' => $readers,
|
||||
'io_writers' => $writers,
|
||||
'signals' => $signals,
|
||||
'disabled' => $disabled,
|
||||
'last_watcher_id' => $this->lastWatcherId,
|
||||
'instances' => self::$instanceCount,
|
||||
"immediates" => $immediates,
|
||||
"timers" => $timers,
|
||||
"io_readers" => $readers,
|
||||
"io_writers" => $writers,
|
||||
"signals" => $signals,
|
||||
"disabled" => $disabled,
|
||||
"last_watcher_id" => $this->lastWatcherId,
|
||||
"instances" => self::$instanceCount,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -674,14 +674,14 @@ class UvReactor implements ExtensionReactor {
|
||||
}
|
||||
|
||||
return [
|
||||
'timers' => $timers,
|
||||
'immediates' => $immediates,
|
||||
'io_readers' => $readers,
|
||||
'io_writers' => $writers,
|
||||
'signals' => $signals,
|
||||
'disabled' => $disabled,
|
||||
'last_watcher_id' => $this->lastWatcherId,
|
||||
'instances' => self::$instanceCount,
|
||||
"immediates" => $immediates,
|
||||
"timers" => $timers,
|
||||
"io_readers" => $readers,
|
||||
"io_writers" => $writers,
|
||||
"signals" => $signals,
|
||||
"disabled" => $disabled,
|
||||
"last_watcher_id" => $this->lastWatcherId,
|
||||
"instances" => self::$instanceCount,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,148 @@ namespace Amp\Test;
|
||||
abstract class ReactorTest extends \PHPUnit_Framework_TestCase {
|
||||
abstract protected function getReactor();
|
||||
|
||||
public function testMultipleCallsToRunHaveNoEffect() {
|
||||
$reactor = $this->getReactor();
|
||||
$reactor->run(function($reactor) {
|
||||
$reactor->run();
|
||||
});
|
||||
}
|
||||
|
||||
public function testImmediatelyWatcherRegistrationAndCancellation() {
|
||||
$reactor = $this->getReactor();
|
||||
|
||||
$watcherId = $reactor->immediately(function () {});
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["immediates"]);
|
||||
|
||||
$reactor->disable($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["immediates"]);
|
||||
$this->assertSame(1, $info["disabled"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["immediates"]);
|
||||
|
||||
$watcherId = $reactor->immediately(function () {});
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["immediates"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["immediates"]);
|
||||
}
|
||||
|
||||
public function testOnceWatcherRegistrationAndCancellation() {
|
||||
$reactor = $this->getReactor();
|
||||
|
||||
$watcherId = $reactor->once(function () {}, 1000);
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["timers"]);
|
||||
|
||||
$reactor->disable($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["timers"]);
|
||||
$this->assertSame(1, $info["disabled"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["timers"]);
|
||||
|
||||
$watcherId = $reactor->once(function () {}, 1000);
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["timers"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["timers"]);
|
||||
}
|
||||
|
||||
public function testRepeatWatcherRegistrationAndCancellation() {
|
||||
$reactor = $this->getReactor();
|
||||
|
||||
$watcherId = $reactor->repeat(function () {}, 1000);
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["timers"]);
|
||||
|
||||
$reactor->disable($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["timers"]);
|
||||
$this->assertSame(1, $info["disabled"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["timers"]);
|
||||
|
||||
$watcherId = $reactor->repeat(function () {}, 1000);
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["timers"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["timers"]);
|
||||
}
|
||||
|
||||
public function testOnWritableWatcherRegistrationAndCancellation() {
|
||||
$reactor = $this->getReactor();
|
||||
|
||||
$watcherId = $reactor->onWritable(STDIN, function () {});
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["io_writers"]);
|
||||
|
||||
$reactor->disable($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["io_writers"]);
|
||||
$this->assertSame(1, $info["disabled"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["io_writers"]);
|
||||
|
||||
$watcherId = $reactor->onWritable(STDIN, function () {});
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["io_writers"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["io_writers"]);
|
||||
}
|
||||
|
||||
public function testOnReadableWatcherRegistrationAndCancellation() {
|
||||
$reactor = $this->getReactor();
|
||||
|
||||
$watcherId = $reactor->onReadable(STDIN, function () {});
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["io_readers"]);
|
||||
|
||||
$reactor->disable($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["io_readers"]);
|
||||
$this->assertSame(1, $info["disabled"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["io_readers"]);
|
||||
|
||||
$watcherId = $reactor->onReadable(STDIN, function () {});
|
||||
$this->assertInternalType("string", $watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(1, $info["io_readers"]);
|
||||
|
||||
$reactor->cancel($watcherId);
|
||||
$info = $reactor->__debugInfo();
|
||||
$this->assertSame(0, $info["io_readers"]);
|
||||
}
|
||||
|
||||
public function testEnablingWatcherAllowsSubsequentInvocation() {
|
||||
$reactor = $this->getReactor();
|
||||
$increment = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user