use of java.nio.channels.AsynchronousCloseException in project hadoop by apache.
the class TestDomainSocket method testSocketAcceptAndClose.
/**
* Test that if one thread is blocking in a read or write operation, another
* thread can close the socket and stop the accept.
*
* @throws IOException
*/
@Test(timeout = 180000)
public void testSocketAcceptAndClose() throws Exception {
final String TEST_PATH = new File(sockDir.getDir(), "test_sock_accept_and_close").getAbsolutePath();
final DomainSocket serv = DomainSocket.bindAndListen(TEST_PATH);
ExecutorService exeServ = Executors.newSingleThreadExecutor();
Callable<Void> callable = new Callable<Void>() {
public Void call() {
try {
serv.accept();
throw new RuntimeException("expected the accept() to be " + "interrupted and fail");
} catch (AsynchronousCloseException e) {
return null;
} catch (IOException e) {
throw new RuntimeException("unexpected IOException", e);
}
}
};
Future<Void> future = exeServ.submit(callable);
Thread.sleep(500);
serv.close();
future.get(2, TimeUnit.MINUTES);
}
use of java.nio.channels.AsynchronousCloseException in project Openfire by igniterealtime.
the class BlockingReadingMode method run.
/**
* A dedicated thread loop for reading the stream and sending incoming
* packets to the appropriate router.
*/
@Override
public void run() {
try {
socketReader.reader.getXPPParser().setInput(new InputStreamReader(ServerTrafficCounter.wrapInputStream(socket.getInputStream()), CHARSET));
// Read in the opening tag and prepare for packet stream
try {
socketReader.createSession();
} catch (IOException e) {
Log.debug("Error creating session", e);
throw e;
}
// Read the packet stream until it ends
if (socketReader.session != null) {
readStream();
}
} catch (EOFException eof) {
// Normal disconnect
} catch (SocketException se) {
// The socket was closed. The server may close the connection for several
// reasons (e.g. user requested to remove his account). Do nothing here.
} catch (AsynchronousCloseException ace) {
// The socket was closed.
} catch (XmlPullParserException ie) {
// It is normal for clients to abruptly cut a connection
// rather than closing the stream document. Since this is
// normal behavior, we won't log it as an error.
// Log.error(LocaleUtils.getLocalizedString("admin.disconnect"),ie);
} catch (Exception e) {
if (socketReader.session != null) {
Log.warn(LocaleUtils.getLocalizedString("admin.error.stream") + ". Session: " + socketReader.session, e);
}
} finally {
if (socketReader.session != null) {
if (Log.isDebugEnabled()) {
Log.debug("Logging off " + socketReader.session.getAddress() + " on " + socketReader.connection);
}
try {
socketReader.session.close();
} catch (Exception e) {
Log.warn(LocaleUtils.getLocalizedString("admin.error.connection") + socket.toString());
}
} else {
// Close and release the created connection
socketReader.connection.close();
Log.debug(LocaleUtils.getLocalizedString("admin.error.connection") + socket.toString());
}
socketReader.shutdown();
}
}
use of java.nio.channels.AsynchronousCloseException in project robovm by robovm.
the class ServerSocketChannelTest method testAccept_Block_NoConnect.
public void testAccept_Block_NoConnect() throws IOException {
assertTrue(this.serverChannel.isBlocking());
ServerSocket gotSocket = this.serverChannel.socket();
gotSocket.bind(localAddr1);
// blocking mode , will block and wait for ever...
// so must close the server channel with another thread.
new Thread() {
public void run() {
try {
Thread.sleep(TIME_UNIT);
ServerSocketChannelTest.this.serverChannel.close();
} catch (Exception e) {
fail("Fail to close the server channel because of" + e.getClass().getName());
}
}
}.start();
try {
this.serverChannel.accept();
fail("Should throw a AsynchronousCloseException");
} catch (AsynchronousCloseException e) {
// OK.
}
}
use of java.nio.channels.AsynchronousCloseException in project robovm by robovm.
the class DatagramChannelTest method testReadWrite_configureBlock.
// -------------------------------------------------------------------
// test read and write
// -------------------------------------------------------------------
public void testReadWrite_configureBlock() throws Exception {
byte[] targetArray = new byte[2];
// bind and connect
this.channel1.socket().bind(localAddr2);
this.channel1.connect(localAddr1);
this.channel2.socket().bind(localAddr1);
this.channel2.connect(localAddr2);
ByteBuffer targetBuf = ByteBuffer.wrap(targetArray);
new Thread() {
public void run() {
try {
Thread.sleep(TIME_UNIT);
channel1.configureBlocking(false);
channel1.close();
} catch (Exception e) {
//ignore
}
}
}.start();
try {
this.channel1.read(targetBuf);
fail("should throw AsynchronousCloseException");
} catch (AsynchronousCloseException e) {
// ok
}
}
use of java.nio.channels.AsynchronousCloseException in project robovm by robovm.
the class ConcurrentCloseTest method test_connect_nonBlocking.
public void test_connect_nonBlocking() throws Exception {
StuckServer ss = new StuckServer(false);
SocketChannel s = SocketChannel.open();
new Killer(s.socket()).start();
try {
System.err.println("connect (non-blocking)...");
s.configureBlocking(false);
s.connect(ss.getLocalSocketAddress());
while (!s.finishConnect()) {
// Spin like a mad thing!
}
fail("connect returned: " + s + "!");
} catch (SocketException expected) {
assertEquals("Socket closed", expected.getMessage());
} catch (AsynchronousCloseException alsoOkay) {
// See below.
} catch (ClosedChannelException alsoOkay) {
// For now, I'm assuming that we're happy as long as we get any reasonable exception.
// It may be that we're supposed to guarantee only one or the other.
} finally {
ss.close();
}
}
Aggregations