Search in sources :

Example 1 with MockDataListener

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);
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress) MockDataListener(org.webpieces.nio.api.mocks.MockDataListener) Test(org.junit.Test)

Example 2 with MockDataListener

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());
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress) MockDataListener(org.webpieces.nio.api.mocks.MockDataListener) Test(org.junit.Test)

Example 3 with MockDataListener

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());
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress) MockDataListener(org.webpieces.nio.api.mocks.MockDataListener) Test(org.junit.Test)

Example 4 with MockDataListener

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);
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress) MockDataListener(org.webpieces.nio.api.mocks.MockDataListener) Test(org.junit.Test)

Example 5 with MockDataListener

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 }));
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress) MockDataListener(org.webpieces.nio.api.mocks.MockDataListener) NioException(org.webpieces.util.exceptions.NioException) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)7 MockDataListener (org.webpieces.nio.api.mocks.MockDataListener)7 Test (org.junit.Test)5 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)5 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 Before (org.junit.Before)2 TwoPools (org.webpieces.data.api.TwoPools)2 DirectExecutor (org.webpieces.util.threading.DirectExecutor)2 NioException (org.webpieces.util.exceptions.NioException)1