|
239 | 239 |
|
240 | 240 | $this->runEventLoopBriefly(); |
241 | 241 |
|
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('.................'); |
243 | 304 | }); |
244 | 305 |
|
245 | 306 | it('handles exceptions in async operations', function () { |
|
0 commit comments