Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/Blocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public function __construct(LoopInterface $loop)
*/
public function wait($time)
{
$wait = true;
$loop = $this->loop;
$loop->addTimer($time, function () use ($loop) {
$loop->addTimer($time, function () use ($loop, &$wait) {
$loop->stop();
$wait = false;
});
$loop->run();

do {
$loop->run();
} while($wait);
}

/**
Expand All @@ -40,20 +45,26 @@ public function wait($time)
*/
public function awaitOne(PromiseInterface $promise)
{
$wait = true;
$resolved = null;
$exception = null;
$loop = $this->loop;

$promise->then(
function ($c) use (&$resolved) {
function ($c) use (&$resolved, &$wait, $loop) {
$resolved = $c;
$wait = false;
$loop->stop();
},
function ($error) use (&$exception) {
function ($error) use (&$exception, &$wait, $loop) {
$exception = $error;
$wait = false;
$loop->stop();
}
);

while ($resolved === null && $exception === null) {
$this->loop->tick();
while ($wait) {
$loop->run();
}

if ($exception !== null) {
Expand All @@ -80,11 +91,12 @@ public function awaitRace(array $promises)
$wait = count($promises);
$value = null;
$success = false;
$loop = $this->loop;

foreach ($promises as $key => $promise) {
/* @var $promise PromiseInterface */
$promise->then(
function ($return) use (&$value, &$wait, &$success, $promises) {
function ($return) use (&$value, &$wait, &$success, $promises, $loop) {
if (!$wait) {
// only store first promise value
return;
Expand All @@ -99,19 +111,25 @@ function ($return) use (&$value, &$wait, &$success, $promises) {
$promise->cancel();
}
}

$loop->stop();
},
function ($e) use (&$wait) {
function ($e) use (&$wait, $loop) {
if ($wait) {
// count number of promises to await
// cancelling promises will reject all remaining ones, ignore this
--$wait;

if (!$wait) {
$loop->stop();
}
}
}
);
}

while ($wait) {
$this->loop->tick();
$loop->run();
}

if (!$success) {
Expand Down Expand Up @@ -140,15 +158,20 @@ public function awaitAll(array $promises)
$wait = count($promises);
$exception = null;
$values = array();
$loop = $this->loop;

foreach ($promises as $key => $promise) {
/* @var $promise PromiseInterface */
$promise->then(
function ($value) use (&$values, $key, &$wait) {
function ($value) use (&$values, $key, &$wait, $loop) {
$values[$key] = $value;
--$wait;

if (!$wait) {
$loop->stop();
}
},
function ($e) use ($promises, &$exception, &$wait) {
function ($e) use ($promises, &$exception, &$wait, $loop) {
if (!$wait) {
// cancelling promises will reject all remaining ones, only store first error
return;
Expand All @@ -163,12 +186,14 @@ function ($e) use ($promises, &$exception, &$wait) {
$promise->cancel();
}
}

$loop->stop();
}
);
}

while ($wait) {
$this->loop->tick();
$loop->run();
}

if ($exception !== null) {
Expand Down
32 changes: 32 additions & 0 deletions tests/BlockerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function testAwaitOneResolved()
$this->assertEquals(2, $this->block->awaitOne($promise));
}

public function testAwaitOneInterrupted()
{
$promise = $this->createPromiseResolved(2, 0.02);
$this->createTimerInterrupt(0.01);

$this->assertEquals(2, $this->block->awaitOne($promise));
}

/**
* @expectedException UnderflowException
*/
Expand Down Expand Up @@ -89,6 +97,14 @@ public function testAwaitRaceAllRejected()
$this->block->awaitRace($all);
}

public function testAwaitRaceInterrupted()
{
$promise = $this->createPromiseResolved(2, 0.02);
$this->createTimerInterrupt(0.01);

$this->assertEquals(2, $this->block->awaitRace(array($promise)));
}

public function testAwaitAllEmpty()
{
$this->assertEquals(array(), $this->block->awaitAll(array()));
Expand Down Expand Up @@ -126,6 +142,14 @@ public function testAwaitAllOnlyRejected()
$this->block->awaitAll($all);
}

public function testAwaitAllInterrupted()
{
$promise = $this->createPromiseResolved(2, 0.02);
$this->createTimerInterrupt(0.01);

$this->assertEquals(array(2), $this->block->awaitAll(array($promise)));
}

private function createPromiseResolved($value = null, $delay = 0.01)
{
$deferred = new Deferred();
Expand All @@ -147,4 +171,12 @@ private function createPromiseRejected($value = null, $delay = 0.01)

return $deferred->promise();
}

private function createTimerInterrupt($delay = 0.01)
{
$loop = $this->loop;
$loop->addTimer($delay, function () use ($loop) {
$loop->stop();
});
}
}