Skip to content

Commit 2faa08d

Browse files
committed
Keep the loop running if anything interrupts its operation
1 parent c6dc8b4 commit 2faa08d

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/Blocker.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ public function __construct(LoopInterface $loop)
2424
*/
2525
public function wait($time)
2626
{
27+
$wait = true;
2728
$loop = $this->loop;
28-
$loop->addTimer($time, function () use ($loop) {
29+
$loop->addTimer($time, function () use ($loop, &$wait) {
2930
$loop->stop();
31+
$wait = false;
3032
});
31-
$loop->run();
33+
34+
do {
35+
$loop->run();
36+
} while($wait);
3237
}
3338

3439
/**
@@ -40,22 +45,27 @@ public function wait($time)
4045
*/
4146
public function awaitOne(PromiseInterface $promise)
4247
{
48+
$wait = true;
4349
$resolved = null;
4450
$exception = null;
4551
$loop = $this->loop;
4652

4753
$promise->then(
48-
function ($c) use (&$resolved, $loop) {
54+
function ($c) use (&$resolved, &$wait, $loop) {
4955
$resolved = $c;
56+
$wait = false;
5057
$loop->stop();
5158
},
52-
function ($error) use (&$exception, $loop) {
59+
function ($error) use (&$exception, &$wait, $loop) {
5360
$exception = $error;
61+
$wait = false;
5462
$loop->stop();
5563
}
5664
);
5765

58-
$loop->run();
66+
while ($wait) {
67+
$loop->run();
68+
}
5969

6070
if ($exception !== null) {
6171
throw $exception;
@@ -118,7 +128,9 @@ function ($e) use (&$wait, $loop) {
118128
);
119129
}
120130

121-
$loop->run();
131+
while ($wait) {
132+
$loop->run();
133+
}
122134

123135
if (!$success) {
124136
throw new UnderflowException('No promise could resolve');
@@ -180,7 +192,9 @@ function ($e) use ($promises, &$exception, &$wait, $loop) {
180192
);
181193
}
182194

183-
$loop->run();
195+
while ($wait) {
196+
$loop->run();
197+
}
184198

185199
if ($exception !== null) {
186200
throw $exception;

tests/BlockerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function testAwaitOneResolved()
3838
$this->assertEquals(2, $this->block->awaitOne($promise));
3939
}
4040

41+
public function testAwaitOneInterrupted()
42+
{
43+
$promise = $this->createPromiseResolved(2, 0.02);
44+
$this->createTimerInterrupt(0.01);
45+
46+
$this->assertEquals(2, $this->block->awaitOne($promise));
47+
}
48+
4149
/**
4250
* @expectedException UnderflowException
4351
*/
@@ -89,6 +97,14 @@ public function testAwaitRaceAllRejected()
8997
$this->block->awaitRace($all);
9098
}
9199

100+
public function testAwaitRaceInterrupted()
101+
{
102+
$promise = $this->createPromiseResolved(2, 0.02);
103+
$this->createTimerInterrupt(0.01);
104+
105+
$this->assertEquals(2, $this->block->awaitRace(array($promise)));
106+
}
107+
92108
public function testAwaitAllEmpty()
93109
{
94110
$this->assertEquals(array(), $this->block->awaitAll(array()));
@@ -126,6 +142,14 @@ public function testAwaitAllOnlyRejected()
126142
$this->block->awaitAll($all);
127143
}
128144

145+
public function testAwaitAllInterrupted()
146+
{
147+
$promise = $this->createPromiseResolved(2, 0.02);
148+
$this->createTimerInterrupt(0.01);
149+
150+
$this->assertEquals(array(2), $this->block->awaitAll(array($promise)));
151+
}
152+
129153
private function createPromiseResolved($value = null, $delay = 0.01)
130154
{
131155
$deferred = new Deferred();
@@ -147,4 +171,12 @@ private function createPromiseRejected($value = null, $delay = 0.01)
147171

148172
return $deferred->promise();
149173
}
174+
175+
private function createTimerInterrupt($delay = 0.01)
176+
{
177+
$loop = $this->loop;
178+
$loop->addTimer($delay, function () use ($loop) {
179+
$loop->stop();
180+
});
181+
}
150182
}

0 commit comments

Comments
 (0)