forked from nikic/ditaio
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFiberTest.php
166 lines (137 loc) · 4.14 KB
/
FiberTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Async\Tests;
use function Async\Fibers\{create_fiber, starting, resuming, suspending, throwing, fiber_return};
use Async\Co;
use Async\Fiber;
use Async\Panicking;
use PHPUnit\Framework\TestCase;
class FiberTest extends TestCase
{
protected function setUp(): void
{
if (((float) \phpversion() >= 8.1))
$this->markTestSkipped('For PHP 8.0 or less builtin Fibers.');
\coroutine_clear();
}
public function fiberArgs()
{
create_fiber('fi', function (int $x) {
return ($x + yield suspending($x));
});
yield starting('fi', 1);
yield resuming('fi', 5);
$this->assertEquals(6, fiber_return('fi'));
}
public function testArgs()
{
\coroutine_run($this->fiberArgs());
}
public function fiberResume()
{
create_fiber('fi', function () {
$value = yield suspending(1);
$this->assertEquals(2, $value);
});
$value = yield starting('fi');
$this->assertEquals(1, $value);
yield resuming('fi', $value + 1);
}
public function testResume()
{
\coroutine_run($this->fiberResume());
}
public function fiberCatch()
{
create_fiber('fi', function () {
try {
yield suspending('test');
} catch (\Exception $exception) {
$this->assertEquals('test', $exception->getMessage());
}
});
$value = yield starting('fi');
$this->assertEquals('test', $value);
yield throwing('fi', new \Exception('test'));
}
public function testCatch()
{
\coroutine_run($this->fiberCatch());
}
public function fiberGetReturn()
{
create_fiber('fi', function () {
$value = yield suspending(1);
return $value;
});
$value = yield starting('fi');
$this->assertEquals(1, $value);
$this->assertNull(yield resuming('fi', $value + 1));
$this->assertEquals(2, fiber_return('fi'));
}
public function testGetReturn()
{
\coroutine_run($this->fiberGetReturn());
}
public function fiberStatus()
{
create_fiber('fi', function () {
$fiber = Fiber::this();
$this->assertTrue($fiber->isStarted());
$this->assertTrue($fiber->isRunning());
$this->assertFalse($fiber->isSuspended());
$this->assertFalse($fiber->isTerminated());
yield suspending();
});
$fiber = Co::getFiber('fi');
$this->assertFalse($fiber->isStarted());
$this->assertFalse($fiber->isRunning());
$this->assertFalse($fiber->isSuspended());
$this->assertFalse($fiber->isTerminated());
yield starting('fi');
$this->assertTrue($fiber->isStarted());
$this->assertFalse($fiber->isRunning());
$this->assertTrue($fiber->isSuspended());
$this->assertFalse($fiber->isTerminated());
yield resuming('fi');
$this->assertTrue($fiber->isStarted());
$this->assertFalse($fiber->isRunning());
$this->assertFalse($fiber->isSuspended());
$this->assertTrue($fiber->isTerminated());
}
public function testStatus()
{
/**
* --EXPECT--
*bool(false) / before starting
*bool(false)
*bool(false)
*bool(false)
*bool(true) / inside fiber
*bool(true)
*bool(false)
*bool(false)
*bool(true) / after suspending
*bool(false)
*bool(true)
*bool(false)
*bool(true) / after resuming
*bool(false)
*bool(false)
*bool(true)
*/
\coroutine_run($this->fiberStatus());
}
public function taskCreatingFiberPanickingSame()
{
$this->expectException(Panicking::class);
create_fiber('childTask', function ($av = null) {
});
create_fiber('childTask', function ($av = null) {
});
yield \shutdown();
}
public function testCreatingFiberPanickingSame()
{
\coroutine_run($this->taskCreatingFiberPanickingSame());
}
}