Search in sources :

Example 16 with CalledMethod

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

the class TestUdpIntegration method xxxtestDisconnect.

/** 
     * This tests disconnecting udp handles still receiving from other end gracefully.  The media
     * component discovered that even though the udp got disconnected, it kept receiving udp
     * packets and nio kept firing ready to read from which was bad. 
     * @throws InterruptedException 
     * @throws IOException 
     *
     */
//TODO: fix this test to work on linux and windows.
public void xxxtestDisconnect() throws IOException, InterruptedException {
    InetSocketAddress svrAddr = runBasic();
    client.disconnect();
    String msg = "hello";
    //NOTE: write packet from server to client.  Ideally, nothing will be fired to client
    writePacket(server, msg);
    //allow one second to go by so we know packet was received and no method should be
    //called on the client.
    Thread.sleep(1000);
    clientHandler.expect(MockObject.NONE);
    System.out.println("addr=" + svrAddr);
    client.oldConnect(svrAddr);
    String msg2 = "abxdefg";
    writePacket(server, msg2);
    String[] methodNames = new String[] { "incomingData", "incomingData" };
    CalledMethod[] methods = clientHandler.expect(methodNames);
    ByteBuffer actualBuf1 = (ByteBuffer) methods[0].getAllParams()[1];
    String actual1 = HELPER.readString(actualBuf1, actualBuf1.remaining());
    assertEquals(msg, actual1);
    ByteBuffer actualBuf2 = (ByteBuffer) methods[1].getAllParams()[1];
    String actual2 = HELPER.readString(actualBuf2, actualBuf2.remaining());
    assertEquals(msg2, actual2);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) ByteBuffer(java.nio.ByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 17 with CalledMethod

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

the class TestWrites method xxtestQueuedAsynchWrite.

/**
     * This tests the situation where client writes something which can't be written and
     * so that gets queued, and then client writes another thing which gets queued.  finally
     * the selector is fired, and this tests that both buffers were written.
     * 
     * @throws Exception
     */
public void xxtestQueuedAsynchWrite() throws Exception {
    mockSelect.setDefaultReturnValue("getThread", Thread.currentThread());
    MySelectableChannel channel = new MySelectableChannel((SocketChannel) mockSunsChannel);
    MyKey key = new MyKey(channel);
    mockSunsChannel.setDefaultReturnValue("getSelectableChannel", channel);
    MockObject mockKey = key.getMock();
    mockKey.addIgnore("readyOps");
    mockKey.setDefaultReturnValue("channel", mockSunsChannel);
    mockKey.setDefaultReturnValue("readyOps", SelectionKey.OP_WRITE);
    //mockSunsChannel.addBehavior("write", new NoReadByteBuffer2());
    //mockSunsChannel.addBehavior("write", new CloneByteBuffer());
    String expected = "abc";
    String expected2 = "def";
    mockKey.addReturnValue("interestOps", SelectionKey.OP_WRITE);
    fireSelector(key, expected, expected2, false);
    String[] methodNames = new String[] { "write", "write" };
    CalledMethod[] methods = mockSunsChannel.expect(methodNames);
    ByteBuffer actual = (ByteBuffer) methods[0].getAllParams()[0];
    String msg = HELPER.readString(actual, actual.remaining());
    assertEquals(expected, msg);
    ByteBuffer actual2 = (ByteBuffer) methods[1].getAllParams()[0];
    msg = HELPER.readString(actual2, actual2.remaining());
    assertEquals(expected2, msg);
    //expect that an unregistration happened
    methodNames = new String[] { "interestOps", "interestOps" };
    methods = mockKey.expect(methodNames);
    //expect that this channel is no longer interested in anything
    int ops = (Integer) methods[1].getAllParams()[0];
    assertEquals(0, ops);
    //make sure data can flow through as usual...
    runBasic();
}
Also used : ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) MockObject(biz.xsoftware.mock.MockObject) CalledMethod(biz.xsoftware.mock.CalledMethod)

Example 18 with CalledMethod

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

the class TestWrites method setUp.

