@@ -10,85 +10,91 @@ use function Async\spawn;
1010use function Async \await ;
1111
1212$ server = async_test_server_start ();
13+ $ output = [];
1314
1415function sync_request ($ server ) {
15- echo "Sync request start \n" ;
16-
16+ global $ output ;
17+ $ output ['0 ' ] = "Sync request start " ;
18+
1719 $ ch = curl_init ();
1820 curl_setopt ($ ch , CURLOPT_URL , "http://localhost: {$ server ->port }/ " );
1921 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
2022 curl_setopt ($ ch , CURLOPT_TIMEOUT , 5 );
21-
23+
2224 $ response = curl_exec ($ ch );
2325 $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
24-
25-
26- echo "Sync request complete: HTTP $ http_code\n " ;
26+
27+
28+ $ output [ ' 1 ' ] = "Sync request complete: HTTP $ http_code " ;
2729 return $ response ;
2830}
2931
3032function async_request ($ server ) {
31- echo "Async request start \n" ;
32-
33+ global $ output ;
34+ $ output ['2 ' ] = "Async request start " ;
35+
3336 $ ch = curl_init ();
3437 curl_setopt ($ ch , CURLOPT_URL , "http://localhost: {$ server ->port }/ " );
3538 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
3639 curl_setopt ($ ch , CURLOPT_TIMEOUT , 5 );
37-
40+
3841 $ response = curl_exec ($ ch );
3942 $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
40-
41-
42- echo "Async request complete: HTTP $ http_code\n " ;
43+
44+
45+ $ output [ ' 3 ' ] = "Async request complete: HTTP $ http_code " ;
4346 return $ response ;
4447}
4548
46- echo "Test start \n " ;
49+ $ output [ ' 4 ' ] = "Test start " ;
4750
4851// First, make a synchronous request (not in coroutine)
4952$ sync_result = sync_request ($ server );
50- echo "Sync result: $ sync_result\n " ;
53+ $ output [ ' 5 ' ] = "Sync result: $ sync_result " ;
5154
5255// Then make an async request (in coroutine)
5356$ async_coroutine = spawn (fn () => async_request ($ server ));
5457$ async_result = await ($ async_coroutine );
55- echo "Async result: $ async_result\n " ;
58+ $ output [ ' 6 ' ] = "Async result: $ async_result " ;
5659
5760// Mix them together
58- echo "Mixed execution start \n " ;
61+ $ output [ ' 7 ' ] = "Mixed execution start " ;
5962$ mixed_coroutine = spawn (function () use ($ server ) {
60- echo "In coroutine: making async request \n" ;
63+ global $ output ;
64+ $ output ['8 ' ] = "In coroutine: making async request " ;
6165 return async_request ($ server );
6266});
6367
6468// Make sync request while coroutine is running
65- echo "Making sync request while coroutine runs \n " ;
69+ $ output [ ' 9 ' ] = "Making sync request while coroutine runs " ;
6670$ sync_while_async = sync_request ($ server );
6771
6872$ mixed_result = await ($ mixed_coroutine );
6973
70- echo "Sync while async: $ sync_while_async\n " ;
71- echo "Mixed async result: $ mixed_result\n " ;
74+ $ output [ ' 10 ' ] = "Sync while async: $ sync_while_async " ;
75+ $ output [ ' 11 ' ] = "Mixed async result: $ mixed_result " ;
7276
73- echo "Test end \n" ;
77+ $ output ['12 ' ] = "Test end " ;
78+
79+ // Sort and output
80+ ksort ($ output );
81+ foreach ($ output as $ msg ) {
82+ echo "$ msg \n" ;
83+ }
7484
7585async_test_server_stop ($ server );
7686?>
7787--EXPECT--
78- Test start
7988Sync request start
8089Sync request complete: HTTP 200
81- Sync result: Hello World
8290Async request start
8391Async request complete: HTTP 200
92+ Test start
93+ Sync result: Hello World
8494Async result: Hello World
8595Mixed execution start
86- Making sync request while coroutine runs
87- Sync request start
8896In coroutine: making async request
89- Async request start
90- Sync request complete: HTTP 200
91- Async request complete: HTTP 200
97+ Making sync request while coroutine runs
9298Sync while async: Hello World
9399Mixed async result: Hello World
94100Test end
0 commit comments