Skip to content

Commit 5ea7c6d

Browse files
committed
+ exec functions
1 parent bbfcde4 commit 5ea7c6d

7 files changed

+391
-0
lines changed

tests/exec/004-exec_basic.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
exec() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("exec")) echo "skip exec() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
spawn(function () {
15+
echo "Starting async exec test\n";
16+
17+
$php = getenv('TEST_PHP_EXECUTABLE');
18+
if ($php === false) {
19+
die("skip no php executable defined");
20+
}
21+
22+
$output = [];
23+
$return_var = null;
24+
25+
exec($php . ' -r "echo \'Hello from async exec\';"', $output, $return_var);
26+
27+
echo "Output: " . implode("\n", $output) . "\n";
28+
echo "Return code: " . $return_var . "\n";
29+
echo "Exec test completed successfully\n";
30+
});
31+
32+
spawn(function() {
33+
echo "Other async task executing\n";
34+
});
35+
36+
echo "Main thread end\n";
37+
?>
38+
--EXPECT--
39+
Main thread start
40+
Main thread end
41+
Starting async exec test
42+
Other async task executing
43+
Output: Hello from async exec
44+
Return code: 0
45+
Exec test completed successfully
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
shell_exec() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("shell_exec")) echo "skip shell_exec() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
spawn(function () {
15+
echo "Starting async shell_exec test\n";
16+
17+
$php = getenv('TEST_PHP_EXECUTABLE');
18+
if ($php === false) {
19+
die("skip no php executable defined");
20+
}
21+
22+
$output = shell_exec($php . ' -r "echo \'Hello from async shell_exec\';"');
23+
24+
echo "Output: " . trim($output) . "\n";
25+
echo "Shell_exec test completed successfully\n";
26+
});
27+
28+
spawn(function() {
29+
echo "Other async task executing\n";
30+
});
31+
32+
echo "Main thread end\n";
33+
?>
34+
--EXPECT--
35+
Main thread start
36+
Main thread end
37+
Starting async shell_exec test
38+
Other async task executing
39+
Output: Hello from async shell_exec
40+
Shell_exec test completed successfully
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
exec() multiple concurrent async executions
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("exec")) echo "skip exec() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
$results = [];
13+
14+
echo "Main start\n";
15+
16+
spawn(function () use (&$results) {
17+
echo "Exec 1 starting\n";
18+
19+
$php = getenv('TEST_PHP_EXECUTABLE');
20+
if ($php === false) {
21+
die("skip no php executable defined");
22+
}
23+
24+
$output = [];
25+
$return_var = null;
26+
27+
exec($php . ' -r "usleep(30000); echo \'Exec 1 done\';"', $output, $return_var);
28+
29+
$results[] = "Exec 1: " . implode("", $output) . " (exit: $return_var)";
30+
echo "Exec 1 completed\n";
31+
});
32+
33+
spawn(function () use (&$results) {
34+
echo "Exec 2 starting\n";
35+
36+
$php = getenv('TEST_PHP_EXECUTABLE');
37+
if ($php === false) {
38+
die("skip no php executable defined");
39+
}
40+
41+
$output = [];
42+
$return_var = null;
43+
44+
exec($php . ' -r "usleep(20000); echo \'Exec 2 done\';"', $output, $return_var);
45+
46+
$results[] = "Exec 2: " . implode("", $output) . " (exit: $return_var)";
47+
echo "Exec 2 completed\n";
48+
});
49+
50+
spawn(function() {
51+
echo "Other task executing\n";
52+
});
53+
54+
echo "Main end\n";
55+
?>
56+
--EXPECT--
57+
Main start
58+
Main end
59+
Exec 1 starting
60+
Exec 2 starting
61+
Other task executing
62+
Exec 2 completed
63+
Exec 1 completed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
exec() vs shell_exec() async comparison
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("exec") || !function_exists("shell_exec")) {
6+
echo "skip exec() or shell_exec() is not available";
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
use function Async\spawn;
13+
14+
echo "Starting comparison test\n";
15+
16+
spawn(function () {
17+
echo "Testing exec() async\n";
18+
19+
$php = getenv('TEST_PHP_EXECUTABLE');
20+
if ($php === false) {
21+
die("skip no php executable defined");
22+
}
23+
24+
$output = [];
25+
$return_var = null;
26+
27+
exec($php . ' -r "echo \'exec result\';"', $output, $return_var);
28+
29+
echo "exec() result: " . implode("", $output) . " (code: $return_var)\n";
30+
});
31+
32+
spawn(function () {
33+
echo "Testing shell_exec() async\n";
34+
35+
$php = getenv('TEST_PHP_EXECUTABLE');
36+
if ($php === false) {
37+
die("skip no php executable defined");
38+
}
39+
40+
$output = shell_exec($php . ' -r "echo \'shell_exec result\';"');
41+
42+
echo "shell_exec() result: " . trim($output) . "\n";
43+
});
44+
45+
spawn(function() {
46+
echo "Background task running\n";
47+
});
48+
49+
echo "Comparison test completed\n";
50+
?>
51+
--EXPECT--
52+
Starting comparison test
53+
Comparison test completed
54+
Testing exec() async
55+
Testing shell_exec() async
56+
Background task running
57+
exec() result: exec result (code: 0)
58+
shell_exec() result: shell_exec result

tests/exec/008-system_basic.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
system() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("system")) echo "skip system() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
spawn(function () {
15+
echo "Starting async system test\n";
16+
17+
$php = getenv('TEST_PHP_EXECUTABLE');
18+
if ($php === false) {
19+
die("skip no php executable defined");
20+
}
21+
22+
$return_var = null;
23+
24+
$output = system($php . ' -r "echo \'Hello from async system\';"', $return_var);
25+
26+
echo "Output: " . $output . "\n";
27+
echo "Return code: " . $return_var . "\n";
28+
echo "System test completed successfully\n";
29+
});
30+
31+
spawn(function() {
32+
echo "Other async task executing\n";
33+
});
34+
35+
echo "Main thread end\n";
36+
?>
37+
--EXPECT--
38+
Main thread start
39+
Main thread end
40+
Starting async system test
41+
Other async task executing
42+
Hello from async system
43+
Output: Hello from async system
44+
Return code: 0
45+
System test completed successfully

tests/exec/009-passthru_basic.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
passthru() async basic functionality
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists("passthru")) echo "skip passthru() is not available";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
use function Async\spawn;
11+
12+
echo "Main thread start\n";
13+
14+
spawn(function () {
15+
echo "Starting async passthru test\n";
16+
17+
$php = getenv('TEST_PHP_EXECUTABLE');
18+
if ($php === false) {
19+
die("skip no php executable defined");
20+
}
21+
22+
$return_var = null;
23+
24+
passthru($php . ' -r "echo \'Hello from async passthru\';"', $return_var);
25+
26+
echo "Return code: " . $return_var . "\n";
27+
echo "Passthru test completed successfully\n";
28+
});
29+
30+
spawn(function() {
31+
echo "Other async task executing\n";
32+
});
33+
34+
echo "Main thread end\n";
35+
?>
36+
--EXPECT--
37+
Main thread start
38+
Main thread end
39+
Starting async passthru test
40+
Other async task executing
41+
Hello from async passthruReturn code: 0
42+
Passthru test completed successfully

0 commit comments

Comments
 (0)