Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward compatibility with future EventLoop v1.0 and v0.5 and cap small timeout values for legacy EventLoop #26

Merged
merged 2 commits into from
Aug 3, 2017
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ The `sleep($seconds, LoopInterface $loop)` method can be used to wait/sleep for
Block\sleep(1.5, $loop);
```

The $time value will be used as a timer for the loop so that it keeps running
until the timeout triggers.
This implies that if you pass a really small (or negative) value, it will still
start a timer and will thus trigger at the earliest possible time in the future.

While this may look similar to PHP's [`sleep()`](http://php.net/sleep) function,
it's actual way more powerful:
Instead of making the whole process sleep and handing over control to your operating system,
Expand Down Expand Up @@ -135,6 +140,8 @@ potentially wait/block forever until the promise is settled.

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

#### awaitAny()

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

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

#### awaitAll()

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

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

## Install

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require": {
"php": ">=5.3",
"react/event-loop": "0.4.*|0.3.*",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/promise": "~2.1|~1.2",
"react/promise-timer": "~1.0"
},
Expand Down
11 changes: 11 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
/**
* wait/sleep for $time seconds
*
* The $time value will be used as a timer for the loop so that it keeps running
* until the timeout triggers.
* This implies that if you pass a really small (or negative) value, it will still
* start a timer and will thus trigger at the earliest possible time in the future.
*
* @param float $time
* @param LoopInterface $loop
*/
Expand All @@ -34,6 +39,8 @@ function sleep($time, LoopInterface $loop)
*
* If a $timeout is given and the promise is still pending once the timeout
* triggers, this will cancel() the promise and throw a `TimeoutException`.
* This implies that if you pass a really small (or negative) value, it will still
* start a timer and will thus trigger at the earliest possible time in the future.
*
* @param PromiseInterface $promise
* @param LoopInterface $loop
Expand Down Expand Up @@ -89,6 +96,8 @@ function ($error) use (&$exception, &$wait, $loop) {
*
* If a $timeout is given and either promise is still pending once the timeout
* triggers, this will cancel() all pending promises and throw a `TimeoutException`.
* This implies that if you pass a really small (or negative) value, it will still
* start a timer and will thus trigger at the earliest possible time in the future.
*
* @param array $promises
* @param LoopInterface $loop
Expand Down Expand Up @@ -145,6 +154,8 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
*
* If a $timeout is given and either promise is still pending once the timeout
* triggers, this will cancel() all pending promises and throw a `TimeoutException`.
* This implies that if you pass a really small (or negative) value, it will still
* start a timer and will thus trigger at the earliest possible time in the future.
*
* @param array $promises
* @param LoopInterface $loop
Expand Down
9 changes: 9 additions & 0 deletions tests/FunctionSleepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ public function testSleep()

$this->assertEquals(0.2, $time, '', 0.1);
}

public function testSleepSmallTimerWillBeCappedReasonably()
{
$time = microtime(true);
Block\sleep(0.0000001, $this->loop);
$time = microtime(true) - $time;

$this->assertEquals(0.1, $time, '', 0.1);
}
}