1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00

Fix issues with interval = 0 repeat watchers, fixes #131

This commit is contained in:
Niklas Keller 2017-05-23 19:46:23 +02:00
parent dbc853c3f1
commit 38ef4d9ffa
3 changed files with 35 additions and 0 deletions

View File

@ -61,6 +61,13 @@ class EvDriver extends Driver {
$this->cancel($watcher->id);
}
if ($watcher->type & Watcher::REPEAT && $watcher->value === 0) {
// Disable and re-enable so it's not executed repeatedly in the same tick
// See https://github.com/amphp/amp/issues/131
$this->disable($watcher->id);
$this->enable($watcher->id);
}
try {
$result = ($watcher->callback)($watcher->id, $watcher->data);

View File

@ -90,6 +90,13 @@ class UvDriver extends Driver {
$this->cancel($watcher->id); // Remove reference to watcher in parent.
}
if ($watcher->type & Watcher::REPEAT && $watcher->value === 0) {
// Disable and re-enable so it's not executed repeatedly in the same tick
// See https://github.com/amphp/amp/issues/131
$this->disable($watcher->id);
$this->enable($watcher->id);
}
try {
$result = ($watcher->callback)($watcher->id, $watcher->data);

View File

@ -1452,4 +1452,25 @@ abstract class DriverTest extends TestCase {
$this->loop->run();
}
public function testTwoShortRepeatTimersWorkAsExpected() {
$this->loop->repeat(0, function () use (&$j) {
static $i = 0;
if (++$i === 5) {
$this->loop->stop();
}
$j = $i;
});
$this->loop->repeat(0, function () use (&$k) {
static $i = 0;
if (++$i === 5) {
$this->loop->stop();
}
$k = $i;
});
$this->loop->run();
$this->assertLessThan(2, \abs($j - $k));
$this->assertNotSame(0, $j);
}
}