@Test public void testRegisterChannelDuplexWithSelectableChannel() throws Exception { // Normal register, with selectable channel SocketChannel[] peerSocketChannels = IntrabandTestUtil.createSocketChannelPeers(); SocketChannel socketChannel = peerSocketChannels[0]; socketChannel.configureBlocking(true); try { FutureRegistrationReference futureRegistrationReference = (
@Test public void testRegisterChannelDuplex() throws Exception { // Channel is null try { _selectorIntraband.registerChannel(null); Assert.fail(); } catch (NullPointerException npe) { Assert.assertEquals("Channel is null", npe.getMessage()); } // Channel is not of type GatheringByteChannel try { _selectorIntraband.registerChannel(IntrabandTestUtil.<Channel>createProxy(Channel.class)); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals("Channel is not of type GatheringByteChannel", iae.getMessage()); } // Channel is not of type ScatteringByteChannel try { _selectorIntraband.registerChannel( IntrabandTestUtil.<Channel>createProxy(GatheringByteChannel.class)); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals("Channel is not of type ScatteringByteChannel", iae.getMessage()); } // Channel is not of type SelectableChannel try { _selectorIntraband.registerChannel( IntrabandTestUtil.<Channel>createProxy( ScatteringByteChannel.class, GatheringByteChannel.class)); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals("Channel is not of type SelectableChannel", iae.getMessage()); } // Channel is not valid for reading try { _selectorIntraband.registerChannel(new MockDuplexSelectableChannel(false, true)); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals("Channel is not valid for reading", iae.getMessage()); } // Channel is not valid for writing try { _selectorIntraband.registerChannel(new MockDuplexSelectableChannel(true, false)); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals("Channel is not valid for writing", iae.getMessage()); } SocketChannel[] peerSocketChannels = IntrabandTestUtil.createSocketChannelPeers(); try { SocketChannel socketChannel = peerSocketChannels[0]; // Interruptted on register final Thread mainThread = Thread.currentThread(); Thread wakeUpThread = new Thread(new WakeUpRunnable(_selectorIntraband)); Thread interruptThread = new Thread() { @Override public void run() { while (mainThread.getState() != Thread.State.WAITING) ; mainThread.interrupt(); } }; wakeUpThread.start(); Selector selector = _selectorIntraband.selector; synchronized (selector) { wakeUpThread.interrupt(); wakeUpThread.join(); interruptThread.start(); try { _selectorIntraband.registerChannel(socketChannel); Assert.fail(); } catch (IOException ioe) { Throwable cause = ioe.getCause(); Assert.assertTrue(cause instanceof InterruptedException); } interruptThread.join(); } _selectorIntraband.close(); _selectorIntraband = new SelectorIntraband(_DEFAULT_TIMEOUT); // Normal register SelectionKeyRegistrationReference selectionKeyRegistrationReference = (SelectionKeyRegistrationReference) _selectorIntraband.registerChannel(socketChannel); Assert.assertNotNull(selectionKeyRegistrationReference); Assert.assertSame( selectionKeyRegistrationReference.readSelectionKey, selectionKeyRegistrationReference.writeSelectionKey); SelectionKey selectionKey = selectionKeyRegistrationReference.readSelectionKey; Assert.assertTrue(selectionKey.isValid()); Assert.assertEquals(SelectionKey.OP_READ, selectionKey.interestOps()); Assert.assertNotNull(selectionKey.attachment()); selectionKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE); selector = _selectorIntraband.selector; selector.wakeup(); while (selectionKey.interestOps() != SelectionKey.OP_READ) ; // Concurrent cancelling wakeUpThread = new Thread(new WakeUpRunnable(_selectorIntraband)); wakeUpThread.start(); synchronized (selector) { wakeUpThread.interrupt(); wakeUpThread.join(); selectionKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE); SocketChannel peerSocketChannel = peerSocketChannels[1]; peerSocketChannel.write(ByteBuffer.allocate(1)); Socket socket = peerSocketChannel.socket(); socket.shutdownOutput(); } while (selectionKey.isValid()) ; // Register after close _selectorIntraband.close(); try { _selectorIntraband.registerChannel(socketChannel); Assert.fail(); } catch (ClosedIntrabandException cibe) { } } finally { peerSocketChannels[0].close(); peerSocketChannels[1].close(); } }
@Test public void testRegisterChannelDuplexWithErrors() throws Exception { // Channel is null try { _executorIntraband.registerChannel(null); Assert.fail(); } catch (NullPointerException npe) { Assert.assertEquals("Channel is null", npe.getMessage()); } // Channel is not of type GatheringByteChannel try { _executorIntraband.registerChannel( IntrabandTestUtil.<Channel>createProxy(Channel.class)); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals( "Channel is not of type GatheringByteChannel", iae.getMessage()); } // Channel is not of type ScatteringByteChannel try { _executorIntraband.registerChannel( IntrabandTestUtil.<Channel>createProxy( GatheringByteChannel.class)); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals( "Channel is not of type ScatteringByteChannel", iae.getMessage()); } // Channel is of type SelectableChannel and configured in nonblocking // mode SocketChannel[] peerSocketChannels = IntrabandTestUtil.createSocketChannelPeers(); SocketChannel socketChannel = peerSocketChannels[0]; socketChannel.configureBlocking(false); try { _executorIntraband.registerChannel(socketChannel); Assert.fail(); } catch (IllegalArgumentException iae) { Assert.assertEquals( "Channel is of type SelectableChannel and configured in " + "nonblocking mode", iae.getMessage()); } }