@@ -13,7 +13,7 @@ function silencedYield(): int {
1313 return $ level ;
1414}
1515
16- check ( extension_loaded ('swoole ' ), ' Swoole missing ' );
16+ $ hasSwoole = extension_loaded ('swoole ' );
1717check (!function_exists ('swoole_call_array_method ' ), 'stdext still enabled ' );
1818$ disabledJit = ['enabled ' => false , 'on ' => false , 'buffer_size ' => 0 , 'buffer_free ' => 0 ];
1919$ before = opcache_get_status (false )['jit ' ] ?? $ disabledJit ;
@@ -27,62 +27,66 @@ function silencedYield(): int {
2727 maybeExit (false );
2828}
2929$ after = opcache_get_status (false )['jit ' ] ?? $ disabledJit ;
30- $ events = [];
31- Swoole \Coroutine \run (function () use (&$ events ): void {
32- $ channel = new Swoole \Coroutine \Channel (1 );
33- Swoole \Coroutine ::create (function () use ($ channel ): void {
30+ function checkCoroutines (): array {
31+ $ events = [];
32+ Swoole \Coroutine \run (function () use (&$ events ): void {
33+ $ channel = new Swoole \Coroutine \Channel (1 );
34+ Swoole \Coroutine ::create (function () use ($ channel ): void {
35+ for ($ i = 0 ; $ i < 100 ; $ i ++) {
36+ $ channel ->push (numeric ($ i % 10 ));
37+ Swoole \Coroutine ::sleep (0.001 );
38+ }
39+ });
40+ $ total = 0 ;
3441 for ($ i = 0 ; $ i < 100 ; $ i ++) {
35- $ channel ->push (numeric ($ i % 10 ));
42+ $ total += $ channel ->pop ();
43+ }
44+ check ($ total === array_sum (array_map ('numeric ' , array_map (fn ($ i ) => $ i % 10 , range (0 , 99 )))), 'channel result mismatch ' );
45+ $ events [] = 'channel ' ;
46+ try {
3647 Swoole \Coroutine ::sleep (0.001 );
48+ throw new LogicException ('after-yield ' );
49+ } catch (LogicException $ e ) {
50+ check ($ e ->getMessage () === 'after-yield ' , 'exception changed ' );
51+ $ events [] = 'exception ' ;
52+ }
53+ $ normal = error_reporting ();
54+ @silencedYield ();
55+ check (error_reporting () === $ normal , 'error_reporting not restored ' );
56+ $ events [] = 'silence ' ;
57+ $ server = new Swoole \Coroutine \Socket (AF_INET , SOCK_STREAM , 0 );
58+ check ($ server ->bind ('127.0.0.1 ' , 0 ) && $ server ->listen (), 'TCP listen failed ' );
59+ $ port = $ server ->getsockname ()['port ' ];
60+ Swoole \Coroutine ::create (function () use ($ server ): void {
61+ $ peer = $ server ->accept (3 );
62+ check ($ peer !== false , 'TCP accept failed ' );
63+ check ($ peer ->recvAll (4 , 3 ) === 'ping ' , 'TCP receive failed ' );
64+ check ($ peer ->sendAll ('pong ' ) === 4 , 'TCP send failed ' );
65+ $ peer ->close ();
66+ $ server ->close ();
67+ });
68+ $ client = new Swoole \Coroutine \Socket (AF_INET , SOCK_STREAM , 0 );
69+ check ($ client ->connect ('127.0.0.1 ' , $ port , 3 ), 'TCP connect failed ' );
70+ $ client ->sendAll ('ping ' );
71+ check ($ client ->recvAll (4 , 3 ) === 'pong ' , 'TCP round trip failed ' );
72+ $ client ->close ();
73+ $ events [] = 'tcp ' ;
74+ try {
75+ maybeExit (true );
76+ throw new RuntimeException ('exit did not throw ' );
77+ } catch (Swoole \ExitException $ e ) {
78+ check ($ e ->getStatus () === 37 , 'coroutine exit status changed ' );
79+ check (($ e ->getFlags () & SWOOLE_EXIT_IN_COROUTINE ) !== 0 , 'coroutine exit flag missing ' );
80+ $ events [] = 'exit ' ;
3781 }
3882 });
39- $ total = 0 ;
40- for ($ i = 0 ; $ i < 100 ; $ i ++) {
41- $ total += $ channel ->pop ();
42- }
43- check ($ total === array_sum (array_map ('numeric ' , array_map (fn ($ i ) => $ i % 10 , range (0 , 99 )))), 'channel result mismatch ' );
44- $ events [] = 'channel ' ;
45- try {
46- Swoole \Coroutine ::sleep (0.001 );
47- throw new LogicException ('after-yield ' );
48- } catch (LogicException $ e ) {
49- check ($ e ->getMessage () === 'after-yield ' , 'exception changed ' );
50- $ events [] = 'exception ' ;
51- }
52- $ normal = error_reporting ();
53- @silencedYield ();
54- check (error_reporting () === $ normal , 'error_reporting not restored ' );
55- $ events [] = 'silence ' ;
56- $ server = new Swoole \Coroutine \Socket (AF_INET , SOCK_STREAM , 0 );
57- check ($ server ->bind ('127.0.0.1 ' , 0 ) && $ server ->listen (), 'TCP listen failed ' );
58- $ port = $ server ->getsockname ()['port ' ];
59- Swoole \Coroutine ::create (function () use ($ server ): void {
60- $ peer = $ server ->accept (3 );
61- check ($ peer !== false , 'TCP accept failed ' );
62- check ($ peer ->recvAll (4 , 3 ) === 'ping ' , 'TCP receive failed ' );
63- check ($ peer ->sendAll ('pong ' ) === 4 , 'TCP send failed ' );
64- $ peer ->close ();
65- $ server ->close ();
66- });
67- $ client = new Swoole \Coroutine \Socket (AF_INET , SOCK_STREAM , 0 );
68- check ($ client ->connect ('127.0.0.1 ' , $ port , 3 ), 'TCP connect failed ' );
69- $ client ->sendAll ('ping ' );
70- check ($ client ->recvAll (4 , 3 ) === 'pong ' , 'TCP round trip failed ' );
71- $ client ->close ();
72- $ events [] = 'tcp ' ;
73- try {
74- maybeExit (true );
75- throw new RuntimeException ('exit did not throw ' );
76- } catch (Swoole \ExitException $ e ) {
77- check ($ e ->getStatus () === 37 , 'coroutine exit status changed ' );
78- check (($ e ->getFlags () & SWOOLE_EXIT_IN_COROUTINE ) !== 0 , 'coroutine exit flag missing ' );
79- $ events [] = 'exit ' ;
80- }
81- });
82- check ($ events === ['channel ' , 'exception ' , 'silence ' , 'tcp ' , 'exit ' ], 'coroutine checks incomplete ' );
83+ check ($ events === ['channel ' , 'exception ' , 'silence ' , 'tcp ' , 'exit ' ], 'coroutine checks incomplete ' );
84+ return $ events ;
85+ }
86+ $ events = $ hasSwoole ? checkCoroutines () : [];
8387echo 'JIT_REPORT: ' , json_encode ([
8488 'php ' => PHP_VERSION ,
85- 'swoole ' => swoole_version (),
89+ 'swoole ' => $ hasSwoole ? swoole_version () : null ,
8690 'stdext ' => function_exists ('swoole_call_array_method ' ),
8791 'before ' => $ before ,
8892 'after ' => $ after ,
0 commit comments