test/java/nio/channels/SocketChannel/FinishConnect.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
--- 1,7 ----
/*
! * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*** 34,58 ****
import java.util.*;
public class FinishConnect {
- static final int DAYTIME_PORT = 13;
- static final String DAYTIME_HOST = TestUtil.HOST;
-
public static void main(String[] args) throws Exception {
! test1(true, true);
! test1(true, false);
! test1(false, true);
! test1(false, false);
! test2();
}
! static void test1(boolean select, boolean setBlocking) throws Exception {
! InetSocketAddress isa
! = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
! DAYTIME_PORT);
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(isa);
int attempts = 0;
--- 34,58 ----
import java.util.*;
public class FinishConnect {
public static void main(String[] args) throws Exception {
! try (TestUtil.DayTimeServer dayTimeServer =
! TestUtil.DayTimeServer.startNewServer(100)) {
! test1(dayTimeServer, true, true);
! test1(dayTimeServer, true, false);
! test1(dayTimeServer, false, true);
! test1(dayTimeServer, false, false);
! test2(dayTimeServer);
! }
}
! static void test1(TestUtil.DayTimeServer daytimeServer,
! boolean select, boolean setBlocking) throws Exception {
! InetSocketAddress isa = new InetSocketAddress(daytimeServer.getAddress(),
! daytimeServer.getPort());
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(isa);
int attempts = 0;
*** 107,125 ****
CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
System.err.println(isa + " says: \"" + cb + "\"");
sc.close();
}
! static void test2() throws Exception {
! InetSocketAddress isa
! = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST),
! DAYTIME_PORT);
boolean done = false;
int globalAttempts = 0;
while (!done) {
! if (globalAttempts++ > 50)
throw new RuntimeException("Failed to connect");
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(isa);
int localAttempts = 0;
while (!connected) {
--- 107,136 ----
CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);
System.err.println(isa + " says: \"" + cb + "\"");
sc.close();
}
! static void test2(TestUtil.DayTimeServer daytimeServer) throws Exception {
! InetSocketAddress isa = new InetSocketAddress(daytimeServer.getAddress(),
! daytimeServer.getPort());
boolean done = false;
int globalAttempts = 0;
+ int connectSuccess = 0;
while (!done) {
! // When using a local daytime server it is not always possible
! // to get a pending connection, as sc.connect(isa) may always
! // return true.
! // So we're going to throw the exception only if there was
! // at least 1 case where we did not manage to connect.
! if (globalAttempts++ > 50) {
! if (globalAttempts == connectSuccess + 1) {
! System.err.println("Can't fully test on "
! + System.getProperty("os.name"));
! break;
! }
throw new RuntimeException("Failed to connect");
+ }
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
boolean connected = sc.connect(isa);
int localAttempts = 0;
while (!connected) {
*** 130,139 ****
--- 141,153 ----
done = true;
break;
}
Thread.sleep(10);
}
+ if (connected) {
+ connectSuccess++;
+ }
sc.close();
}
}
}