-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
delay()
function to delay program execution
- Loading branch information
Showing
3 changed files
with
325 additions
and
9 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,91 @@ | ||
<?php | ||
|
||
namespace React\Tests\Async; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use React\EventLoop\Loop; | ||
use function React\Async\async; | ||
use function React\Async\await; | ||
use function React\Async\delay; | ||
|
||
class DelayTest extends TestCase | ||
{ | ||
public function testDelayBlocksForGivenPeriod() | ||
{ | ||
$time = microtime(true); | ||
delay(0.02); | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertGreaterThan(0.01, $time); | ||
$this->assertLessThan(0.03, $time); | ||
} | ||
|
||
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 testAwaitAsyncDelayBlocksForGivenPeriod() | ||
{ | ||
$promise = async(function () { | ||
delay(0.02); | ||
})(); | ||
|
||
$time = microtime(true); | ||
await($promise); | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertGreaterThan(0.01, $time); | ||
$this->assertLessThan(0.03, $time); | ||
} | ||
|
||
public function testAwaitAsyncDelayCancelledImmediatelyStopsTimerAndBlocksForCloseToZeroSeconds() | ||
{ | ||
$promise = async(function () { | ||
delay(1.0); | ||
})(); | ||
$promise->cancel(); | ||
|
||
$time = microtime(true); | ||
try { | ||
await($promise); | ||
} catch (\RuntimeException $e) { | ||
$this->assertEquals('Delay cancelled', $e->getMessage()); | ||
} | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertLessThan(0.03, $time); | ||
} | ||
|
||
public function testAwaitAsyncDelayCancelledAfterSmallPeriodStopsTimerAndBlocksUntilCancelled() | ||
{ | ||
$promise = async(function () { | ||
delay(1.0); | ||
})(); | ||
Loop::addTimer(0.02, fn() => $promise->cancel()); | ||
|
||
$time = microtime(true); | ||
try { | ||
await($promise); | ||
} catch (\RuntimeException $e) { | ||
$this->assertEquals('Delay cancelled', $e->getMessage()); | ||
} | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertGreaterThan(0.01, $time); | ||
$this->assertLessThan(0.03, $time); | ||
} | ||
} |