Skip to content

Commit 92c545f

Browse files
committed
Add FibersConcurrencyTest for validating fiber parameter and return contracts for fiber suspension boundaries
1 parent 7377d9e commit 92c545f

1 file changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use TypePHP\Exception\TypeError;
6+
use TypePHP\Tests\Fixtures\Domain\Cat;
7+
use TypePHP\Tests\Fixtures\Domain\Dog;
8+
use TypePHP\Tests\Fixtures\Generics\GenericCollection;
9+
use TypePHP\Tests\Fixtures\Services\UserService;
10+
use TypePHP\TypePHP;
11+
12+
/**
13+
* Function executed inside a Fiber with strict contracts
14+
*
15+
* @param positive-int $id
16+
* @param non-empty-string $name
17+
* @return array{id: positive-int, name: non-empty-string}
18+
*/
19+
function fiberTaskWithContracts(int $id, string $name): array
20+
{
21+
$step = Fiber::suspend("paused_at_step_1");
22+
23+
return [
24+
'id' => $id,
25+
'name' => "{$name}_{$step}",
26+
];
27+
}
28+
29+
/**
30+
* Fiber function with invalid return type
31+
*
32+
* @return positive-int
33+
*/
34+
function fiberTaskWithBadReturn(): int
35+
{
36+
Fiber::suspend();
37+
return -99;
38+
}
39+
40+
describe('PHP 8.1+ Fibers & Concurrent Coroutine Execution', function () {
41+
42+
describe('Basic Fiber Parameter and Return Contracts', function () {
43+
test('validates parameter contracts on fiber start and return shape on completion', function () {
44+
$fiber = new Fiber(function (int $id, string $name) {
45+
return fiberTaskWithContracts($id, $name);
46+
});
47+
48+
$state1 = $fiber->start(42, 'Alice');
49+
expect($state1)->toBe('paused_at_step_1')
50+
->and($fiber->isSuspended())->toBeTrue();
51+
52+
$fiber->resume('step_2');
53+
54+
expect($fiber->isTerminated())->toBeTrue()
55+
->and($fiber->getReturn())->toBe([
56+
'id' => 42,
57+
'name' => 'Alice_step_2',
58+
]);
59+
});
60+
61+
test('throws TypeError immediately when starting a fiber with invalid parameters', function () {
62+
$fiber = new Fiber(function (int $id, string $name) {
63+
return fiberTaskWithContracts($id, $name);
64+
});
65+
66+
expect(fn () => $fiber->start(-5, 'Alice'))
67+
->toThrow(TypeError::class, 'positive-int');
68+
});
69+
70+
test('throws TypeError upon fiber completion when return value violates contract', function () {
71+
$fiber = new Fiber(function () {
72+
return fiberTaskWithBadReturn();
73+
});
74+
75+
$fiber->start();
76+
expect($fiber->isSuspended())->toBeTrue();
77+
78+
expect(fn () => $fiber->resume())
79+
->toThrow(TypeError::class, 'Return value must be of type positive-int');
80+
});
81+
});
82+
83+
describe('Interleaved Fiber Execution & Reified Generics Isolation (\WeakMap)', function () {
84+
test('isolates generic template states across concurrent interleaved fibers without cross-contamination', function () {
85+
$fiberDog = new Fiber(function () {
86+
/** @var GenericCollection<Dog> $dogs */
87+
$dogs = new GenericCollection();
88+
$dogs->add(new Dog());
89+
90+
Fiber::suspend($dogs);
91+
92+
$dogs->add(new Dog());
93+
return $dogs;
94+
});
95+
96+
$fiberCat = new Fiber(function () {
97+
/** @var GenericCollection<Cat> $cats */
98+
$cats = new GenericCollection();
99+
$cats->add(new Cat());
100+
101+
Fiber::suspend($cats);
102+
$cats->add(new Cat());
103+
return $cats;
104+
});
105+
106+
/** @var GenericCollection<Dog> $dogCollection */
107+
$dogCollection = $fiberDog->start();
108+
/** @var GenericCollection<Cat> $catCollection */
109+
$catCollection = $fiberCat->start();
110+
111+
expect($dogCollection)->toBeInstanceOf(GenericCollection::class)
112+
->and(TypePHP::getGenericType($dogCollection))->toBe(Dog::class);
113+
114+
expect($catCollection)->toBeInstanceOf(GenericCollection::class)
115+
->and(TypePHP::getGenericType($catCollection))->toBe(Cat::class);
116+
117+
$fiberDog->resume();
118+
$fiberCat->resume();
119+
120+
$finalDogs = $fiberDog->getReturn();
121+
$finalCats = $fiberCat->getReturn();
122+
123+
expect($finalDogs->count())->toBe(2)
124+
->and($finalCats->count())->toBe(2);
125+
126+
expect(fn () => $dogCollection->add(new Cat()))
127+
->toThrow(TypeError::class, 'must be of type ' . Dog::class);
128+
129+
expect(fn () => $catCollection->add(new Dog()))
130+
->toThrow(TypeError::class, 'must be of type ' . Cat::class);
131+
});
132+
});
133+
134+
describe('Multi-Fiber Concurrency Stress Test', function () {
135+
test('executes 50 concurrent interleaved fibers with generic collections and shape contracts', function () {
136+
$fibers = [];
137+
138+
for ($i = 1; $i <= 50; $i++) {
139+
$fibers[$i] = new Fiber(function (int $fiberId) {
140+
$service = new UserService();
141+
$user = $service->find($fiberId);
142+
143+
Fiber::suspend("fiber_{$fiberId}_paused");
144+
145+
/** @var GenericCollection<Dog> $dogs */
146+
$dogs = new GenericCollection();
147+
$dogs->add(new Dog());
148+
149+
return [
150+
'user' => $user,
151+
'dog_count' => $dogs->count(),
152+
];
153+
});
154+
}
155+
156+
foreach ($fibers as $id => $fiber) {
157+
$status = $fiber->start($id);
158+
expect($status)->toBe("fiber_{$id}_paused")
159+
->and($fiber->isSuspended())->toBeTrue();
160+
}
161+
162+
foreach ($fibers as $id => $fiber) {
163+
$fiber->resume();
164+
$data = $fiber->getReturn();
165+
166+
expect($fiber->isTerminated())->toBeTrue()
167+
->and($data['user']['id'])->toBe($id)
168+
->and($data['dog_count'])->toBe(1);
169+
}
170+
});
171+
});
172+
173+
describe('Fiber Error Injections and Cleanup ($fiber->throw())', function () {
174+
test('cleans up execution state cleanly when an exception is injected into a suspended fiber', function () {
175+
$fiber = new Fiber(function () {
176+
/** @var GenericCollection<Dog> $dogs */
177+
$dogs = new GenericCollection();
178+
$dogs->add(new Dog());
179+
180+
Fiber::suspend();
181+
182+
return $dogs;
183+
});
184+
185+
$fiber->start();
186+
expect($fiber->isSuspended())->toBeTrue();
187+
188+
expect(function () use ($fiber) {
189+
$fiber->throw(new RuntimeException('Injected coroutine cancellation'));
190+
})->toThrow(RuntimeException::class, 'Injected coroutine cancellation');
191+
192+
expect($fiber->isTerminated())->toBeTrue();
193+
});
194+
});
195+
});

0 commit comments

Comments
 (0)