Search in sources :

Example 1 with AsynchronousCloseException

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);
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) File(java.io.File) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 2 with AsynchronousCloseException

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();
    }
}
Also used : SocketException(java.net.SocketException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) InputStreamReader(java.io.InputStreamReader) EOFException(java.io.EOFException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) IOException(java.io.IOException) EOFException(java.io.EOFException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SocketException(java.net.SocketException)

Example 3 with AsynchronousCloseException

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.
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ServerSocket(java.net.ServerSocket) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) NotYetBoundException(java.nio.channels.NotYetBoundException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 4 with AsynchronousCloseException

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
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ByteBuffer(java.nio.ByteBuffer) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ClosedChannelException(java.nio.channels.ClosedChannelException) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) IOException(java.io.IOException) SocketException(java.net.SocketException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Example 5 with AsynchronousCloseException

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();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SocketException(java.net.SocketException) ClosedChannelException(java.nio.channels.ClosedChannelException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) StuckServer(tests.net.StuckServer)

Aggregations

AsynchronousCloseException (java.nio.channels.AsynchronousCloseException)15 IOException (java.io.IOException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)6 SocketException (java.net.SocketException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 ExecutorService (java.util.concurrent.ExecutorService)3 Test (org.junit.Test)3 SettableFuture (com.google.common.util.concurrent.SettableFuture)2 InetSocketAddress (java.net.InetSocketAddress)2 ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 NotYetBoundException (java.nio.channels.NotYetBoundException)2 NotYetConnectedException (java.nio.channels.NotYetConnectedException)2 SocketChannel (java.nio.channels.SocketChannel)2 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)2 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 EOFException (java.io.EOFException)1