2017-12-13 22:58:26 +01:00
|
|
|
<?php
|
|
|
|
|
2017-12-13 23:36:35 +01:00
|
|
|
namespace Amp\ParallelFunctions\Test;
|
2017-12-13 22:58:26 +01:00
|
|
|
|
|
|
|
use Amp\MultiReasonException;
|
|
|
|
use Amp\PHPUnit\TestCase;
|
2017-12-14 17:01:25 +01:00
|
|
|
use function Amp\ParallelFunctions\parallelFilter;
|
2017-12-13 22:58:26 +01:00
|
|
|
use function Amp\Promise\wait;
|
|
|
|
|
2017-12-13 23:57:44 +01:00
|
|
|
class FilterTest extends TestCase {
|
2017-12-13 22:58:26 +01:00
|
|
|
public function testWithoutCallback() {
|
|
|
|
$input = [1, 0, 3, false, true, null];
|
|
|
|
|
2017-12-14 17:01:25 +01:00
|
|
|
$this->assertSame(\array_filter($input), wait(parallelFilter($input)));
|
2017-12-13 22:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithCallback() {
|
|
|
|
$input = [1, 0, 3, false, true, null];
|
|
|
|
$callback = function ($value) {
|
|
|
|
return $value === false;
|
|
|
|
};
|
|
|
|
|
2017-12-14 17:01:25 +01:00
|
|
|
$this->assertSame(\array_filter($input, $callback), wait(parallelFilter($input, $callback)));
|
2017-12-13 22:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithCallbackAndFlagKey() {
|
|
|
|
$input = [1, 0, 3, false, true, null];
|
|
|
|
$callback = function ($key) {
|
|
|
|
return $key === 2;
|
|
|
|
};
|
|
|
|
|
2017-12-14 17:01:25 +01:00
|
|
|
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_KEY), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_KEY)));
|
2017-12-13 22:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithCallbackAndFlagBoth() {
|
|
|
|
$input = [1, 0, 3, false, true, null];
|
|
|
|
$callback = function ($value, $key) {
|
|
|
|
return $key === 2 || $value === true;
|
|
|
|
};
|
|
|
|
|
2017-12-14 17:01:25 +01:00
|
|
|
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_BOTH), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_BOTH)));
|
2017-12-13 22:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testException() {
|
|
|
|
$this->expectException(MultiReasonException::class);
|
|
|
|
|
2017-12-14 17:01:25 +01:00
|
|
|
wait(parallelFilter([1, 2, 3], function () {
|
2017-12-13 22:58:26 +01:00
|
|
|
throw new \Exception;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExecutesAllTasksOnException() {
|
|
|
|
$files = [
|
2017-12-13 23:36:35 +01:00
|
|
|
[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 {
|
2017-12-14 17:01:25 +01:00
|
|
|
wait(parallelFilter($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');
|
|
|
|
}
|
|
|
|
}
|
2017-12-16 10:35:40 +01:00
|
|
|
|
|
|
|
public function testFilterWithNullCallable() {
|
|
|
|
$this->expectException(\Error::class);
|
|
|
|
|
|
|
|
$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-')],
|
|
|
|
];
|
|
|
|
|
|
|
|
wait(parallelFilter($files, null, ARRAY_FILTER_USE_BOTH));
|
|
|
|
}
|
2017-12-13 23:48:03 +01:00
|
|
|
}
|