-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 重命名请求上下文 getCurrentFlag() => getCurrentId() * 重构 RequestContext,尽量不污染 Swoole 原有上下文,并且支持 defer() * 修复、更新文档、测试
- Loading branch information
Showing
10 changed files
with
247 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Components/swoole/tests/unit/Component/Tests/RequestContextTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Imi\Swoole\Test\Component\Tests; | ||
|
||
use Imi\RequestContext; | ||
use Imi\Test\BaseTest; | ||
|
||
use function Yurun\Swoole\Coroutine\goWait; | ||
|
||
/** | ||
* @testdox RequestContext | ||
*/ | ||
class RequestContextTest extends BaseTest | ||
{ | ||
public function testDefer(): void | ||
{ | ||
$result = []; | ||
goWait(static function () use (&$result): void { | ||
RequestContext::defer(static function () use (&$result): void { | ||
$result[] = 1; | ||
}); | ||
RequestContext::defer(static function () use (&$result): void { | ||
$result[] = 2; | ||
}); | ||
}, -1, true); | ||
$this->assertEquals([2, 1], $result); | ||
} | ||
|
||
public function testRemember(): void | ||
{ | ||
$key = 'test_remember'; | ||
$count = 0; | ||
$countFun = static function () use (&$count) { | ||
return ++$count; | ||
}; | ||
|
||
RequestContext::unset($key); | ||
$this->assertEquals(0, $count); | ||
$this->assertEquals(1, RequestContext::remember($key, $countFun)); | ||
$this->assertEquals(1, $count); | ||
$this->assertEquals(1, RequestContext::remember($key, $countFun)); | ||
$this->assertEquals(1, $count); | ||
RequestContext::unset($key); | ||
$this->assertEquals(2, RequestContext::remember($key, $countFun)); | ||
$this->assertEquals(2, $count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Imi\Core\Context; | ||
|
||
class ContextData extends \ArrayObject | ||
{ | ||
protected \SplStack $deferCallbacks; | ||
|
||
public function __construct(mixed $input = []) | ||
{ | ||
parent::__construct($input, self::ARRAY_AS_PROPS, \ArrayIterator::class); | ||
$this->deferCallbacks = new \SplStack(); | ||
} | ||
|
||
/** | ||
* 推迟执行,当协程释放时触发,先进后出. | ||
*/ | ||
public function defer(callable $callback): void | ||
{ | ||
$this->deferCallbacks[] = $callback; | ||
} | ||
|
||
/** | ||
* 获取推迟执行任务栈. | ||
*/ | ||
public function getDeferCallbacks(): \SplStack | ||
{ | ||
return $this->deferCallbacks; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.