Search in sources :

Example 11 with CalledMethod

use of biz.xsoftware.mock.CalledMethod in project webpieces by deanhiller.

the class ZNioFailureSuperclass method expectServerChannel.

static TCPChannel expectServerChannel(MockNIOServer mockServer, Class c) {
    CalledMethod method = mockServer.expect(MockNIOServer.CONNECTED);
    TCPChannel svrChan = (TCPChannel) method.getAllParams()[0];
    assertEquals("should be instance of correct channel type", c, svrChan.getClass());
    return svrChan;
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 12 with CalledMethod

use of biz.xsoftware.mock.CalledMethod in project webpieces by deanhiller.

the class ZPerformanceSuper method testVerySmallReadWrite.

/**
	 * This is the difference in performance of writing/reading secure data vs.
	 * writing/reading non-secure data for a VERY SMALL payload.  Realize though,
	 * in this test, the data is encrypted, decrypted, encrypted again, and 
	 * decrypted again, so the server takes half this load, and the client the
	 * other half.  
	 * Basic seems to be 75% of secure's time. This is a slowdown of 133% 
	 * for echoing 'hello'.
	 * 
     * Basic....
     * total write time         =1732 ms
     * total write/response time=1802 ms
     * time per write/response  =45   ms
     * Secure....
     * total write time         =2374 ms
     * total write/response time=2424 ms
     * time per write/response  =60   ms
     * Basic with network delay of 1 seconds....
     * total write time         =1522 ms
     * total write/response time=3585 ms
     * time per write/response  =89   ms
	 * @throws Exception
	 */
public void testVerySmallReadWrite() 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] = "finished";
    }
    TCPChannel[] clients = new TCPChannel[size];
    for (int i = 0; i < size; i++) {
        clients[i] = chanMgr.createTCPChannel("Client[" + i + "]", getClientFactoryHolder());
        FutureOperation future = clients[i].connect(svrAddr);
        future.setListener((OperationCallback) mockConnectOp);
    }
    mockConnectOp.expect(methodNames);
    log.info("done getting all connections");
    for (TCPChannel client : clients) {
        client.registerForReads((DataListener) mockHandler);
    }
    int numWrites = 200;
    String payload = "hello";
    helper.putString(b, payload);
    helper.doneFillingBuffer(b);
    int numBytes = b.remaining();
    methodNames = new String[size];
    for (int i = 0; i < size; i++) {
        methodNames[i] = "incomingData";
    }
    String[] finNames = new String[size];
    for (int i = 0; i < size; i++) {
        finNames[i] = "finished";
    }
    PerfTimer timer = new PerfTimer();
    PerfTimer timer2 = new PerfTimer();
    timer.start();
    timer2.start();
    CalledMethod[] methods = null;
    for (int i = 0; i < numWrites; i++) {
        for (TCPChannel client : clients) {
            FutureOperation write = client.write(b);
            write.setListener((OperationCallback) mockConnectOp);
            //client.oldWrite(b);
            b.rewind();
        }
        mockConnectOp.expect(finNames);
        methods = mockHandler.expect(methodNames);
    }
    long result2 = timer2.stop();
    long result = timer.stop();
    //pick a method and verify right data came back for performance test
    //to make sure performance test is valid....
    ByteBuffer actualBuf = (ByteBuffer) methods[5].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           =" + getSmallReadWriteTimeLimit());
    assertTrue(readWriteTime < getSmallReadWriteTimeLimit());
}
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)

Example 13 with CalledMethod

use of biz.xsoftware.mock.CalledMethod 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 14 with CalledMethod

use of biz.xsoftware.mock.CalledMethod 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 15 with CalledMethod

use of biz.xsoftware.mock.CalledMethod in project webpieces by deanhiller.

the class TestUdpIntegration method setupPortUnreachable.

/**
     * @param svrAddr
     * @throws IOException
     * @throws InterruptedException
     */
private InetSocketAddress setupPortUnreachable(InetSocketAddress svrAddr) throws IOException, InterruptedException {
    InetAddress localhost = InetAddress.getLocalHost();
    client.bind(new InetSocketAddress(localhost, 0));
    InetSocketAddress clientAddr = client.getLocalAddress();
    client.oldConnect(svrAddr);
    client.registerForReads((DataListener) clientHandler);
    String msg = "aaaaa";
    //should result in port unreachable
    writePacket(client, msg);
    //expect the exception
    CalledMethod m = clientHandler.expect("failure");
    PortUnreachableException exc = (PortUnreachableException) m.getAllParams()[2];
    log.log(Level.FINE, "this is expected", exc);
    return clientAddr;
}
Also used : PortUnreachableException(java.net.PortUnreachableException) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) CalledMethod(biz.xsoftware.mock.CalledMethod)

Aggregations

CalledMethod (biz.xsoftware.mock.CalledMethod)31 ByteBuffer (java.nio.ByteBuffer)27 CloneByteBuffer (org.webpieces.nio.api.testutil.CloneByteBuffer)27 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)8 MockObject (biz.xsoftware.mock.MockObject)5 InetSocketAddress (java.net.InetSocketAddress)4 FutureOperation (org.webpieces.nio.api.handlers.FutureOperation)4 SelectionKey (java.nio.channels.SelectionKey)3 HashSet (java.util.HashSet)3 DataListener (org.webpieces.nio.api.handlers.DataListener)2 PerfTimer (org.webpieces.nio.test.PerfTimer)2 InetAddress (java.net.InetAddress)1 PortUnreachableException (java.net.PortUnreachableException)1 HashMap (java.util.HashMap)1 ChannelServiceFactory (org.webpieces.nio.api.deprecated.ChannelServiceFactory)1 OperationCallback (org.webpieces.nio.api.handlers.OperationCallback)1 FactoryCreator (org.webpieces.nio.api.libs.FactoryCreator)1 SocketChannel (org.webpieces.nio.api.testutil.chanapi.SocketChannel)1 ChannelRegistrationListener (org.webpieces.nio.api.testutil.nioapi.ChannelRegistrationListener)1 Select (org.webpieces.nio.api.testutil.nioapi.Select)1