Skip to content

Commit 5e46e47

Browse files
committed
feat: Update tests for new feature.
1 parent 90a9edf commit 5e46e47

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
],
2626
"autoload": {
2727
"psr-4": {
28-
"ElementaryFramework\\Core\\Async\\": "src/Core/Async/",
29-
"Tests\\": "tests/"
28+
"ElementaryFramework\\Core\\Async\\": "src/Core/Async/"
3029
},
3130
"files": [
3231
"src/Core/Async/functions.php"
3332
]
3433
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"Tests\\": "tests/"
37+
}
38+
},
3539
"require-dev": {
3640
"pestphp/pest": "^4.1.3",
3741
"phpstan/phpstan": "^2.1.31"

tests/Feature/EventLoopTest.php

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,68 @@
239239

240240
$this->runEventLoopBriefly();
241241

242-
expect($executed)->toBeTrue();
242+
expect($executed)->toBeTrue()
243+
->and($promise->unwrap())->toBe('async result');
244+
});
245+
246+
it('handles simple generator functions', function () {
247+
if (!Async::supportsFibers()) {
248+
$this->markTestSkipped('Fibers not supported in this PHP version');
249+
}
250+
251+
$loop = EventLoop::getInstance();
252+
$executed = false;
253+
$result = null;
254+
255+
$promise = $loop->async(function() use (&$executed) {
256+
yield 'first';
257+
$executed = true;
258+
return 'async result';
259+
});
260+
261+
expect($promise)->toBePromise()
262+
->and($loop->hasPendingWork())->toBeTrue();
263+
264+
$this->runEventLoopBriefly();
265+
266+
expect($executed)->toBeTrue()
267+
->and($promise->unwrap())->toBe('async result');
268+
});
269+
270+
it('handles generator functions yielding fibers', function () {
271+
if (!Async::supportsFibers()) {
272+
$this->markTestSkipped('Fibers not supported in this PHP version');
273+
}
274+
275+
$loop = EventLoop::getInstance();
276+
$executed = false;
277+
$result = null;
278+
279+
$promise = $loop->async(function () use (&$executed): Generator {
280+
$dots = "";
281+
$fiber = new Fiber(function () use (&$dots, &$executed) {
282+
while (!$executed) {
283+
usleep(100000);
284+
$dots .= ".";
285+
Fiber::suspend($dots);
286+
}
287+
});
288+
289+
while ($dots !== ".................") {
290+
yield $fiber;
291+
}
292+
293+
$executed = true;
294+
return $dots;
295+
});
296+
297+
expect($promise)->toBePromise()
298+
->and($loop->hasPendingWork())->toBeTrue();
299+
300+
$loop->resume();
301+
302+
expect($executed)->toBeTrue()
303+
->and($promise->unwrap())->toBe('.................');
243304
});
244305

245306
it('handles exceptions in async operations', function () {

0 commit comments

Comments
 (0)