Search in sources :

Example 11 with TCPChannel

use of org.webpieces.nio.api.channels.TCPChannel in project webpieces by deanhiller.

the class ZNioFailureSuperclass method testClientThrowsIntoDataHandlerFarEndClosed.

public void testClientThrowsIntoDataHandlerFarEndClosed() throws Exception {
    setNumberOfExpectedWarnings(1);
    //make sure we are testing the right one....
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be instance of correct channel type", c, client1.getClass());
    String msg = "some exception message";
    IOException e = new IOException(msg);
    mockServer.addThrowException("farEndClosed", e);
    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    client1.registerForReads((DataListener) mockHandler);
    mockConnect.expect("connected");
    TCPChannel svrChan = expectServerChannel(mockServer, c);
    verifyDataPassing(svrChan);
    verifyTearDown();
    //shutdown of threaded CM is complicated...set stop method on threaded CM.
    //We must sleep or thread could be interrupted and never log an error.
    //This is a race condition, but only when we are stopping the ChannelManager
    specialCaseThreadedCM();
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) IOException(java.io.IOException)

Example 12 with TCPChannel

use of org.webpieces.nio.api.channels.TCPChannel in project webpieces by deanhiller.

the class ZNioFailureSuperclass method verifyDataPassing.

//	public void testClientThrowsIntoAcceptHandlerConnect() throws Exception {
//		setNumberOfExpectedWarnings(1);
//
//		//make sure we are testing the right one....
//		Class c = Class.forName(getChannelImplName());
//		assertEquals("should be instance of correct channel type", c, client1.getClass());
//	
//		String msg = "some exception message";
//		IOException e = new IOException(msg);
//		mockServer.addThrowException("connected", e);
//		
//		client1.bind(loopBackAnyPort);
//		client1.oldConnect(svrAddr, (ConnectionCallback)mockConnect);
//		client1.registerForReads((DataListener)mockHandler);
//		
//		mockConnect.expect("connected");
//		TCPChannel svrChan = expectServerChannel(mockServer, c);
//
//		verifyDataPassing(svrChan);
//		verifyTearDown();		
//	}
private ByteBuffer verifyDataPassing(TCPChannel svrChan) throws Exception {
    ByteBuffer b = ByteBuffer.allocate(10);
    helper.putString(b, "de");
    helper.doneFillingBuffer(b);
    int expectedWrote = b.remaining();
    int actualWrite = client1.oldWrite(b);
    assertEquals(expectedWrote, actualWrite);
    CalledMethod m = mockServer.expect(MockNIOServer.INCOMING_DATA);
    TCPChannel actualChannel = (TCPChannel) m.getAllParams()[0];
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be correct type of channel", c, actualChannel.getClass());
    ByteBuffer actualBuf = (ByteBuffer) m.getAllParams()[1];
    String result = helper.readString(actualBuf, actualBuf.remaining());
    log.info("result len=" + result.length());
    assertEquals("de", result);
    b.rewind();
    svrChan.oldWrite(b);
    m = mockHandler.expect(MockDataHandler.INCOMING_DATA);
    actualBuf = (ByteBuffer) m.getAllParams()[1];
    log.info("buffer remain=" + actualBuf.remaining());
    result = helper.readString(actualBuf, actualBuf.remaining());
    log.info("---1st char=" + (result.substring(0, 1).equals("de".substring(0, 1))));
    log.info("---2nd char=" + (result.substring(1, 2).equals("de".substring(1, 2))));
    log.info("substring='" + result.substring(0, 1) + "'");
    log.info("len=" + "de".length() + "  2ndlen=" + result.length());
    log.info("'de'" + " actual='" + result + "'" + "  result=" + ("de".equals(result)));
    assertEquals("de", result);
    return b;
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 13 with TCPChannel

use of org.webpieces.nio.api.channels.TCPChannel in project webpieces by deanhiller.

the class ZNioSuperclassTest method testUnregisterReregisterForReads.

public void testUnregisterReregisterForReads() throws Exception {
    Class c = Class.forName(getChannelImplName());
    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);
    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    client1.registerForReads((DataListener) mockHandler);
    ByteBuffer b = verifyDataPassing(svrChan);
    client1.unregisterForReads();
    b.rewind();
    svrChan.oldWrite(b);
    Thread.sleep(5000);
    mockHandler.expect(MockObject.NONE);
    client1.registerForReads((DataListener) mockHandler);
    CalledMethod m = mockHandler.expect(MockNIOServer.INCOMING_DATA);
    ByteBuffer actualBuf = (ByteBuffer) m.getAllParams()[1];
    String result = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals("de", result);
    verifyTearDown();
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 14 with TCPChannel

use of org.webpieces.nio.api.channels.TCPChannel in project webpieces by deanhiller.

the class ZNioSuperclassTest method testCloseSocketAfterChannelMgrShutdown.

/**
	 * There was a bug where calling ChannelManager.shutdown before closing any sockets
	 * registered with that ChannelManager cannot be closed...well, at least this test
	 * proves when we close the test, the other side should receive that -1 indicating
	 * the far end closed the socket.
	 * 
	 * @throws Exception
	 */
public void testCloseSocketAfterChannelMgrShutdown() throws Exception {
    Class c = Class.forName(getChannelImplName());
    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);
    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    client1.registerForReads((DataListener) mockHandler);
    verifyDataPassing(svrChan);
    //shutdown channel manager first....should all sockets be closed?  Right now
    //someone has to manually close all accepted sockets...ie. client responsibility.
    svrChan.oldClose();
    mockServer.stop();
    //notice the Channelmanager on the client side has not shut down so we should
    //see a close event....
    mockHandler.expect(MockNIOServer.FAR_END_CLOSED);
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Example 15 with TCPChannel

use of org.webpieces.nio.api.channels.TCPChannel in project webpieces by deanhiller.

the class ProxyDataListener method farEndClosed.

@Override
public void farEndClosed(Channel channel) {
    log.error(channel + "far end closed");
    connectedChannels.removeChannel((TCPChannel) channel);
    TCPChannel proxy = lookupExistingOrCreateNew(channel);
    dataListener.farEndClosed(proxy);
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Aggregations

TCPChannel (org.webpieces.nio.api.channels.TCPChannel)39 InetSocketAddress (java.net.InetSocketAddress)12 ByteBuffer (java.nio.ByteBuffer)10 CalledMethod (biz.xsoftware.mock.CalledMethod)8 CloneByteBuffer (org.webpieces.nio.api.testutil.CloneByteBuffer)6 ChannelManager (org.webpieces.nio.api.ChannelManager)5 Channel (org.webpieces.nio.api.channels.Channel)5 BufferCreationPool (org.webpieces.data.api.BufferCreationPool)4 ChannelManagerFactory (org.webpieces.nio.api.ChannelManagerFactory)4 FutureOperation (org.webpieces.nio.api.handlers.FutureOperation)4 AsyncConfig (org.webpieces.asyncserver.api.AsyncConfig)3 AsyncServer (org.webpieces.asyncserver.api.AsyncServer)3 AsyncServerManager (org.webpieces.asyncserver.api.AsyncServerManager)3 BufferPool (org.webpieces.data.api.BufferPool)3 PerfTimer (org.webpieces.nio.test.PerfTimer)3 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)2 Executor (java.util.concurrent.Executor)2 Test (org.junit.Test)2 TCPServerChannel (org.webpieces.nio.api.channels.TCPServerChannel)2