Skip to content

Commit cc36aa0

Browse files
committed
Small timeout values will be capped at 1µs
1 parent 20214a7 commit cc36aa0

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ The `sleep($seconds, LoopInterface $loop)` method can be used to wait/sleep for
9999
Block\sleep(1.5, $loop);
100100
```
101101

102+
The $time value will be used as a timer for the loop so that it keeps running
103+
until the timeout triggers.
104+
This implies that if you pass a really small (or negative) value, it will still
105+
start a timer and will thus trigger at the earliest possible time in the future.
106+
102107
While this may look similar to PHP's [`sleep()`](http://php.net/sleep) function,
103108
it's actual way more powerful:
104109
Instead of making the whole process sleep and handing over control to your operating system,
@@ -135,6 +140,8 @@ potentially wait/block forever until the promise is settled.
135140

136141
If a $timeout is given and the promise is still pending once the timeout
137142
triggers, this will `cancel()` the promise and throw a `TimeoutException`.
143+
This implies that if you pass a really small (or negative) value, it will still
144+
start a timer and will thus trigger at the earliest possible time in the future.
138145

139146
#### awaitAny()
140147

@@ -162,6 +169,8 @@ potentially wait/block forever until the last promise is settled.
162169

163170
If a $timeout is given and either promise is still pending once the timeout
164171
triggers, this will `cancel()` all pending promises and throw a `TimeoutException`.
172+
This implies that if you pass a really small (or negative) value, it will still
173+
start a timer and will thus trigger at the earliest possible time in the future.
165174

166175
#### awaitAll()
167176

@@ -191,6 +200,8 @@ potentially wait/block forever until the last promise is settled.
191200

192201
If a $timeout is given and either promise is still pending once the timeout
193202
triggers, this will `cancel()` all pending promises and throw a `TimeoutException`.
203+
This implies that if you pass a really small (or negative) value, it will still
204+
start a timer and will thus trigger at the earliest possible time in the future.
194205

195206
## Install
196207

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"require": {
1717
"php": ">=5.3",
18-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
18+
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
1919
"react/promise": "~2.1|~1.2",
2020
"react/promise-timer": "~1.0"
2121
},

src/functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
/**
1515
* wait/sleep for $time seconds
1616
*
17+
* The $time value will be used as a timer for the loop so that it keeps running
18+
* until the timeout triggers.
19+
* This implies that if you pass a really small (or negative) value, it will still
20+
* start a timer and will thus trigger at the earliest possible time in the future.
21+
*
1722
* @param float $time
1823
* @param LoopInterface $loop
1924
*/
@@ -34,6 +39,8 @@ function sleep($time, LoopInterface $loop)
3439
*
3540
* If a $timeout is given and the promise is still pending once the timeout
3641
* triggers, this will cancel() the promise and throw a `TimeoutException`.
42+
* This implies that if you pass a really small (or negative) value, it will still
43+
* start a timer and will thus trigger at the earliest possible time in the future.
3744
*
3845
* @param PromiseInterface $promise
3946
* @param LoopInterface $loop
@@ -89,6 +96,8 @@ function ($error) use (&$exception, &$wait, $loop) {
8996
*
9097
* If a $timeout is given and either promise is still pending once the timeout
9198
* triggers, this will cancel() all pending promises and throw a `TimeoutException`.
99+
* This implies that if you pass a really small (or negative) value, it will still
100+
* start a timer and will thus trigger at the earliest possible time in the future.
92101
*
93102
* @param array $promises
94103
* @param LoopInterface $loop
@@ -145,6 +154,8 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
145154
*
146155
* If a $timeout is given and either promise is still pending once the timeout
147156
* triggers, this will cancel() all pending promises and throw a `TimeoutException`.
157+
* This implies that if you pass a really small (or negative) value, it will still
158+
* start a timer and will thus trigger at the earliest possible time in the future.
148159
*
149160
* @param array $promises
150161
* @param LoopInterface $loop

tests/FunctionSleepTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,13 @@ public function testSleep()
1212

1313
$this->assertEquals(0.2, $time, '', 0.1);
1414
}
15+
16+
public function testSleepSmallTimerWillBeCappedReasonably()
17+
{
18+
$time = microtime(true);
19+
Block\sleep(0.0000001, $this->loop);
20+
$time = microtime(true) - $time;
21+
22+
$this->assertEquals(0.1, $time, '', 0.1);
23+
}
1524
}

0 commit comments

Comments
 (0)