@@ -100,6 +100,39 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded()
100100 Block \await ($ connector ->connect ('127.0.0.1:9999 ' ), $ loop , self ::TIMEOUT );
101101 }
102102
103+ /** @test */
104+ public function connectionToInvalidNetworkShouldFailWithUnreachableError ()
105+ {
106+ if (!defined ('SOCKET_ENETUNREACH ' ) || !function_exists ('socket_import_stream ' )) {
107+ $ this ->markTestSkipped ('Test requires ext-socket on PHP 5.4+ ' );
108+ }
109+
110+ // 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+ $ address = 'tcp://192.168. ' . mt_rand (0 , 255 ) . '. ' . mt_rand (1 , 254 ) . ':8123 ' ;
114+ $ client = @stream_socket_client ($ address , $ errno , $ errstr , 0.1 * $ i );
115+ if ($ errno === SOCKET_ENETUNREACH ) {
116+ break ;
117+ }
118+ }
119+ if ($ client || $ errno !== SOCKET_ENETUNREACH ) {
120+ $ this ->markTestSkipped ('Expected error ' . SOCKET_ENETUNREACH . ' but got ' . $ errno . ' ( ' . $ errstr . ') for ' . $ address );
121+ }
122+
123+ $ loop = Factory::create ();
124+ $ connector = new TcpConnector ($ loop );
125+
126+ $ promise = $ connector ->connect ($ address );
127+
128+ $ this ->setExpectedException (
129+ 'RuntimeException ' ,
130+ 'Connection to ' . $ address . ' failed: ' . socket_strerror (SOCKET_ENETUNREACH ),
131+ SOCKET_ENETUNREACH
132+ );
133+ Block \await ($ promise , $ loop , self ::TIMEOUT );
134+ }
135+
103136 /** @test */
104137 public function connectionToTcpServerShouldSucceedWithRemoteAdressSameAsTarget ()
105138 {
0 commit comments