Skip to content

Commit 5ede1d4

Browse files
CopilotEdmondDantes
andcommitted
Add comprehensive tests for Coroutine::getTrace() method
Co-authored-by: EdmondDantes <1571649+EdmondDantes@users.noreply.github.com>
1 parent af37377 commit 5ede1d4

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
--TEST--
2-
Coroutine: getTrace() - returns empty array (TODO implementation)
2+
Coroutine: getTrace() - returns empty array for non-suspended coroutine
33
--FILE--
44
<?php
55

66
use function Async\spawn;
7+
use function Async\await;
78

89
$coroutine = spawn(function() {
910
return "test";
1011
});
1112

13+
// Wait for coroutine to complete
14+
await($coroutine);
15+
16+
// After completion, trace should be empty
1217
$trace = $coroutine->getTrace();
1318

1419
var_dump(is_array($trace));
1520
var_dump(count($trace));
1621

22+
// Test with options parameter
23+
$trace2 = $coroutine->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS);
24+
var_dump(is_array($trace2));
25+
var_dump(count($trace2));
26+
27+
// Test with limit parameter
28+
$trace3 = $coroutine->getTrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5);
29+
var_dump(is_array($trace3));
30+
var_dump(count($trace3));
31+
1732
?>
1833
--EXPECT--
1934
bool(true)
35+
int(0)
36+
bool(true)
37+
int(0)
38+
bool(true)
2039
int(0)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
Coroutine: getTrace() - returns backtrace for suspended coroutine
3+
--FILE--
4+
<?php
5+
6+
use function Async\spawn;
7+
use function Async\await;
8+
9+
$coroutine = spawn(function() {
10+
// This function will suspend
11+
Async\sleep(0.1);
12+
return "test";
13+
});
14+
15+
// Get trace while coroutine is suspended
16+
$trace = $coroutine->getTrace();
17+
18+
var_dump(is_array($trace));
19+
echo "Trace has entries: " . (count($trace) > 0 ? "yes" : "no") . "\n";
20+
21+
// Test with DEBUG_BACKTRACE_IGNORE_ARGS
22+
$traceNoArgs = $coroutine->getTrace(DEBUG_BACKTRACE_IGNORE_ARGS);
23+
var_dump(is_array($traceNoArgs));
24+
25+
// Test with limit
26+
$traceLimited = $coroutine->getTrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
27+
var_dump(is_array($traceLimited));
28+
29+
// Wait for coroutine to complete
30+
await($coroutine);
31+
32+
// After completion, trace should be empty
33+
$traceAfter = $coroutine->getTrace();
34+
var_dump(is_array($traceAfter));
35+
var_dump(count($traceAfter) === 0);
36+
37+
?>
38+
--EXPECTF--
39+
bool(true)
40+
Trace has entries: %s
41+
bool(true)
42+
bool(true)
43+
bool(true)
44+
bool(true)

0 commit comments

Comments
 (0)