protected void setUp() throws Exception {
    HandlerForTests.setupLogging();
    if (bufFactory == null) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(FactoryCreator.KEY_IS_DIRECT, false);
        FactoryCreator creator = FactoryCreator.createFactory(null);
        bufFactory = creator.createBufferFactory(map);
    }
    mockSunsChannel = MockObjectFactory.createMock(SocketChannel.class);
    mockSunsChannel.addIgnore("isBlocking");
    mockSunsChannel.addIgnore("getSelectableChannel");
    mockSunsChannel.setDefaultReturnValue("isBlocking", false);
    mockSunsChannel.addReturnValue("connect", true);
    mockSelect = MockObjectFactory.createMock(Select.class);
    mockSelect.addIgnore("isRunning");
    mockSelect.addIgnore("getThread");
    mockSelect.addIgnore("setRunning");
    mockSelect.setDefaultReturnValue("isRunning", true);
    mockSelect.setDefaultReturnValue("isWantShutdown", false);
    mockWriteHandler = MockObjectFactory.createMock(OperationCallback.class);
    mockRegListener = MockObjectFactory.createMock(ChannelRegistrationListener.class);
    MockObject mockChannels = MockObjectFactory.createMock(ChannelsFactory.class);
    MockObject mockSelectorProv = MockObjectFactory.createMock(SelectorProviderFactory.class);
    ChannelServiceFactory basic = ChannelServiceFactory.createFactory(null);
    Map<String, Object> props2 = new HashMap<String, Object>();
    props2.put(ChannelServiceFactory.KEY_IMPLEMENTATION_CLASS, ChannelServiceFactory.VAL_EXCEPTION_CHANNEL_MGR);
    props2.put(ChannelServiceFactory.KEY_CHILD_CHANNELMGR_FACTORY, basic);
    factory = ChannelServiceFactory.createFactory(props2);
    Map<String, Object> p = new HashMap<String, Object>();
    p.put(ChannelManagerOld.KEY_ID, "[client]");
    p.put(ChannelManagerOld.KEY_BUFFER_FACTORY, bufFactory);
    p.put("mock.channelsFactory", mockChannels);
    p.put("mock.selectorProvider", mockSelectorProv);
    chanMgr = factory.createChannelManager(p);
    mockSelectorProv.addReturnValue("provider", mockSelect);
    chanMgr.start();
    CalledMethod m = mockSelect.expect("startPollingThread");
    listener = (SelectorListener) m.getAllParams()[0];
    mockHandler = MockObjectFactory.createMock(DataListener.class);
    mockHandler.setDefaultBehavior("incomingData", new CloneByteBuffer());
    //mockConnect = MockObjectFactory.createMock(ConnectCallback.class);
    mockChannels.addReturnValue("open", mockSunsChannel);
    client1 = chanMgr.createTCPChannel("ClientChannel", null);
    mockSunsChannel.expect("configureBlocking");
}
Also used : SocketChannel(org.webpieces.nio.api.testutil.chanapi.SocketChannel) HashMap(java.util.HashMap) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) FactoryCreator(org.webpieces.nio.api.libs.FactoryCreator) ChannelServiceFactory(org.webpieces.nio.api.deprecated.ChannelServiceFactory) CalledMethod(biz.xsoftware.mock.CalledMethod) ChannelRegistrationListener(org.webpieces.nio.api.testutil.nioapi.ChannelRegistrationListener) OperationCallback(org.webpieces.nio.api.handlers.OperationCallback) Select(org.webpieces.nio.api.testutil.nioapi.Select) MockObject(biz.xsoftware.mock.MockObject) DataListener(org.webpieces.nio.api.handlers.DataListener) MockObject(biz.xsoftware.mock.MockObject)

Example 19 with CalledMethod

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

the class TestWrites method xxtestDelayedAsynchWrite.

/**
     * Have client do 3 writes, have the jdk not write at first causing writes to be 
     * queued.  Then have 1.5 writes happen causing the channel to stay registered
     * for writes.  fire the selector and write the rest out.
     */
