Skip to content

Commit d4776ff

Browse files
authored
[12.x] Fix Concurrency::run to preserve callback result order (#55161)
* Fix Concurrency::run to preserve result order matching the input callbacks * CS fix * CS fix
1 parent aa1128c commit d4776ff

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Illuminate/Concurrency/ForkDriver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function run(Closure|array $tasks): array
2525
/** @phpstan-ignore class.notFound */
2626
$results = Fork::new()->run(...$values);
2727

28+
ksort($results);
29+
2830
return array_combine($keys, $results);
2931
}
3032

tests/Integration/Concurrency/ConcurrencyTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Process\Factory as ProcessFactory;
99
use Illuminate\Support\Facades\Concurrency;
1010
use Orchestra\Testbench\TestCase;
11+
use PHPUnit\Framework\Attributes\DataProvider;
1112
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
1213

1314
#[RequiresOperatingSystem('Linux|DAR')]
@@ -119,6 +120,42 @@ public function testRunHandlerProcessErrorWithCustomExceptionWithParam()
119120
),
120121
]);
121122
}
123+
124+
public static function getConcurrencyDrivers(): array
125+
{
126+
return [
127+
['sync'],
128+
['process'],
129+
// spatie/fork package is not included by default
130+
// ['fork'],
131+
];
132+
}
133+
134+
#[DataProvider('getConcurrencyDrivers')]
135+
public function testRunPreservesCallbackOrder(string $driver)
136+
{
137+
[$first, $second, $third] = Concurrency::driver($driver)->run([
138+
function () {
139+
usleep(1000000);
140+
141+
return 'first';
142+
},
143+
function () {
144+
usleep(500000);
145+
146+
return 'second';
147+
},
148+
function () {
149+
usleep(200000);
150+
151+
return 'third';
152+
},
153+
]);
154+
155+
$this->assertEquals('first', $first);
156+
$this->assertEquals('second', $second);
157+
$this->assertEquals('third', $third);
158+
}
122159
}
123160

124161
class ExceptionWithoutParam extends Exception

0 commit comments

Comments
 (0)