Search in sources :

Example 11 with MockSocketChannel

use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.

the class BuffersTest method testReadLength.

public void testReadLength() throws Exception {
    byte[] tmp = "hello world".getBytes();
    ByteBuffer data = ByteBuffer.allocate(Global.INT_SIZE + tmp.length).putInt(tmp.length).put(tmp);
    // read the entire length
    data.flip().limit(4);
    MockSocketChannel ch = new MockSocketChannel().bytesToRead(data);
    Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), null);
    ByteBuffer buf = bufs.readLengthAndData(ch);
    assert buf == null;
    // allow for some more data to be read...
    data.limit(8);
    buf = bufs.readLengthAndData(ch);
    assert buf == null;
    // read all data
    data.limit(data.capacity());
    buf = bufs.readLengthAndData(ch);
    assert buf != null;
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 12 with MockSocketChannel

use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.

the class BuffersTest method testMakeSpaceSimple.

public void testMakeSpaceSimple() throws Exception {
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(1000);
    Buffers buf = new Buffers(2);
    buf.add(buf1);
    check(buf, 0, 1, 1, remaining(buf1));
    boolean rc = makeSpace(buf);
    assert !rc;
    buf.write(ch);
    check(buf, 1, 1, 0, 0);
    rc = makeSpace(buf);
    assert rc;
    check(buf, 0, 0, 0, 0);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers)

Example 13 with MockSocketChannel

use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.

the class BuffersTest method testSimpleWrite.

public void testSimpleWrite() throws Exception {
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(1000);
    Buffers buf = new Buffers(2);
    boolean rc = buf.write(ch, buf1.duplicate());
    assert rc;
    check(buf, 1, 1, 0, 0);
    rc = buf.write(ch, buf2.duplicate());
    assert rc;
    check(buf, 0, 0, 0, 0);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers)

Example 14 with MockSocketChannel

use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.

the class BuffersTest method testWriteAtStartup.

public void testWriteAtStartup() throws Exception {
    Buffers bufs = new Buffers(5);
    // all writes will initially fail
    MockSocketChannel ch = new MockSocketChannel();
    boolean rc = bufs.write(ch, buf1);
    assert !rc;
    check(bufs, 0, 1, 1, remaining(buf1));
    ch.bytesToWrite(100);
    rc = bufs.write(ch, buf2);
    assert rc;
    check(bufs, 2, 2, 0, 0);
    for (int i = 0; i < 3; i++) {
        ByteBuffer b = buf3.duplicate();
        rc = bufs.write(ch, b);
        assert rc;
    }
    check(bufs, 0, 0, 0, 0);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 15 with MockSocketChannel

use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.

the class BuffersTest method testWrite2.

public void testWrite2() throws Exception {
    ByteBuffer b = buffer();
    Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), null);
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(15);
    boolean rc = bufs.write(ch, b);
    assert rc;
    // write only a portion of the data
    ch.bytesToWrite(10);
    b.clear();
    rc = bufs.write(ch, b);
    assert rc == false;
    ch.bytesToWrite(10);
    rc = bufs.write(ch);
    assert rc;
    ch.bytesToWrite(10);
    // mimic a new buffer
    b = buffer();
    rc = bufs.write(ch, b);
    assert rc == false;
    ch.bytesToWrite(30);
    // mimic a new buffer
    b = buffer();
    rc = bufs.write(ch, b);
    assert rc;
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Buffers (org.jgroups.nio.Buffers)19 MockSocketChannel (org.jgroups.nio.MockSocketChannel)19 ByteBuffer (java.nio.ByteBuffer)11 IpAddress (org.jgroups.stack.IpAddress)2 ByteArrayDataOutputStream (org.jgroups.util.ByteArrayDataOutputStream)2 EOFException (java.io.EOFException)1