public void xxtestDelayedAsynchWrite() throws Exception {
    mockSelect.setDefaultReturnValue("getThread", Thread.currentThread());
    MySelectableChannel channel = new MySelectableChannel((SocketChannel) mockSunsChannel);
    MyKey key = new MyKey(channel);
    mockSunsChannel.setDefaultReturnValue("getSelectableChannel", channel);
    MockObject mockKey = key.getMock();
    mockKey.addIgnore("readyOps");
    mockKey.setDefaultReturnValue("channel", mockSunsChannel);
    mockKey.setDefaultReturnValue("readyOps", SelectionKey.OP_WRITE);
    //mockSunsChannel.setDefaultBehavior("write", new CloneForDelayedWrite());
    String expected = "abc";
    String expected2 = "def";
    int remain2 = fireSelector(key, expected, expected2, true);
    String[] methodsNames = new String[] { "write", "write" };
    CalledMethod[] methods = mockSunsChannel.expect(methodsNames);
    ByteBuffer actual = (ByteBuffer) methods[0].getAllParams()[0];
    String msg = HELPER.readString(actual, actual.remaining());
    assertEquals(expected, msg);
    //because cache1 was a snapshot of what was passed in, we need to modify it...
    ByteBuffer cache1 = (ByteBuffer) methods[1].getAllParams()[0];
    cache1.limit(1);
    ByteBuffer b3 = ByteBuffer.allocate(50);
    int remain3 = b3.remaining();
    String expected3 = "ghi";
    HELPER.putString(b3, expected3);
    HELPER.doneFillingBuffer(b3);
    client1.oldWrite(b3, (OperationCallback) mockWriteHandler);
    Set<SelectionKey> set = new HashSet<SelectionKey>();
    set.add(key);
    mockSelect.addReturnValue("select", 1);
    mockSelect.addReturnValue("selectedKeys", set);
    //mockKey.addReturnValue("interestOps", SelectionKey.OP_WRITE);        
    mockSunsChannel.addBehavior("write", new NoReadByteBuffer2(remain2));
    mockSunsChannel.addBehavior("write", new NoReadByteBuffer2(remain3));
    mockKey.addReturnValue("interestOps", SelectionKey.OP_WRITE);
    //fire the selector again....
    listener.selectorFired();
    methods = mockSunsChannel.expect(methodsNames);
    ByteBuffer cache2 = (ByteBuffer) methods[0].getAllParams()[0];
    ByteBuffer actual2 = ByteBuffer.allocate(20);
    actual2.put(cache1);
    actual2.put(cache2);
    HELPER.doneFillingBuffer(actual2);
    String msg2 = HELPER.readString(actual2, actual2.remaining());
    assertEquals(expected2, msg2);
    ByteBuffer actual3 = (ByteBuffer) methods[1].getAllParams()[0];
    String msg3 = HELPER.readString(actual3, actual3.remaining());
    assertEquals(expected3, msg3);
    //expect that an unregistration happened
    String[] methodNames = new String[] { "interestOps", "interestOps" };
    methods = mockKey.expect(methodNames);
    //expect that this channel is no longer interested in anything
    int ops = (Integer) methods[1].getAllParams()[0];
    assertEquals(0, ops);
    //make sure data can flow through as usual...
    runBasic();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ByteBuffer(java.nio.ByteBuffer) CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) CalledMethod(biz.xsoftware.mock.CalledMethod) MockObject(biz.xsoftware.mock.MockObject) HashSet(java.util.HashSet)

Example 20 with CalledMethod

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

the class TestUnpacketizer method testHalfAPacket.

public void testHalfAPacket() throws IOException {
    if (log.isTraceEnabled())
        log.log(Level.FINE, "started");
    ByteBuffer b = ByteBuffer.allocate(30);
    String fullString = HALF1 + HALF2;
    int size = fullString.getBytes().length;
    b.putInt(size);
    helper.putString(b, HALF1);
    helper.doneFillingBuffer(b);
    unpacketizer.incomingData(b, null);
    listener.expect(MockObject.NONE);
    helper.eraseBuffer(b);
    helper.putString(b, HALF2);
    b.put(PACKET_SEPARATOR);
    helper.doneFillingBuffer(b);
    if (log.isTraceEnabled())
        log.log(Level.FINE, "FEED NEXT BUFFER********************");
    unpacketizer.incomingData(b, null);
    CalledMethod method = listener.expect(PACKET_METHOD);
    verifyBuffer(method, fullString, size);
    if (log.isTraceEnabled())
        log.log(Level.FINE, "ended");
}
Also used : CloneByteBuffer(org.webpieces.nio.api.testutil.CloneByteBuffer) ByteBuffer(java.nio.ByteBuffer) 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