Skip to content

Commit 9e2310b

Browse files
committed
Merge pull request #1 from clue-labs/run
Run() the loop instead of making it tick()
2 parents f9012b5 + 2faa08d commit 9e2310b

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

src/Blocker.php

Lines changed: 37 additions & 12 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,20 +45,26 @@ public function wait($time)
4045
*/
4146
public function awaitOne(PromiseInterface $promise)
4247
{
48+
$wait = true;
4349
$resolved = null;
4450
$exception = null;
51+
$loop = $this->loop;
4552

4653
$promise->then(
47-
function ($c) use (&$resolved) {
54+
function ($c) use (&$resolved, &$wait, $loop) {
4855
$resolved = $c;
56+
$wait = false;
57+
$loop->stop();
4958
},
50-
function ($error) use (&$exception) {
59+
function ($error) use (&$exception, &$wait, $loop) {
5160
$exception = $error;
61+
$wait = false;
62+
$loop->stop();
5263
}
5364
);
5465

55-
while ($resolved === null && $exception === null) {
56-
$this->loop->tick();
66+
while ($wait) {
67+
$loop->run();
5768
}
5869

5970
if ($exception !== null) {
@@ -80,11 +91,12 @@ public function awaitRace(array $promises)
8091
$wait = count($promises);
8192
$value = null;
8293
$success = false;
94+
$loop = $this->loop;
8395

8496
foreach ($promises as $key => $promise) {
8597
/* @var $promise PromiseInterface */
8698
$promise->then(
87-
function ($return) use (&$value, &$wait, &$success, $promises) {
99+
function ($return) use (&$value, &$wait, &$success, $promises, $loop) {
88100
if (!$wait) {
89101
// only store first promise value
90102
return;
@@ -99,19 +111,25 @@ function ($return) use (&$value, &$wait, &$success, $promises) {
99111
$promise->cancel();
100112
}
101113
}
114+
115+
$loop->stop();
102116
},
103-
function ($e) use (&$wait) {
117+
function ($e) use (&$wait, $loop) {
104118
if ($wait) {
105119
// count number of promises to await
106120
// cancelling promises will reject all remaining ones, ignore this
107121
--$wait;
122+
123+
if (!$wait) {
124+
$loop->stop();
125+
}
108126
}
109127
}
110128
);
111129
}
112130

113131
while ($wait) {
114-
$this->loop->tick();
132+
$loop->run();
115133
}
116134

117135
if (!$success) {
@@ -140,15 +158,20 @@ public function awaitAll(array $promises)
140158
$wait = count($promises);
141159
$exception = null;
142160
$values = array();
161+
$loop = $this->loop;
143162

144163
foreach ($promises as $key => $promise) {
145164
/* @var $promise PromiseInterface */
146165
$promise->then(
147-
function ($value) use (&$values, $key, &$wait) {
166+
function ($value) use (&$values, $key, &$wait, $loop) {
148167
$values[$key] = $value;
149168
--$wait;
169+
170+
if (!$wait) {
171+
$loop->stop();
172+
}
150173
},
151-
function ($e) use ($promises, &$exception, &$wait) {
174+
function ($e) use ($promises, &$exception, &$wait, $loop) {
152175
if (!$wait) {
153176
// cancelling promises will reject all remaining ones, only store first error
154177
return;
@@ -163,12 +186,14 @@ function ($e) use ($promises, &$exception, &$wait) {
163186
$promise->cancel();
164187
}
165188
}
189+
190+
$loop->stop();
166191
}
167192
);
168193
}
169194

170195
while ($wait) {
171-
$this->loop->tick();
196+
$loop->run();
172197
}
173198

174199
if ($exception !== null) {

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)