@@ -103,21 +103,21 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded()
103
103
/** @test */
104
104
public function connectionToInvalidNetworkShouldFailWithUnreachableError ()
105
105
{
106
- if (! defined ( ' SOCKET_ENETUNREACH ' ) || !function_exists ('socket_import_stream ' )) {
107
- $ this ->markTestSkipped ('Test requires ext-socket on PHP 5.4+ ' );
106
+ if (PHP_OS !== ' Linux ' && !function_exists ('socket_import_stream ' )) {
107
+ $ this ->markTestSkipped ('Test requires either Linux or ext-sockets on PHP 5.4+ ' );
108
108
}
109
109
110
+ $ enetunreach = defined ('SOCKET_ENETUNREACH ' ) ? SOCKET_ENETUNREACH : 101 ;
111
+
110
112
// try to find an unreachable network by trying a couple of private network addresses
111
- $ errno = 0 ; $ errstr = '' ;
112
- for ($ i = 0 ; $ i < 20 ; ++$ i ) {
113
+ $ errno = 0 ;
114
+ $ errstr = '' ;
115
+ for ($ i = 0 ; $ i < 20 && $ errno !== $ enetunreach ; ++$ i ) {
113
116
$ address = 'tcp://192.168. ' . mt_rand (0 , 255 ) . '. ' . mt_rand (1 , 254 ) . ':8123 ' ;
114
117
$ client = @stream_socket_client ($ address , $ errno , $ errstr , 0.1 * $ i );
115
- if ($ errno === SOCKET_ENETUNREACH ) {
116
- break ;
117
- }
118
118
}
119
- if ($ client || $ errno !== SOCKET_ENETUNREACH ) {
120
- $ this ->markTestSkipped ('Expected error ' . SOCKET_ENETUNREACH . ' but got ' . $ errno . ' ( ' . $ errstr . ') for ' . $ address );
119
+ if ($ client || $ errno !== $ enetunreach ) {
120
+ $ this ->markTestSkipped ('Expected error ' . $ enetunreach . ' but got ' . $ errno . ' ( ' . $ errstr . ') for ' . $ address );
121
121
}
122
122
123
123
$ loop = Factory::create ();
@@ -127,8 +127,8 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError()
127
127
128
128
$ this ->setExpectedException (
129
129
'RuntimeException ' ,
130
- 'Connection to ' . $ address . ' failed: ' . socket_strerror ( SOCKET_ENETUNREACH ),
131
- SOCKET_ENETUNREACH
130
+ 'Connection to ' . $ address . ' failed: ' . ( function_exists ( ' socket_strerror ' ) ? socket_strerror ( $ enetunreach ) : ' Network is unreachable ' ),
131
+ $ enetunreach
132
132
);
133
133
Block \await ($ promise , $ loop , self ::TIMEOUT );
134
134
}
0 commit comments