Search in sources :

Example 31 with TCPChannel

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

the class ZNioSuperclassTest method testRegisterForReadsAfterConnect.

/**
	 * Order between TCPChannel.connect and TCPChannel.registerForRead
	 * results in different code paths...this is one of the tests.
	 * @throws Exception
	 */
public void testRegisterForReadsAfterConnect() throws Exception {
    //make sure we are testing the right one....
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be instance of secure channel", c, client1.getClass());
    //no bind, just do connect to test port is not zero
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");
    log.info("connected");
    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);
    InetSocketAddress localAddr = client1.getLocalAddress();
    assertTrue("Port should not be 0", localAddr.getPort() != 0);
    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    client1.registerForReads((DataListener) mockHandler);
    log.info("data passing");
    verifyDataPassing(svrChan);
    log.info("teardown");
    verifyTearDown();
    log.info("done");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Example 32 with TCPChannel

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

the class ZNioSuperclassTest method testTwoBindsOnPortZero.

/**
	 * This fixes the bug in the jdk where SocketChannel.getLocalPort
	 * return 0 instead of the port that was bound.
	 * 
	 * @throws Exception
	 */
public void testTwoBindsOnPortZero() throws Exception {
    TCPChannel chan1 = chanMgr.createTCPChannel("chan1", getClientFactoryHolder());
    TCPChannel chan2 = chanMgr.createTCPChannel("chan2", getClientFactoryHolder());
    InetSocketAddress addr = new InetSocketAddress(loopBack, 0);
    chan1.bind(addr);
    chan2.bind(addr);
    int port1 = chan1.getLocalAddress().getPort();
    int port2 = chan2.getLocalAddress().getPort();
    assertTrue("port1 is zero, this is bad", port1 != 0);
    assertTrue("port2 is zero, this is bad", port2 != 0);
    assertTrue("port1==port2, this is bad port1=" + port1, port1 != port2);
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) InetSocketAddress(java.net.InetSocketAddress)

Example 33 with TCPChannel

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

the class ZNioSuperclassTest method verifyDataPassing.

private ByteBuffer verifyDataPassing(TCPChannel svrChan) throws Exception {
    ByteBuffer b = ByteBuffer.allocate(10);
    helper.putString(b, "de");
    helper.doneFillingBuffer(b);
    int expectedWrote = b.remaining();
    log.trace("***********************************************");
    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());
    assertEquals("de", result);
    b.rewind();
    svrChan.oldWrite(b);
    m = mockHandler.expect(MockDataHandler.INCOMING_DATA);
    actualBuf = (ByteBuffer) m.getAllParams()[1];
    result = helper.readString(actualBuf, actualBuf.remaining());
    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 34 with TCPChannel

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

the class ZNioSuperclassTest method xtestRegisterForReadsBeforeConnect.

//	public void testClientThrowsIntoConnectCallback() throws Exception {
//		//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);
//		mockConnect.addThrowException("connected", e);
//		
//		client1.bind(loopBackAnyPort);		
//		client1.registerForReads((DataHandler)mockHandler);
//		client1.connect(svrAddr, (ConnectCallback)mockConnect);
//
//		mockConnect.expect("connected");
//		TCPChannel svrChan = (TCPChannel)mockServer.expect(MockNIOServer.CONNECTED).getAllParams()[0];
//		assertEquals("should be instance of correct channel type", c, svrChan.getClass());
//
//		verifyDataPassing(svrChan);
//		verifyTearDown();		
//	}
/**
	 * This cannot pass on linux right now as warnings end up in the log 
	 * from reads firing even though there should be none if not connected.
	 * We can fix this later if it is needed.
	 * 
	 * Order between TCPChannel.connect and TCPChannel.registerForRead
	 * results in different code paths...this is one of the tests.
	 * @throws Exception
	 */
public void xtestRegisterForReadsBeforeConnect() throws Exception {
    //make sure we are testing the right one....
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be instance of correct channel type", c, client1.getClass());
    client1.bind(loopBackAnyPort);
    client1.registerForReads((DataListener) mockHandler);
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");
    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);
    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    verifyDataPassing(svrChan);
    verifyTearDown();
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel)

Example 35 with TCPChannel

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

the class ZPerformanceSuper method testLargeReadWrite.

/**
	 * This is the difference in performance of writing/reading secure data vs.
	 * writing/reading non-secure data for a very small payload.  
	 * Basic seems to be 75% of secure's time. This is a slowdown of 133% 
	 * for echoing 'hello'.
	 * 
     * Basic....
     * total write time         = 1402 ms
     * total write/response time= 1433 ms
     * time per write/response  = 35   ms

     * Secure....
     * total write time         = 6119 ms
     * total write/response time= 6159 ms
     * time per write/response  = 153 ms
     *
	 * @throws Exception
	 */
public void testLargeReadWrite() throws Exception {
    ByteBuffer b = ByteBuffer.allocate(4000);
    log.info("getting all proper connections");
    int size = 40;
    String[] methodNames = new String[size];
    for (int i = 0; i < size; i++) {
        methodNames[i] = "connected";
    }
    TCPChannel[] clients = new TCPChannel[size];
    for (int i = 0; i < size; i++) {
        clients[i] = chanMgr.createTCPChannel("Client[" + i + "]", getClientFactoryHolder());
        clients[i].oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    }
    mockConnect.expect(methodNames);
    log.info("done getting all connections");
    for (TCPChannel client : clients) {
        client.registerForReads((DataListener) mockHandler);
    }
    int numWrites = 100;
    String payload = "hello";
    for (int i = 0; i < 3000; i++) {
        payload += "i";
    }
    helper.putString(b, payload);
    helper.doneFillingBuffer(b);
    int numBytes = b.remaining();
    log.info("size=" + b.remaining());
    methodNames = new String[size * numWrites];
    for (int i = 0; i < size * numWrites; i++) {
        methodNames[i] = "incomingData";
    }
    PerfTimer timer = new PerfTimer();
    PerfTimer timer2 = new PerfTimer();
    timer.start();
    timer2.start();
    for (TCPChannel client : clients) {
        for (int i = 0; i < numWrites; i++) {
            FutureOperation future = client.write(b);
            future.waitForOperation(5000);
            b.rewind();
        }
    }
    long result2 = timer2.stop();
    CalledMethod[] methods = mockHandler.expect(methodNames);
    long result = timer.stop();
    ByteBuffer actualBuf = (ByteBuffer) methods[6].getAllParams()[1];
    String actual = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals(payload, actual);
    log.info("payload=" + actual);
    long readWriteTime = result / size;
    long byteTime = 100 * result / (numWrites * numBytes);
    log.info("total write time         =" + result2);
    log.info("total write/read time    =" + result);
    log.info("--time per 100 bytes     =" + byteTime);
    log.info("test result info:");
    log.info("--time per write/read    =" + readWriteTime);
    log.info("--time to beat           =" + getLargerReadWriteTimeLimit());
    assertTrue(readWriteTime < getLargerReadWriteTimeLimit());
}
Also used : PerfTimer(org.webpieces.nio.test.PerfTimer) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) FutureOperation(org.webpieces.nio.api.handlers.FutureOperation) CalledMethod(biz.xsoftware.mock.CalledMethod)

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