parallel-functions/test/ParallelMapTest.php

50 lines
1.4 KiB
PHP
Raw Normal View History

2017-12-13 22:58:26 +01:00
<?php
namespace Amp\ParallelFunctions\Test;
2017-12-13 22:58:26 +01:00
use Amp\MultiReasonException;
use Amp\PHPUnit\TestCase;
use function Amp\ParallelFunctions\parallelMap;
2017-12-13 22:58:26 +01:00
use function Amp\Promise\wait;
class ParallelMapTest extends TestCase {
public function testValidInput() {
$this->assertSame([3, 4, 5], wait(parallelMap([1, 2, 3], function ($input) {
2017-12-13 22:58:26 +01:00
return $input + 2;
})));
}
public function testException() {
$this->expectException(MultiReasonException::class);
wait(parallelMap([1, 2, 3], function () {
2017-12-13 22:58:26 +01:00
throw new \Exception;
}));
}
public function testExecutesAllTasksOnException() {
$files = [
[0, \tempnam(\sys_get_temp_dir(), 'amp-parallel-functions-')],
[1, \tempnam(\sys_get_temp_dir(), 'amp-parallel-functions-')],
[2, \tempnam(\sys_get_temp_dir(), 'amp-parallel-functions-')],
2017-12-13 22:58:26 +01:00
];
try {
wait(parallelMap($files, function ($args) {
2017-12-13 22:58:26 +01:00
list($id, $filename) = $args;
if ($id === 0) {
throw new \Exception;
}
\sleep(1);
\file_put_contents($filename, $id);
}));
$this->fail('No exception thrown.');
} catch (MultiReasonException $e) {
$this->assertStringEqualsFile($files[1][1], '1');
$this->assertStringEqualsFile($files[2][1], '2');
}
}
}