/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.testsuite.transport.socket;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOption;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Assume;
import org.junit.Test;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class SocketConnectionAttemptTest extends AbstractClientSocketTest {

    private static final String BAD_HOST = SystemPropertyUtil.get("io.netty.testsuite.badHost", "255.255.255.0");

    static {
        InternalLoggerFactory.getInstance(SocketConnectionAttemptTest.class).debug(
                "-Dio.netty.testsuite.badHost: {}", BAD_HOST);
    }

    @Test(timeout = 30000)
    public void testConnectTimeout() throws Throwable {
        run();
    }

    public void testConnectTimeout(Bootstrap cb) throws Throwable {
        TestHandler h = new TestHandler();
        cb.handler(h).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
        ChannelFuture future = cb.connect(BAD_HOST, 8080);
        try {
            assertThat(future.await(3000), is(true));
        } finally {
            future.channel().close();
        }
    }

    @Test
    public void testConnectCancellation() throws Throwable {
        // Check if the test can be executed or should be skipped because of no network/internet connection
        // See https://github.com/netty/netty/issues/1474
        boolean noRoute = false;
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(BAD_HOST, 8080), 10);
        } catch (ConnectException e) {
            noRoute = true;
            // is thrown for no route to host when using Socket connect
        } catch (Exception e) {
            // ignore
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                // ignore
            }
        }

        Assume.assumeFalse(
                "No route to host, so skip the test as it may be because of no network connection ", noRoute);
        run();
    }

    public void testConnectCancellation(Bootstrap cb) throws Throwable {
        TestHandler h = new TestHandler();
        cb.handler(h).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000);
        ChannelFuture future = cb.connect(BAD_HOST, 8080);
        try {
            assertThat(future.await(1000), is(false));
            if (future.cancel(true)) {
                assertThat(future.channel().closeFuture().await(500), is(true));
                assertThat(future.isCancelled(), is(true));
            } else {
                // Cancellation not supported by the transport.
            }
        } finally {
            future.channel().close();
        }
    }

    private static class TestHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
        }
    }
}

@@ -1005,33 +1005,8 @@
 ry;%0A
-import org.junit.Assume;%0A
 impo
@@ -1223,16 +1223,50 @@
 ssert.*;
+%0Aimport static org.junit.Assume.*;
 %0A%0Apublic
@@ -1799,32 +1799,27 @@
         
-TestH
+cb.h
 andler
- h = 
+(
 new Test
@@ -1827,38 +1827,16 @@
 andler()
-;%0A        cb.handler(h
 ).option
@@ -2340,22 +2340,29 @@
 ean 
-noRo
+badHostTimedO
 ut
-e
  = 
-fals
+tru
 e;%0A 
@@ -2538,21 +2538,30 @@
     
-noRo
+badHostTimedO
 ut
-e
  = 
-tru
+fals
 e;%0A 
@@ -2856,138 +2856,112 @@
     
-Assume.assumeFalse(%0A                %22No route to host, so skip the test as it may be because of no network connection %22, noRoute);
+assumeThat(%22The connection attempt to %22 + BAD_HOST + %22 does not time out.%22, badHostTimedOut, is(true));%0A
 %0A   
@@ -3064,24 +3064,19 @@
     
-TestH
+cb.h
 andler
- h = 
+(
 new 
@@ -3092,30 +3092,8 @@
 er()
-;%0A        cb.handler(h
 ).op
@@ -3223,34 +3223,27 @@
             
-assertThat
+if 
 (future.awai
@@ -3253,21 +3253,241 @@
 000)
-, is(false));
+) %7B%0A                if (future.isSuccess()) %7B%0A                    fail(%22A connection attempt to %22 + BAD_HOST + %22 must not succeed.%22);%0A                %7D else %7B%0A                    throw future.cause();%0A                %7D%0A            %7D%0A
 %0A   
@@ -4046,30 +4046,133 @@
     
-cause.printStackTrace(
+InternalLoggerFactory.getInstance(%0A                    SocketConnectionAttemptTest.class).warn(%22Unexpected exception:%22, cause
 );%0A 

Add more diagnostics to SocketConnectionAttemptTest to fix the flaky test / Cleanup