Last active
August 29, 2015 14:00
-
-
Save brianium/11388900 to your computer and use it in GitHub Desktop.
hacklang async?
This file contains hidden or 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
<?hh //partial | |
class TestResult { | |
public function __construct(public string $message) | |
{ | |
} | |
} | |
class TestCase { | |
public function __construct(protected string $name) | |
{ | |
} | |
public function testSlower(): TestResult | |
{ | |
$i = 0; | |
while ($i < 1000000000) { | |
$i++; | |
} | |
return new TestResult("test slower OK\n"); | |
} | |
public function testFaster(): TestResult | |
{ | |
$i = 0; | |
while ($i < 100000) { | |
$i++; | |
} | |
return new TestResult("test faster OK\n"); | |
} | |
public function run(): TestResult | |
{ | |
if ($this->name == "testSlower") { | |
$result = $this->testSlower(); | |
} else { | |
$result = $this->testFaster(); | |
} | |
print $result->message; | |
return $result; | |
} | |
} | |
$testSlower = new TestCase('testSlower'); | |
$testFaster = new TestCase('testFaster'); | |
async function runTest(TestCase $case): Awaitable<TestResult> { | |
//how can I make this run in parallel? | |
return $case->run(); | |
} | |
async function runTests(array<TestCase> $cases): Awaitable<T> { | |
$tasks = array(); | |
foreach ($cases as $case) { | |
$tasks[] = runTest($case); | |
} | |
await GenArrayWaitHandle::create($tasks); | |
} | |
runTests(array($testSlower, $testFaster))->join();//expect testFaster to execute first |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment