Skip to content

Commit 74ec513

Browse files
authored
Added Barrier (#36)
1 parent 3002d34 commit 74ec513

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"require": {
2626
"php": ">=8.0",
27-
"hyperf/engine-contract": "~1.12.0"
27+
"hyperf/engine-contract": "~1.13.0"
2828
},
2929
"require-dev": {
3030
"friendsofphp/php-cs-fixer": "^3.0",
@@ -52,7 +52,7 @@
5252
},
5353
"extra": {
5454
"branch-alias": {
55-
"dev-master": "2.13-dev"
55+
"dev-master": "2.14-dev"
5656
},
5757
"hyperf": {
5858
"config": "Hyperf\\Engine\\ConfigProvider"

src/Barrier.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Engine;
14+
15+
use Hyperf\Engine\Contract\BarrierInterface;
16+
use Swoole\Coroutine\Barrier as SwooleBarrier;
17+
18+
class Barrier implements BarrierInterface
19+
{
20+
public static function wait(object &$barrier, int $timeout = -1): void
21+
{
22+
SwooleBarrier::wait($barrier, $timeout);
23+
}
24+
25+
public static function create(): object
26+
{
27+
return SwooleBarrier::make();
28+
}
29+
}

tests/Cases/BarrierTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Cases;
14+
15+
use Hyperf\Engine\Barrier;
16+
use Hyperf\Engine\Coroutine;
17+
18+
/**
19+
* @internal
20+
* @coversNothing
21+
*/
22+
class BarrierTest extends AbstractTestCase
23+
{
24+
public function testBarrier()
25+
{
26+
$this->runInCoroutine(function () {
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+
usleep(2000);
34+
++$count;
35+
});
36+
}
37+
Barrier::wait($barrier);
38+
$this->assertSame($N, $count);
39+
});
40+
}
41+
}

0 commit comments

Comments
 (0)