forked from reactphp/async
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request reactphp#71 from clue-labs/delay-v3
[3.x] Add new `delay()` function to delay program execution
- Loading branch information
Showing
3 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace React\Tests\Async; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use React\EventLoop\Loop; | ||
use function React\Async\delay; | ||
|
||
class DelayTest extends TestCase | ||
{ | ||
public function testDelayBlocksForGivenPeriod() | ||
{ | ||
$time = microtime(true); | ||
delay(0.02); | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertEqualsWithDelta(0.02, $time, 0.01); | ||
} | ||
|
||
public function testDelaySmallPeriodBlocksForCloseToZeroSeconds() | ||
{ | ||
$time = microtime(true); | ||
delay(0.000001); | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertLessThan(0.01, $time); | ||
} | ||
|
||
public function testDelayNegativePeriodBlocksForCloseToZeroSeconds() | ||
{ | ||
$time = microtime(true); | ||
delay(-1); | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertLessThan(0.01, $time); | ||
} | ||
|
||
public function testDelayRunsOtherEventsWhileWaiting() | ||
{ | ||
$buffer = 'a'; | ||
Loop::addTimer(0.001, function () use (&$buffer) { | ||
$buffer .= 'c'; | ||
}); | ||
$buffer .= 'b'; | ||
delay(0.002); | ||
$buffer .= 'd'; | ||
|
||
$this->assertEquals('abcd', $buffer); | ||
} | ||
} |