@@ -103,21 +103,29 @@ 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
113
$ errno = 0 ; $ errstr = '' ;
112
- for ($ i = 0 ; $ i < 20 ; ++$ i ) {
114
+ for ($ i = 0 ; $ i < 10 && $ errno !== $ enetunreach ; ++$ i ) {
113
115
$ address = 'tcp://192.168. ' . mt_rand (0 , 255 ) . '. ' . mt_rand (1 , 254 ) . ':8123 ' ;
114
116
$ client = @stream_socket_client ($ address , $ errno , $ errstr , 0.1 * $ i );
115
- if ($ errno === SOCKET_ENETUNREACH ) {
116
- break ;
117
- }
118
117
}
119
- if ($ client || $ errno !== SOCKET_ENETUNREACH ) {
120
- $ this ->markTestSkipped ('Expected error ' . SOCKET_ENETUNREACH . ' but got ' . $ errno . ' ( ' . $ errstr . ') for ' . $ address );
118
+ for ($ i = 0 ; $ i < 10 && $ errno !== $ enetunreach ; ++$ i ) {
119
+ $ address = 'tcp://172. ' . mt_rand (16 , 31 ) . '. ' . mt_rand (0 , 255 ) . '. ' . mt_rand (1 , 254 ) . ':8123 ' ;
120
+ $ client = @stream_socket_client ($ address , $ errno , $ errstr , 0.1 * $ i );
121
+ }
122
+ for ($ i = 0 ; $ i < 10 && $ errno !== $ enetunreach ; ++$ i ) {
123
+ $ address = 'tcp://10. ' . mt_rand (0 , 255 ) . '. ' . mt_rand (0 , 255 ) . '. ' . mt_rand (1 , 254 ) . ':8123 ' ;
124
+ $ client = @stream_socket_client ($ address , $ errno , $ errstr , 0.1 * $ i );
125
+ }
126
+ if ($ client || $ errno !== $ enetunreach ) {
127
+ passthru ('ip route list ' );
128
+ $ this ->markTestSkipped ('Expected error ' . $ enetunreach . ' but got ' . $ errno . ' ( ' . $ errstr . ') for ' . $ address );
121
129
}
122
130
123
131
$ loop = Factory::create ();
@@ -127,8 +135,8 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError()
127
135
128
136
$ this ->setExpectedException (
129
137
'RuntimeException ' ,
130
- 'Connection to ' . $ address . ' failed: ' . socket_strerror ( SOCKET_ENETUNREACH ),
131
- SOCKET_ENETUNREACH
138
+ 'Connection to ' . $ address . ' failed: ' . ( function_exists ( ' socket_strerror ' ) ? socket_strerror ( $ enetunreach ) : ' Network is unreachable ' ),
139
+ $ enetunreach
132
140
);
133
141
Block \await ($ promise , $ loop , self ::TIMEOUT );
134
142
}
0 commit comments