Skip to content

Commit 5b474c4

Browse files
authored
Added Hyperf\Coroutine\Barrier (#7343)
1 parent 223f0f9 commit 5b474c4

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=8.1",
2020
"hyperf/context": "~3.1.0",
2121
"hyperf/contract": "~3.1.0",
22-
"hyperf/engine": "^2.13.0"
22+
"hyperf/engine": "^2.14.0"
2323
},
2424
"autoload": {
2525
"psr-4": {

src/Barrier.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\Coroutine;
14+
15+
class Barrier extends \Hyperf\Engine\Barrier
16+
{
17+
}

tests/BarrierTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Coroutine;
14+
15+
use Hyperf\Coroutine\Barrier;
16+
use Hyperf\Coroutine\Coroutine;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @internal
21+
* @coversNothing
22+
*/
23+
class BarrierTest extends TestCase
24+
{
25+
public function testBarrier()
26+
{
27+
$barrier = Barrier::create();
28+
$N = 10;
29+
$count = 0;
30+
for ($i = 0; $i < $N; ++$i) {
31+
Coroutine::create(function () use (&$count, $barrier) {
32+
isset($barrier);
33+
sleep(1);
34+
++$count;
35+
});
36+
}
37+
Barrier::wait($barrier);
38+
$this->assertSame($N, $count);
39+
}
40+
}

0 commit comments

Comments
 (0)