Fixed bug when using (new Parallel())->add($callback, $key) and the parameter $key is a not string index, the returned result will be sorted from 0.

This commit is contained in:
wiseker 2020-02-07 19:21:23 +08:00 committed by GitHub
parent 4525a2d9d7
commit fd9404ac2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 3 deletions

View File

@ -16,6 +16,10 @@
- [#1303](https://github.com/hyperf/hyperf/pull/1303) Deleted useless `$httpMethod` for `Hyperf\RpcServer\Router\Router`.
## Fixed
- [#1330](https://github.com/hyperf/hyperf/pull/1330) Fixed bug when using `(new Parallel())->add($callback, $key)` and the parameter `$key` is a not string index, the returned result will sort `$key` from 0.
# v1.1.17 - 2020-01-24
## Added

View File

@ -39,10 +39,10 @@ class Parallel
public function add(callable $callable, $key = null)
{
if (is_string($key)) {
$this->callbacks[$key] = $callable;
} else {
if (is_null($key)) {
$this->callbacks[] = $callable;
} else {
$this->callbacks[$key] = $callable;
}
}

View File

@ -117,6 +117,39 @@ class ParallelTest extends TestCase
$this->assertEquals(count($res), 4);
}
public function testParallelKeys()
{
$parallel = new Parallel();
$callback = function () {
return 1;
};
for ($i = 0; $i < 4; ++$i) {
$parallel->add($callback);
}
$res = $parallel->wait();
$parallel->clear();
$this->assertSame([1, 1, 1, 1], $res);
for ($i = 0; $i < 4; ++$i) {
$parallel->add($callback, 'id_' . $i);
}
$res = $parallel->wait();
$parallel->clear();
$this->assertSame(['id_0' => 1, 'id_1' => 1, 'id_2' => 1, 'id_3' => 1], $res);
for ($i = 0; $i < 4; ++$i) {
$parallel->add($callback, $i - 1);
}
$res = $parallel->wait();
$parallel->clear();
$this->assertSame([-1 => 1, 0 => 1, 1 => 1, 2 => 1], $res);
$parallel->add($callback, 1.0);
$res = $parallel->wait();
$parallel->clear();
$this->assertSame([1.0 => 1], $res);
}
public function testParallelThrows()
{
$parallel = new Parallel();