use of org.webpieces.nio.api.mocks.MockDataListener in project webpieces by deanhiller.
the class TestConnecting method testConnectingDelayedAndOnSelectorThread.
@Test
public void testConnectingDelayedAndOnSelectorThread() throws InterruptedException, ExecutionException, TimeoutException {
MockDataListener listener = new MockDataListener();
TCPChannel channel = mgr.createTCPChannel("myid");
mockChannel.addConnectReturnValue(false);
mockJdk.setThread(Thread.currentThread());
XFuture<Void> future = channel.connect(new InetSocketAddress(4444), listener);
Assert.assertFalse(future.isDone());
Assert.assertFalse(mockChannel.isRegisteredForReads());
Assert.assertEquals(0, mockChannel.getNumTimesFinishConnectCalled());
// NOW, fire the selector AND return ready for finishing connection so the connection future resolves
mockChannel.setReadyToConnect();
mockJdk.fireSelector();
Assert.assertTrue(mockChannel.isRegisteredForReads());
Assert.assertEquals(1, mockChannel.getNumTimesFinishConnectCalled());
future.get(2, TimeUnit.SECONDS);
}
use of org.webpieces.nio.api.mocks.MockDataListener in project webpieces by deanhiller.
the class TestConnecting method testBasicConnectingOnSelectorThread.
@Test
public void testBasicConnectingOnSelectorThread() {
MockDataListener listener = new MockDataListener();
TCPChannel channel = mgr.createTCPChannel("myid");
mockChannel.addConnectReturnValue(true);
mockJdk.setThread(Thread.currentThread());
XFuture<Void> future = channel.connect(new InetSocketAddress(4444), listener);
Assert.assertTrue(future.isDone());
Assert.assertTrue(mockChannel.isRegisteredForReads());
}
use of org.webpieces.nio.api.mocks.MockDataListener in project webpieces by deanhiller.
the class TestConnecting method testBasicConnectingNotOnSelectorThreadAtFirst.
@Test
public void testBasicConnectingNotOnSelectorThreadAtFirst() {
MockDataListener listener = new MockDataListener();
TCPChannel channel = mgr.createTCPChannel("myid");
mockChannel.addConnectReturnValue(true);
XFuture<Void> future = channel.connect(new InetSocketAddress(4444), listener);
Assert.assertFalse(future.isDone());
Assert.assertFalse(mockChannel.isRegisteredForReads());
// simulate being on selector thread
mockJdk.setThread(Thread.currentThread());
mockJdk.fireSelector();
Assert.assertTrue(future.isDone());
Assert.assertTrue(mockChannel.isRegisteredForReads());
}
use of org.webpieces.nio.api.mocks.MockDataListener in project webpieces by deanhiller.
the class TestConnecting method testConnectingDelayedAndNotOnSelectorThread.
@Test
public void testConnectingDelayedAndNotOnSelectorThread() throws InterruptedException, ExecutionException, TimeoutException {
MockDataListener listener = new MockDataListener();
TCPChannel channel = mgr.createTCPChannel("myid");
mockChannel.addConnectReturnValue(false);
XFuture<Void> future = channel.connect(new InetSocketAddress(4444), listener);
Assert.assertEquals(1, mockJdk.getNumTimesWokenUp());
Assert.assertFalse(future.isDone());
// simulate being on selector thread
mockJdk.setThread(Thread.currentThread());
// next simulate the selector waking up and firing
mockJdk.fireSelector();
// still not done
Assert.assertFalse(future.isDone());
Assert.assertEquals(0, mockChannel.getNumTimesFinishConnectCalled());
Assert.assertFalse(mockChannel.isRegisteredForReads());
// NOW, fire the selector AND return ready for finishing connection so the connection future resolves
mockChannel.setReadyToConnect();
mockJdk.fireSelector();
Assert.assertTrue(mockChannel.isRegisteredForReads());
Assert.assertEquals(1, mockChannel.getNumTimesFinishConnectCalled());
future.get(2, TimeUnit.SECONDS);
}
use of org.webpieces.nio.api.mocks.MockDataListener in project webpieces by deanhiller.
the class TestConnecting method testWriteBeforeConnection.
@Test
public void testWriteBeforeConnection() throws InterruptedException, ExecutionException, TimeoutException {
MockDataListener listener = new MockDataListener();
TCPChannel channel = mgr.createTCPChannel("myid");
mockChannel.addConnectReturnValue(false);
XFuture<Void> future = channel.connect(new InetSocketAddress(4444), listener);
Assert.assertEquals(1, mockJdk.getNumTimesWokenUp());
Assert.assertFalse(future.isDone());
// simulate being on selector thread
mockJdk.setThread(Thread.currentThread());
// next simulate the selector waking up and firing
mockJdk.fireSelector();
// still not done
Assert.assertFalse(future.isDone());
Assert.assertEquals(0, mockChannel.getNumTimesFinishConnectCalled());
Assert.assertFalse(mockChannel.isRegisteredForReads());
try {
channel.write(ByteBuffer.wrap(new byte[] { 1 }));
Assert.fail("should have thrown exception since channel is not connected yet");
} catch (NioException e) {
}
// NOW, fire the selector AND return ready for finishing connection so the connection future resolves
mockChannel.setReadyToConnect();
mockJdk.fireSelector();
Assert.assertTrue(mockChannel.isRegisteredForReads());
Assert.assertEquals(1, mockChannel.getNumTimesFinishConnectCalled());
future.get(2, TimeUnit.SECONDS);
channel.write(ByteBuffer.wrap(new byte[] { 1 }));
}
Aggregations