Search in sources :

Example 16 with MockSocketChannel

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

the class BuffersTest method testWrite.

public void testWrite() throws Exception {
    ByteBuffer b = buf1.duplicate();
    Buffers bufs = new Buffers(2);
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(15);
    boolean rc = bufs.write(ch, b);
    assert rc;
    check(bufs, 1, 1, 0, 0);
    // write only a portion of the data
    ch.bytesToWrite(10);
    b.clear();
    rc = bufs.write(ch, b);
    assert !rc;
    check(bufs, 1, 2, 1, b.remaining());
    ch.bytesToWrite(10);
    rc = bufs.write(ch);
    assert rc;
    check(bufs, 0, 0, 0, 0);
    ch.bytesToWrite(10);
    // mimic a new buffer
    b = buf1.duplicate();
    rc = bufs.write(ch, b);
    assert rc == false;
    check(bufs, 0, 1, 1, b.remaining());
    ch.bytesToWrite(30);
    // mimic a new buffer
    b = buf1.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 17 with MockSocketChannel

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

the class BuffersTest method testRead3.

public void testRead3() throws Exception {
    byte[] cookie = { 'b', 'e', 'l', 'a' };
    IpAddress addr = new IpAddress(7500);
    ByteArrayDataOutputStream out = new ByteArrayDataOutputStream();
    addr.writeTo(out);
    MockSocketChannel ch = new MockSocketChannel().bytesToRead((ByteBuffer) ByteBuffer.allocate(cookie.length + Global.SHORT_SIZE + out.position()).put(cookie).putShort(Version.version).put(out.buffer(), 0, out.position()).flip());
    int remaining = ch.bytesToRead().remaining();
    ch.bytesToRead().limit(remaining - 10);
    Buffers bufs = new Buffers(ByteBuffer.allocate(cookie.length), ByteBuffer.allocate(Global.SHORT_SIZE), ByteBuffer.allocate(out.position()));
    boolean rc = bufs.read(ch);
    assert !rc;
    ch.bytesToRead().limit(remaining - 2);
    rc = bufs.read(ch);
    assert !rc;
    ch.bytesToRead().limit(remaining);
    rc = bufs.read(ch);
    assert rc;
    readCookieVersionAndAddress(bufs, cookie, addr);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) IpAddress(org.jgroups.stack.IpAddress) Buffers(org.jgroups.nio.Buffers) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream)

Example 18 with MockSocketChannel

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

the class BuffersTest method testNullData.

public void testNullData() throws Exception {
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(1000);
    Buffers buf = new Buffers(3);
    Arrays.asList(buf1, buf2, buf3).forEach(buf::add);
    check(buf, 0, 3, 3, remaining(buf1, buf2, buf3));
    boolean rc = buf.write(ch);
    assert rc;
    // makeSpace(buf);
    check(buf, 0, 0, 0, 0);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers)

Example 19 with MockSocketChannel

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

the class BuffersTest method testMakeSpaceMoveTwo.

public void testMakeSpaceMoveTwo() throws Exception {
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(1000);
    Buffers buf = new Buffers(6);
    buf.add(buf1);
    buf.add(buf2);
    buf.write(ch);
    buf.add(buf3);
    buf.add(ByteBuffer.wrap("bla".getBytes()));
    ch.bytesToWrite(2);
    boolean rc = buf.write(ch);
    assert !rc;
    buf.copy();
    rc = makeSpace(buf);
    assert rc;
    check(buf, 0, 2, 2, remaining(buf3) + 3);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers)

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