Search in sources :

Example 6 with MockSocketChannel

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

the class BuffersTest method testRead2.

public void testRead2() 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());
    Buffers bufs = new Buffers(ByteBuffer.allocate(cookie.length), ByteBuffer.allocate(Global.SHORT_SIZE), ByteBuffer.allocate(out.position()));
    boolean 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 7 with MockSocketChannel

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

the class BuffersTest method testMakeSpaceSimpleMove.

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

Example 8 with MockSocketChannel

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

the class BuffersTest method testRead.

public void testRead() throws Exception {
    byte[] data = "hello world".getBytes();
    MockSocketChannel ch = new MockSocketChannel().bytesToRead((ByteBuffer) ByteBuffer.allocate(Global.INT_SIZE + data.length).putInt(data.length).put(data).flip());
    Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), null);
    ByteBuffer b = bufs.readLengthAndData(ch);
    System.out.println("b = " + b);
    assert b != null;
    assert Arrays.equals(data, b.array());
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 9 with MockSocketChannel

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

the class BuffersTest method testMakeSpaceOverlappingMove.

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

Example 10 with MockSocketChannel

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

the class BuffersTest method testWriteWithBuffering.

public void testWriteWithBuffering() throws Exception {
    Buffers bufs = new Buffers(4);
    // all writes will fail
    MockSocketChannel ch = new MockSocketChannel();
    for (int i = 0; i < 4; i++) {
        ByteBuffer b = buf1.duplicate();
        boolean rc = bufs.write(ch, b);
        assert !rc;
    }
    check(bufs, 0, 4, 4, remaining(buf1) * 4);
    ch.bytesToWrite(20);
    boolean rc = bufs.write(ch);
    assert !rc;
    rc = bufs.write(ch, buf1.duplicate());
    assert !rc;
    check(bufs, 0, 4, 4, remaining(buf1) * 5 - 20);
    // write all buffers now
    ch.bytesToWrite(1000);
    rc = bufs.write(ch);
    assert rc;
    check(bufs, 0, 0, 0, 0);
    rc = bufs.write(ch, buf1.duplicate());
    assert rc;
    check(bufs, 1, 1, 0, 0);
}
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