Search in sources :

Example 6 with CalledMethod

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

the class TestWrites method fireSelector.

public int fireSelector(MyKey key, String expected, String expected2, boolean isSpecial) throws Exception {
    client1.oldConnect(null);
    mockSunsChannel.expect("connect");
    ByteBuffer b = ByteBuffer.allocate(1000);
    HELPER.putString(b, expected);
    HELPER.doneFillingBuffer(b);
    int remain1 = b.remaining();
    mockSelect.addReturnValue("createRegistrationListener", mockRegListener);
    mockSunsChannel.addBehavior("write", new NoReadByteBuffer2(0));
    client1.oldWrite(b, (OperationCallback) mockWriteHandler);
    mockSunsChannel.expect("write");
    b = ByteBuffer.allocate(50);
    int remain2 = b.remaining();
    HELPER.putString(b, expected2);
    HELPER.doneFillingBuffer(b);
    client1.oldWrite(b, (OperationCallback) mockWriteHandler);
    mockSunsChannel.expect(MockObject.NONE);
    mockSelect.setDefaultReturnValue("getKeyFromChannel", key);
    String[] methodNames = new String[] { "getKeyFromChannel", "register" };
    CalledMethod[] methods = mockSelect.expect(methodNames);
    Object attachment = methods[1].getAllParams()[2];
    key.attach(attachment);
    Set<SelectionKey> set = new HashSet<SelectionKey>();
    set.add(key);
    mockSelect.addReturnValue("select", 1);
    mockSelect.addReturnValue("selectedKeys", set);
    mockSunsChannel.addBehavior("write", new NoReadByteBuffer2(remain1));
    if (isSpecial)
        mockSunsChannel.addBehavior("write", new NoReadByteBuffer2(1));
    else
        mockSunsChannel.addBehavior("write", new NoReadByteBuffer2(remain2));
    //now, simlute the jdk selector going off....
    listener.selectorFired();
    return remain2;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) MockObject(biz.xsoftware.mock.MockObject) ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod) HashSet(java.util.HashSet)

Example 7 with CalledMethod

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

the class TestWrites method runBasic.

private void runBasic() throws IOException {
    mockSelect.setDefaultReturnValue("getThread", null);
    mockSunsChannel.setDefaultBehavior("write", new CloneByteBuffer());
    ByteBuffer b = ByteBuffer.allocate(1000);
    String expected = "abc";
    HELPER.putString(b, expected);
    HELPER.doneFillingBuffer(b);
    //        mockSunsChannel.addReturnValue("write", b.remaining());
    client1.oldWrite(b);
    CalledMethod m = mockSunsChannel.expect("write");
    ByteBuffer actual = (ByteBuffer) m.getAllParams()[0];
    String msg = HELPER.readString(actual, actual.remaining());
    assertEquals(expected, msg);
}
Also used : CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 8 with CalledMethod

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

the class TestUnpacketizer method testSplitInSecondHeader.

public void testSplitInSecondHeader() throws IOException {
    if (log.isTraceEnabled())
        log.log(Level.FINE, "started");
    String test = "";
    for (int i = 0; i < 260; i++) {
        test += i % 10;
    }
    int len = test.getBytes().length;
    byte b1 = (byte) (len & 0x000000FF);
    byte b2 = (byte) ((len & 0x0000FF00) >> 8);
    byte b3 = (byte) ((len & 0x00FF0000) >> 16);
    byte b4 = (byte) ((len & 0xFF000000) >> 32);
    ByteBuffer b = ByteBuffer.allocate(400);
    String fullString = HALF1 + HALF2;
    int size = fullString.getBytes().length;
    b.putInt(size);
    helper.putString(b, fullString);
    b.put(PACKET_SEPARATOR);
    b.put(b4);
    b.put(b3);
    b.put(b2);
    //leave b1 off and do it later for the split
    //b.put(b1);
    helper.doneFillingBuffer(b);
    //contract is a donePut()(flip() is donePut() buffer that it can read to begin with.
    unpacketizer.incomingData(b, null);
    CalledMethod method = listener.expect(PACKET_METHOD);
    verifyBuffer(method, fullString, size);
    //numChars);
    doLastPartOfSplitHeaderVerification(b, b1, test);
}
Also used : CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) ByteBuffer(java.nio.ByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 9 with CalledMethod

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

the class XTestDelayServer method testVerySmallReadWrite.

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] = "connected";
    }
    TCPChannel[] clients = new TCPChannel[size];
    for (int i = 0; i < size; i++) {
        clients[i] = chanMgr.createTCPChannel("Client[" + i + "]", factoryHolder);
        log.trace("starting connect");
        Thread.sleep(100);
        clients[i].oldConnect(delaySvrAddr, (ConnectionCallback) mockConnect);
    }
    mockConnect.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);
    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++) {
            client.oldWrite(b);
            b.rewind();
        }
    }
    mockHandler.setExpectTimeout(10000);
    long result2 = timer2.stop();
    CalledMethod[] methods = mockHandler.expect(methodNames);
    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[0].getAllParams()[1];
    String actual = HELPER.readString(actualBuf, actualBuf.remaining());
    assertEquals(payload, actual);
    log.info("payload=" + actual);
    long readWriteTime = result / size;
    log.info("total write time         =" + result2);
    log.info("total write/read time    =" + result);
    log.info("--time per write/read    =" + readWriteTime);
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) ByteBuffer(java.nio.ByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 10 with CalledMethod

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

the class TestBasicUDP method verifyDataPassing.

private ByteBuffer verifyDataPassing(DatagramChannel svrChan) throws Exception {
    ByteBuffer b = createBuffer();
    int expectedWrote = b.remaining();
    log.trace("***********************************************");
    int actualWrite = client1.oldWrite(b);
    assertEquals(expectedWrote, actualWrite);
    CalledMethod m = mockServer.expect(MockNIOServer.INCOMING_DATA);
    ByteBuffer actualBuf = (ByteBuffer) m.getAllParams()[1];
    String result = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals("de", result);
    b.rewind();
    svrChan.send(b, client1.getLocalAddress());
    m = mockHandler.expect(MockDataHandler.INCOMING_DATA);
    actualBuf = (ByteBuffer) m.getAllParams()[1];
    result = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals("de", result);
    return b;
}
Also used : ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) 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