Search in sources :

Example 1 with MockObject

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

the class TestWrites method xxtestAsynchWrite.

public void xxtestAsynchWrite() 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.setDefaultReturnValue("channel", mockSunsChannel);
    mockKey.setDefaultReturnValue("readyOps", SelectionKey.OP_WRITE);
    mockSunsChannel.setDefaultBehavior("write", new NoReadByteBuffer2(0));
    client1.oldConnect(null);
    mockSunsChannel.expect("connect");
    ByteBuffer b = ByteBuffer.allocate(1000);
    String expected = "abc";
    HELPER.putString(b, expected);
    HELPER.doneFillingBuffer(b);
    mockSelect.addReturnValue("createRegistrationListener", mockRegListener);
    mockSunsChannel.addReturnValue("write", 0);
    client1.oldWrite(b, (OperationCallback) mockWriteHandler);
    mockSunsChannel.expect("write");
    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);
    mockKey.addReturnValue("interestOps", SelectionKey.OP_WRITE);
    mockSunsChannel.addReturnValue("write", b.remaining());
    // now, simlute the jdk selector going off....
    listener.selectorFired();
    CalledMethod m = mockSunsChannel.expect("write");
    ByteBuffer actual = (ByteBuffer) m.getAllParams()[0];
    String msg = HELPER.readString(actual, actual.remaining());
    assertEquals(expected, msg);
}
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) MockObject(biz.xsoftware.mock.MockObject) HashSet(java.util.HashSet)

Example 2 with MockObject

use of biz.xsoftware.mock.MockObject 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 3 with MockObject

use of biz.xsoftware.mock.MockObject 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 4 with MockObject

use of biz.xsoftware.mock.MockObject 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 5 with MockObject

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

the class TryRealConnection method xxxtestRealConnection.

public void xxxtestRealConnection() throws Exception {
    MockObject mockConnect = MockObjectFactory.createMock(ConnectionCallback.class);
    Settings h = new Settings(new MockSSLEngineFactory(), null);
    TCPChannel channel = chanMgr.createTCPChannel("testId", h);
    channel.bind(loopBackAddr);
    log.trace("aaaaa");
    MockObject handler = new MockDataHandler();
    channel.registerForReads((DataListener) handler);
    // InetAddress host = InetAddress.getByName("shell.sourceforge.net");
    InetAddress host = InetAddress.getByName("www.xsoftware.biz");
    InetSocketAddress addr = new InetSocketAddress(host, 22);
    channel.oldConnect(addr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");
    handler.addIgnore("getBuffer");
    // CalledMethod method =
    handler.expect("incomingData");
// ByteBuffer b = (ByteBuffer)method.getAllParams()[1];
}
Also used : TCPChannel(org.webpieces.nio.api.channels.TCPChannel) MockDataHandler(org.webpieces.nio.api.testutil.MockDataHandler) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) MockObject(biz.xsoftware.mock.MockObject) Settings(org.webpieces.nio.api.deprecated.Settings) MockSSLEngineFactory(org.webpieces.nio.api.testutil.MockSSLEngineFactory)

Aggregations

MockObject (biz.xsoftware.mock.MockObject)5 CalledMethod (biz.xsoftware.mock.CalledMethod)4 CloneByteBuffer (org.webpieces.nio.api.testutil.CloneByteBuffer)4 ByteBuffer (java.nio.ByteBuffer)3 SelectionKey (java.nio.channels.SelectionKey)2 HashSet (java.util.HashSet)2 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1 TCPChannel (org.webpieces.nio.api.channels.TCPChannel)1 ChannelServiceFactory (org.webpieces.nio.api.deprecated.ChannelServiceFactory)1 Settings (org.webpieces.nio.api.deprecated.Settings)1 DataListener (org.webpieces.nio.api.handlers.DataListener)1 OperationCallback (org.webpieces.nio.api.handlers.OperationCallback)1 FactoryCreator (org.webpieces.nio.api.libs.FactoryCreator)1 MockDataHandler (org.webpieces.nio.api.testutil.MockDataHandler)1 MockSSLEngineFactory (org.webpieces.nio.api.testutil.MockSSLEngineFactory)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