|
| 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\Incubator\Cases\Barrier; |
| 14 | + |
| 15 | +use Hyperf\Context\Context as HyperfContext; |
| 16 | +use Hyperf\Incubator\Barrier\Context; |
| 17 | +use PHPUnit\Framework\Attributes\CoversNothing; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +use function Hyperf\Coroutine\go; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[CoversNothing] |
| 26 | +class ContextTest extends TestCase |
| 27 | +{ |
| 28 | + protected function tearDown(): void |
| 29 | + { |
| 30 | + Context::clearAll(); |
| 31 | + } |
| 32 | + |
| 33 | + public function testWithKeyAndKey() |
| 34 | + { |
| 35 | + $key = 'test_barrier_key'; |
| 36 | + $result = Context::withKey($key); |
| 37 | + $this->assertEquals($key, $result); |
| 38 | + $this->assertEquals($key, Context::key()); |
| 39 | + |
| 40 | + Context::withKey(''); |
| 41 | + $this->assertEquals('', Context::key()); |
| 42 | + |
| 43 | + Context::clearAll(); |
| 44 | + $this->assertEquals('', Context::key()); |
| 45 | + } |
| 46 | + |
| 47 | + public function testWithPartiesAndParties() |
| 48 | + { |
| 49 | + $parties = 5; |
| 50 | + $result = Context::withParties($parties); |
| 51 | + $this->assertEquals($parties, $result); |
| 52 | + $this->assertEquals($parties, Context::parties()); |
| 53 | + |
| 54 | + Context::withParties(0); |
| 55 | + $this->assertEquals(0, Context::parties()); |
| 56 | + |
| 57 | + Context::withParties(-1); |
| 58 | + $this->assertEquals(-1, Context::parties()); |
| 59 | + |
| 60 | + Context::clearAll(); |
| 61 | + $this->assertEquals(0, Context::parties()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testWithTimeoutAndTimeout() |
| 65 | + { |
| 66 | + $timeout = 10.5; |
| 67 | + $result = Context::withTimeout($timeout); |
| 68 | + $this->assertEquals($timeout, $result); |
| 69 | + $this->assertEquals($timeout, Context::timeout()); |
| 70 | + |
| 71 | + Context::withTimeout(0.0); |
| 72 | + $this->assertEquals(0.0, Context::timeout()); |
| 73 | + |
| 74 | + Context::withTimeout(-1.0); |
| 75 | + $this->assertEquals(-1.0, Context::timeout()); |
| 76 | + |
| 77 | + Context::clearAll(); |
| 78 | + $this->assertEquals(-1, Context::timeout()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testClear() |
| 82 | + { |
| 83 | + Context::withKey('test_key'); |
| 84 | + Context::withParties(3); |
| 85 | + Context::withTimeout(5.0); |
| 86 | + |
| 87 | + Context::clear(Context::BARRIER_KEY); |
| 88 | + $this->assertEquals('', Context::key()); |
| 89 | + $this->assertEquals(3, Context::parties()); |
| 90 | + $this->assertEquals(5.0, Context::timeout()); |
| 91 | + |
| 92 | + Context::clear(Context::BARRIER_PARTIES); |
| 93 | + $this->assertEquals('', Context::key()); |
| 94 | + $this->assertEquals(0, Context::parties()); |
| 95 | + $this->assertEquals(5.0, Context::timeout()); |
| 96 | + |
| 97 | + Context::clear(Context::BARRIER_TIMEOUT); |
| 98 | + $this->assertEquals('', Context::key()); |
| 99 | + $this->assertEquals(0, Context::parties()); |
| 100 | + $this->assertEquals(-1, Context::timeout()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testClearAll() |
| 104 | + { |
| 105 | + Context::withKey('test_key'); |
| 106 | + Context::withParties(3); |
| 107 | + Context::withTimeout(5.0); |
| 108 | + |
| 109 | + $this->assertEquals('test_key', Context::key()); |
| 110 | + $this->assertEquals(3, Context::parties()); |
| 111 | + $this->assertEquals(5.0, Context::timeout()); |
| 112 | + |
| 113 | + Context::clearAll(); |
| 114 | + |
| 115 | + $this->assertEquals('', Context::key()); |
| 116 | + $this->assertEquals(0, Context::parties()); |
| 117 | + $this->assertEquals(-1, Context::timeout()); |
| 118 | + } |
| 119 | + |
| 120 | + public function testConstants() |
| 121 | + { |
| 122 | + $this->assertEquals('barrier_key', Context::BARRIER_KEY); |
| 123 | + $this->assertEquals('barrier_parties', Context::BARRIER_PARTIES); |
| 124 | + $this->assertEquals('barrier_timeout', Context::BARRIER_TIMEOUT); |
| 125 | + } |
| 126 | + |
| 127 | + public function testMultipleOperations() |
| 128 | + { |
| 129 | + Context::withKey('key1'); |
| 130 | + $this->assertEquals('key1', Context::key()); |
| 131 | + |
| 132 | + Context::withKey('key2'); |
| 133 | + $this->assertEquals('key2', Context::key()); |
| 134 | + |
| 135 | + Context::withParties(10); |
| 136 | + $this->assertEquals(10, Context::parties()); |
| 137 | + $this->assertEquals('key2', Context::key()); |
| 138 | + |
| 139 | + Context::withTimeout(15.5); |
| 140 | + $this->assertEquals(15.5, Context::timeout()); |
| 141 | + $this->assertEquals(10, Context::parties()); |
| 142 | + $this->assertEquals('key2', Context::key()); |
| 143 | + } |
| 144 | + |
| 145 | + public function testEdgeCases() |
| 146 | + { |
| 147 | + Context::withParties(PHP_INT_MAX); |
| 148 | + $this->assertEquals(PHP_INT_MAX, Context::parties()); |
| 149 | + |
| 150 | + Context::withParties(PHP_INT_MIN); |
| 151 | + $this->assertEquals(PHP_INT_MIN, Context::parties()); |
| 152 | + |
| 153 | + Context::withTimeout(PHP_FLOAT_MAX); |
| 154 | + $this->assertEquals(PHP_FLOAT_MAX, Context::timeout()); |
| 155 | + |
| 156 | + Context::withTimeout(-PHP_FLOAT_MAX); |
| 157 | + $this->assertEquals(-PHP_FLOAT_MAX, Context::timeout()); |
| 158 | + |
| 159 | + $longKey = str_repeat('a', 1000); |
| 160 | + Context::withKey($longKey); |
| 161 | + $this->assertEquals($longKey, Context::key()); |
| 162 | + } |
| 163 | + |
| 164 | + public function testIsolation() |
| 165 | + { |
| 166 | + Context::withTimeout(1.23); |
| 167 | + go(function () { |
| 168 | + $this->assertEmpty(HyperfContext::getContainer()); |
| 169 | + }); |
| 170 | + } |
| 171 | +} |
0 commit comments