use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.
the class DatagramChannelTest method test_socket_IllegalBlockingModeException.
/**
* @tests DatagramChannel#socket()
*/
public void test_socket_IllegalBlockingModeException() throws Exception {
// regression test for Harmony-1036
DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
DatagramSocket socket = channel.socket();
try {
socket.send(null);
fail("should throw IllegalBlockingModeException");
} catch (IllegalBlockingModeException e) {
// expected
}
try {
socket.receive(null);
fail("should throw IllegalBlockingModeException");
} catch (IllegalBlockingModeException e) {
// expected
}
}
use of java.nio.channels.IllegalBlockingModeException in project XobotOS by xamarin.
the class AbstractSelectableChannel method register.
/**
* Registers this channel with the specified selector for the specified
* interest set. If the channel is already registered with the selector, the
* {@link SelectionKey interest set} is updated to {@code interestSet} and
* the corresponding selection key is returned. If the channel is not yet
* registered, this method calls the {@code register} method of
* {@code selector} and adds the selection key to this channel's key set.
*
* @param selector
* the selector with which to register this channel.
* @param interestSet
* this channel's {@link SelectionKey interest set}.
* @param attachment
* the object to attach, can be {@code null}.
* @return the selection key for this registration.
* @throws CancelledKeyException
* if this channel is registered but its key has been canceled.
* @throws ClosedChannelException
* if this channel is closed.
* @throws IllegalArgumentException
* if {@code interestSet} is not supported by this channel.
* @throws IllegalBlockingModeException
* if this channel is in blocking mode.
* @throws IllegalSelectorException
* if this channel does not have the same provider as the given
* selector.
*/
@Override
public final SelectionKey register(Selector selector, int interestSet, Object attachment) throws ClosedChannelException {
if (!isOpen()) {
throw new ClosedChannelException();
}
if (!((interestSet & ~validOps()) == 0)) {
throw new IllegalArgumentException();
}
synchronized (blockingLock) {
if (isBlocking) {
throw new IllegalBlockingModeException();
}
if (!selector.isOpen()) {
if (interestSet == 0) {
// throw ISE exactly to keep consistency
throw new IllegalSelectorException();
}
// throw NPE exactly to keep consistency
throw new NullPointerException();
}
SelectionKey key = keyFor(selector);
if (key == null) {
key = ((AbstractSelector) selector).register(this, interestSet, attachment);
keyList.add(key);
} else {
if (!key.isValid()) {
throw new CancelledKeyException();
}
key.interestOps(interestSet);
key.attach(attachment);
}
return key;
}
}
use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.
the class OldSocketTest method test_connectLjava_net_SocketAddress.
public void test_connectLjava_net_SocketAddress() throws Exception {
// needed for some tests
class mySocketAddress extends SocketAddress {
public mySocketAddress() {
}
}
class SocketCloser extends Thread {
int timeout = 0;
Socket theSocket = null;
public void run() {
try {
Thread.sleep(timeout);
theSocket.close();
} catch (Exception e) {
}
;
return;
}
public SocketCloser(int timeout, Socket theSocket) {
this.timeout = timeout;
this.theSocket = theSocket;
}
}
// start by validating the error checks
int portNumber = 0;
Socket theSocket = null;
ServerSocket serverSocket = null;
SocketAddress theAddress = null;
SocketAddress nonConnectableAddress = null;
SocketAddress nonReachableAddress = null;
SocketAddress invalidType = null;
// byte[] theBytes = {-1,-1,-1,-1};
byte[] theBytes = { 0, 0, 0, 0 };
theAddress = new InetSocketAddress(InetAddress.getLocalHost(), portNumber);
nonConnectableAddress = new InetSocketAddress(InetAddress.getByAddress(theBytes), portNumber);
nonReachableAddress = new InetSocketAddress(InetAddress.getByName(Support_Configuration.ResolvedNotExistingHost), portNumber);
invalidType = new mySocketAddress();
try {
theSocket = new Socket();
theSocket.connect(null);
fail("No exception after null address passed in");
} catch (Exception e) {
assertTrue("Wrong exception null address passed in: " + e.toString(), (e instanceof IllegalArgumentException));
}
try {
theSocket = new Socket();
theSocket.connect(invalidType);
fail("No exception when invalid socket address type passed in: ");
} catch (Exception e) {
assertTrue("Wrong exception when when invalid socket address type passed in: " + e.toString(), (e instanceof IllegalArgumentException));
}
try {
theSocket = new Socket();
theSocket.connect(nonConnectableAddress);
fail("No exception when non Connectable Address passed in: ");
} catch (Exception e) {
assertTrue("Wrong exception when non Connectable Address passed in: " + e.toString(), (e instanceof ConnectException));
}
// an address on which nobody is listening
try {
theSocket = new Socket();
theSocket.connect(theAddress);
theSocket.close();
fail("No exception when connecting to address nobody listening on: ");
} catch (Exception e) {
assertTrue("Wrong exception when connecting to address nobody listening on: " + e.toString(), (e instanceof ConnectException));
}
// now validate that we can actually connect when somebody is listening
theSocket = new Socket();
serverSocket = new ServerSocket(0, 5);
theSocket.connect(serverSocket.getLocalSocketAddress());
// validate that when a socket is connected that it answers
// correctly to related queries
assertTrue("Socket did not returned connected when it is: ", theSocket.isConnected());
assertFalse("Socket returned closed when it should be connected ", theSocket.isClosed());
assertTrue("Socket returned not bound when it should be: ", theSocket.isBound());
assertFalse("Socket returned input Shutdown when it should be connected ", theSocket.isInputShutdown());
assertFalse("Socket returned output Shutdown when it should be connected ", theSocket.isOutputShutdown());
assertTrue("Local port on connected socket was 0", theSocket.getLocalPort() != 0);
theSocket.close();
serverSocket.close();
// are already connected
try {
theSocket = new Socket();
serverSocket = new ServerSocket(0, 5);
theSocket.connect(serverSocket.getLocalSocketAddress());
theSocket.connect(serverSocket.getLocalSocketAddress());
theSocket.close();
serverSocket.close();
fail("No exception when we try to connect on a connected socket: ");
} catch (Exception e) {
assertTrue("Wrong exception when connecting on socket that is allready connected" + e.toString(), (e instanceof SocketException));
assertFalse("Wrong exception when connecting on socket that is allready connected" + e.toString(), (e instanceof SocketTimeoutException));
try {
theSocket.close();
serverSocket.close();
} catch (Exception e2) {
}
}
// now validate that connected socket can be used to read/write
theSocket = new Socket();
serverSocket = new ServerSocket(0, 5);
theSocket.connect(serverSocket.getLocalSocketAddress());
Socket servSock = serverSocket.accept();
InputStream theInput = theSocket.getInputStream();
OutputStream theOutput = servSock.getOutputStream();
InputStream theInput2 = servSock.getInputStream();
OutputStream theOutput2 = theSocket.getOutputStream();
String sendString = new String("Test");
theOutput.write(sendString.getBytes());
theOutput.flush();
Thread.sleep(1000);
int totalBytesRead = 0;
byte[] myBytes = new byte[100];
while (theInput.available() > 0) {
int bytesRead = theInput.read(myBytes, totalBytesRead, myBytes.length - totalBytesRead);
totalBytesRead = totalBytesRead + bytesRead;
}
String receivedString = new String(myBytes, 0, totalBytesRead);
assertTrue("Could not recv on socket connected with timeout:" + receivedString + ":" + sendString, receivedString.equals(sendString));
sendString = new String("SEND - Test");
theOutput2.write(sendString.getBytes());
theOutput2.flush();
Thread.sleep(1000);
totalBytesRead = 0;
myBytes = new byte[100];
while (theInput2.available() > 0) {
int bytesRead = theInput2.read(myBytes, totalBytesRead, myBytes.length - totalBytesRead);
totalBytesRead = totalBytesRead + bytesRead;
}
receivedString = new String(myBytes, 0, totalBytesRead);
assertTrue("Could not send on socket connected with timeout:" + receivedString + ":" + sendString, receivedString.equals(sendString));
theSocket.close();
serverSocket.close();
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
Socket socket = channel.socket();
try {
socket.connect(serverSocket.getLocalSocketAddress());
fail("IllegalBlockingModeException was not thrown.");
} catch (IllegalBlockingModeException expected) {
}
socket.close();
}
use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.
the class OldSocketTest method test_connectLjava_net_SocketAddressI.
public void test_connectLjava_net_SocketAddressI() throws Exception {
// needed for some tests
class mySocketAddress extends SocketAddress {
public mySocketAddress() {
}
}
class SocketCloser extends Thread {
int timeout = 0;
Socket theSocket = null;
public void run() {
try {
Thread.sleep(timeout);
theSocket.close();
} catch (Exception e) {
}
return;
}
public SocketCloser(int timeout, Socket theSocket) {
this.timeout = timeout;
this.theSocket = theSocket;
}
}
class SocketConnector extends Thread {
int timeout = 0;
Socket theSocket = null;
SocketAddress address = null;
public void run() {
try {
theSocket.connect(address, timeout);
} catch (Exception e) {
}
return;
}
public SocketConnector(int timeout, Socket theSocket, SocketAddress address) {
this.timeout = timeout;
this.theSocket = theSocket;
this.address = address;
}
}
// start by validating the error checks
byte[] theBytes = { 0, 0, 0, 0 };
SocketAddress theAddress = new InetSocketAddress(InetAddress.getLocalHost(), 0);
SocketAddress nonConnectableAddress = new InetSocketAddress(InetAddress.getByAddress(theBytes), 0);
SocketAddress nonReachableAddress = new InetSocketAddress(InetAddress.getByName(Support_Configuration.ResolvedNotExistingHost), 0);
SocketAddress invalidType = new mySocketAddress();
Socket theSocket = null;
ServerSocket serverSocket = null;
try {
theSocket = new Socket();
theSocket.connect(theAddress, -100);
fail("No exception after negative timeout passed in");
} catch (Exception e) {
assertTrue("Wrong exception when negative timeout passed in: " + e.toString(), (e instanceof IllegalArgumentException));
}
try {
theSocket = new Socket();
theSocket.connect(null, 0);
fail("No exception after null address passed in");
} catch (Exception e) {
assertTrue("Wrong exception null address passed in: " + e.toString(), (e instanceof IllegalArgumentException));
}
try {
theSocket = new Socket();
theSocket.connect(invalidType, 100000);
fail("No exception when invalid socket address type passed in: ");
} catch (Exception e) {
assertTrue("Wrong exception when when invalid socket address type passed in: " + e.toString(), (e instanceof IllegalArgumentException));
}
try {
theSocket = new Socket();
theSocket.connect(nonConnectableAddress, 100000);
fail("No exception when non Connectable Address passed in: ");
} catch (Exception e) {
assertTrue("Wrong exception when non Connectable Address passed in: " + e.toString(), (e instanceof SocketException));
}
// an address on which nobody is listening
try {
theSocket = new Socket();
theSocket.connect(theAddress, 0);
theSocket.close();
fail("No timeout:No exception when connecting to address nobody listening on: ");
} catch (Exception e) {
assertTrue("No timeout:Wrong exception when connecting to address nobody listening on: " + e.toString(), (e instanceof ConnectException));
}
// now validate that we can actually connect when somebody is listening
theSocket = new Socket();
serverSocket = new ServerSocket(0, 5);
theSocket.connect(serverSocket.getLocalSocketAddress());
theSocket.close();
serverSocket.close();
// an address on which nobody is listening
try {
theSocket = new Socket();
theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 80), 100000);
theSocket.close();
fail("No exception when connecting to address nobody listening on: ");
} catch (Exception e) {
assertTrue("Wrong exception when connecting to address nobody listening on: " + e.toString(), (e instanceof ConnectException));
}
// timeout expired
try {
theSocket = new Socket();
theSocket.connect(nonReachableAddress, 200);
theSocket.close();
fail("No interrupted exception when connecting to address nobody listening on with short timeout 200: ");
} catch (Exception e) {
assertTrue("Wrong exception when connecting to address nobody listening on with short timeout 200: " + e.toString(), (e instanceof SocketTimeoutException));
}
// timeout expired
try {
theSocket = new Socket();
theSocket.connect(nonReachableAddress, 40);
theSocket.close();
fail("No interrupted exception when connecting to address nobody listening on with short timeout 40: ");
} catch (Exception e) {
assertTrue("Wrong exception when connecting to address nobody listening on with short timeout 40: " + e.toString(), (e instanceof SocketTimeoutException));
}
// now validate that we can actually connect when somebody is listening
theSocket = new Socket();
serverSocket = new ServerSocket(0, 5);
theSocket.connect(serverSocket.getLocalSocketAddress());
// validate that when a socket is connected that it answers
// correctly to related queries
assertTrue("Socket did not returned connected when it is: ", theSocket.isConnected());
assertFalse("Socket returned closed when it should be connected ", theSocket.isClosed());
assertTrue("Socket returned not bound when it should be: ", theSocket.isBound());
assertFalse("Socket returned input Shutdown when it should be connected ", theSocket.isInputShutdown());
assertFalse("Socket returned output Shutdown when it should be connected ", theSocket.isOutputShutdown());
assertTrue("Local port on connected socket was 0", theSocket.getLocalPort() != 0);
theSocket.close();
serverSocket.close();
// are already connected
try {
theSocket = new Socket();
serverSocket = new ServerSocket();
serverSocket.bind(theAddress);
theSocket.connect(theAddress, 100000);
theSocket.connect(theAddress, 100000);
theSocket.close();
serverSocket.close();
fail("No exception when we try to connect on a connected socket: ");
} catch (Exception e) {
assertTrue("Wrong exception when connecting on socket that is already connected" + e.toString(), (e instanceof SocketException));
assertFalse("Wrong exception when connecting on socket that is already connected" + e.toString(), (e instanceof SocketTimeoutException));
try {
theSocket.close();
serverSocket.close();
} catch (Exception e2) {
}
}
// now validate that connected socket can be used to read/write
theSocket = new Socket();
serverSocket = new ServerSocket(0, 5);
theSocket.connect(serverSocket.getLocalSocketAddress());
Socket servSock = serverSocket.accept();
InputStream theInput = theSocket.getInputStream();
OutputStream theOutput = servSock.getOutputStream();
InputStream theInput2 = servSock.getInputStream();
OutputStream theOutput2 = theSocket.getOutputStream();
String sendString = new String("Test");
theOutput.write(sendString.getBytes());
theOutput.flush();
Thread.sleep(1000);
int totalBytesRead = 0;
byte[] myBytes = new byte[100];
while (theInput.available() > 0) {
int bytesRead = theInput.read(myBytes, totalBytesRead, myBytes.length - totalBytesRead);
totalBytesRead = totalBytesRead + bytesRead;
}
String receivedString = new String(myBytes, 0, totalBytesRead);
assertTrue("Could not recv on socket connected with timeout:" + receivedString + ":" + sendString, receivedString.equals(sendString));
sendString = new String("SEND - Test");
theOutput2.write(sendString.getBytes());
theOutput2.flush();
totalBytesRead = 0;
myBytes = new byte[100];
Thread.sleep(1000);
while (theInput2.available() > 0) {
int bytesRead = theInput2.read(myBytes, totalBytesRead, myBytes.length - totalBytesRead);
totalBytesRead = totalBytesRead + bytesRead;
}
receivedString = new String(myBytes, 0, totalBytesRead);
assertTrue("Could not send on socket connected with timeout:" + receivedString + ":" + sendString, receivedString.equals(sendString));
theSocket.close();
serverSocket.close();
// now try to set options while we are connecting
theSocket = new Socket();
SocketConnector connector = new SocketConnector(5000, theSocket, nonReachableAddress);
connector.start();
theSocket.setSoTimeout(1000);
Thread.sleep(10);
assertEquals("Socket option not set during connect: 10 ", 1000, theSocket.getSoTimeout());
Thread.sleep(50);
theSocket.setSoTimeout(2000);
assertEquals("Socket option not set during connect: 50 ", 2000, theSocket.getSoTimeout());
Thread.sleep(5000);
theSocket.close();
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
Socket socket = channel.socket();
try {
socket.connect(serverSocket.getLocalSocketAddress());
fail("IllegalBlockingModeException was not thrown.");
} catch (IllegalBlockingModeException expected) {
}
channel.close();
}
use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.
the class OldServerSocketTest method test_accept.
public void test_accept() throws IOException {
ServerSocket newSocket = new ServerSocket(0);
newSocket.setSoTimeout(500);
try {
Socket accepted = newSocket.accept();
fail("SocketTimeoutException was not thrown: " + accepted);
} catch (SocketTimeoutException expected) {
}
newSocket.close();
ServerSocketChannel ssc = ServerSocketChannel.open();
ServerSocket ss = ssc.socket();
try {
ss.accept();
fail("IllegalBlockingModeException was not thrown.");
} catch (IllegalBlockingModeException ibme) {
//expected
} finally {
ss.close();
ssc.close();
}
}
Aggregations