|
| 1 | +--TEST-- |
| 2 | +Coroutine: getTrace() - returns backtrace for suspended coroutine |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +use function Async\spawn; |
| 7 | +use function Async\suspend; |
| 8 | +use function Async\await; |
| 9 | + |
| 10 | +$parentCoroutine = null; |
| 11 | +$childCoroutine = null; |
| 12 | + |
| 13 | +// Spawn a parent coroutine |
| 14 | +$parentCoroutine = spawn(function() use (&$childCoroutine) { |
| 15 | + echo "Parent: Starting\n"; |
| 16 | + |
| 17 | + // Spawn a child coroutine that will suspend |
| 18 | + $childCoroutine = spawn(function() { |
| 19 | + echo "Child: Before suspend\n"; |
| 20 | + suspend(); |
| 21 | + echo "Child: After suspend\n"; |
| 22 | + }); |
| 23 | + |
| 24 | + // Suspend to let child start and suspend |
| 25 | + suspend(); |
| 26 | + |
| 27 | + echo "Parent: Back from suspend\n"; |
| 28 | + |
| 29 | + // Now check child's trace while it's suspended |
| 30 | + $trace = $childCoroutine->getTrace(); |
| 31 | + |
| 32 | + if ($trace !== null) { |
| 33 | + echo "Child trace is array: " . (is_array($trace) ? "yes" : "no") . "\n"; |
| 34 | + echo "Child trace has entries: " . (count($trace) > 0 ? "yes" : "no") . "\n"; |
| 35 | + |
| 36 | + // Test with DEBUG_BACKTRACE_IGNORE_ARGS |
| 37 | + $traceNoArgs = $childCoroutine->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
| 38 | + echo "Trace with IGNORE_ARGS is array: " . (is_array($traceNoArgs) ? "yes" : "no") . "\n"; |
| 39 | + |
| 40 | + // Test with limit |
| 41 | + $traceLimited = $childCoroutine->getTrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); |
| 42 | + echo "Trace with limit is array: " . (is_array($traceLimited) ? "yes" : "no") . "\n"; |
| 43 | + } else { |
| 44 | + echo "Child trace is null\n"; |
| 45 | + } |
| 46 | + |
| 47 | + // Yield control to allow scheduler to resume the child coroutine |
| 48 | + suspend(); |
| 49 | + |
| 50 | + // After completion, trace should be null |
| 51 | + $traceAfter = $childCoroutine->getTrace(); |
| 52 | + echo "Child trace after completion is null: " . ($traceAfter === null ? "yes" : "no") . "\n"; |
| 53 | +}); |
| 54 | + |
| 55 | +?> |
| 56 | +--EXPECT-- |
| 57 | +Parent: Starting |
| 58 | +Child: Before suspend |
| 59 | +Parent: Back from suspend |
| 60 | +Child trace is array: yes |
| 61 | +Child trace has entries: yes |
| 62 | +Trace with IGNORE_ARGS is array: yes |
| 63 | +Trace with limit is array: yes |
| 64 | +Child: After suspend |
| 65 | +Child trace after completion is null: yes |
0 commit comments