use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.
the class BuffersTest method testEof.
public void testEof() throws Exception {
// -1 == EOF
byte[] data = { 'B', 'e', 'l', 'a' };
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 buf = bufs.readLengthAndData(ch);
assert buf != null;
assert buf.limit() == data.length;
ch.doClose();
try {
buf = bufs.readLengthAndData(ch);
assert false : "read() should have thrown an EOFException";
} catch (EOFException eof) {
System.out.printf("received exception as expected: %s\n", eof);
}
}
use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.
the class BuffersTest method copyHelper.
protected void copyHelper(int capacity, int num_buffers, int buffer_size, int bytes_to_write) throws Exception {
assert capacity > 0 && num_buffers <= capacity;
// the original data, will be used to compare after the copy()
byte[][] arrays = new byte[num_buffers][];
for (int i = 0; i < arrays.length; i++) arrays[i] = Util.generateArray(buffer_size);
ByteBuffer[] buffers = new ByteBuffer[num_buffers];
for (int i = 0; i < arrays.length; i++) {
// make a copy as we'll modify it later, so the original is not modified: we need it to compare later
byte[] tmp = Arrays.copyOf(arrays[i], arrays[i].length);
buffers[i] = ByteBuffer.wrap(tmp);
}
ByteBuffer recorder = ByteBuffer.allocate(Math.max(bytes_to_write, num_buffers * buffer_size));
MockSocketChannel ch = new MockSocketChannel().bytesToWrite(bytes_to_write).recorder(recorder);
Buffers bufs = new Buffers(capacity).add(buffers);
System.out.println("\nbufs = " + bufs);
assert bufs.size() == buffers.length;
boolean successful_write = bytes_to_write >= num_buffers * buffer_size;
boolean rc = bufs.write(ch);
assert rc == successful_write;
if (!successful_write) {
bufs.copy();
int num_bufs_not_written = (int) Math.ceil((num_buffers * buffer_size - bytes_to_write) / (double) buffer_size);
assertNotEqual(bufs, buffers, num_bufs_not_written);
// modify the buffers and compare the output to the original buffers (should match)
for (ByteBuffer buf : buffers) modifyBuffer(buf);
// the next write() will succeed
ch.bytesToWrite(num_buffers * buffer_size);
rc = bufs.write(ch);
assert rc;
// now compare the original buffers with the recorded bytes
recorder.flip();
for (byte[] original : arrays) {
byte[] actual = new byte[original.length];
recorder.get(actual);
assert Arrays.equals(original, actual);
}
}
}
use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.
the class BuffersTest method testCopyWithPartialWrite.
public void testCopyWithPartialWrite() throws Exception {
final String s1 = "hello", s2 = " world", s3 = " from ", s4 = "Bela";
ByteBuffer a = ByteBuffer.wrap(s1.getBytes()), b = ByteBuffer.wrap(s2.getBytes()), c = ByteBuffer.wrap(s3.getBytes()), d = ByteBuffer.wrap(s4.getBytes());
Buffers bufs = new Buffers(a, b, c, d);
ByteBuffer recorder = ByteBuffer.allocate(100);
MockSocketChannel ch = // a, b: OK, c: partial, d: fail
new MockSocketChannel().bytesToWrite(13).recorder(// we're recording the writes so we can compare expected bytes to actually written bytes
recorder);
boolean success = bufs.write(ch);
System.out.println("bufs = " + bufs);
assert !success;
assert bufs.position() == 2;
assert bufs.limit() == 4;
// copy the buffers which have not yet been written so that we can reuse buffers (not needed if buffers are already copies)
// https://issues.jboss.org/browse/JGRP-1991
bufs.copy();
makeSpace(bufs);
assert bufs.position() == 0;
assert bufs.limit() == 2;
assert bufs.nextToCopy() == 2;
// now modify the original buffers
for (ByteBuffer buf : Arrays.asList(a, b, c, d)) buf.putInt(0, 322649);
// next write will be successful
ch.bytesToWrite(100);
success = bufs.write(ch);
System.out.println("bufs = " + bufs);
assert success;
assert bufs.position() == 2;
assert bufs.limit() == 2;
// now compare the contents of the buffers
recorder.flip();
for (String s : Arrays.asList(s1, s2, s3, s4)) {
byte[] expected = s.getBytes();
byte[] actual = new byte[expected.length];
recorder.get(actual);
assert Arrays.equals(expected, actual) : String.format("expected %s, got %s\n", s, new String(actual));
}
}
use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.
the class BuffersTest method testPartialRead.
public void testPartialRead() throws Exception {
byte[] tmp = "hello world".getBytes();
ByteBuffer data = ByteBuffer.allocate(Global.INT_SIZE + tmp.length).putInt(tmp.length).put(tmp);
// read only the first 2 bytes of the length
data.flip().limit(2);
MockSocketChannel ch = new MockSocketChannel().bytesToRead(data);
Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), null);
ByteBuffer rc = bufs.readLengthAndData(ch);
assert rc == null;
// we can now read the remaining 2 bytes to complete the length, plus 4 bytes into the data
data.limit(8);
rc = bufs.readLengthAndData(ch);
assert rc == null;
// this will still not allow the read to complete
data.limit(14);
rc = bufs.readLengthAndData(ch);
assert rc == null;
// this will still not allow the read to complete
data.limit(15);
rc = bufs.readLengthAndData(ch);
assert rc != null;
System.out.println("rc = " + rc);
assert Arrays.equals(tmp, rc.array());
}
use of org.jgroups.nio.MockSocketChannel in project JGroups by belaban.
the class BuffersTest method testRead3Buffers.
/**
* Reads | version (short) | number (int) | cookie [4 bytes] |
*/
public void testRead3Buffers() throws Exception {
byte[] cookie = { 'b', 'e', 'l', 'a' };
int num = 322649;
ByteBuffer input = (ByteBuffer) ByteBuffer.allocate(Global.SHORT_SIZE + cookie.length + Global.INT_SIZE).putShort(Version.version).putInt(num).put(cookie, 0, cookie.length).flip();
MockSocketChannel ch = new MockSocketChannel().bytesToRead(input);
Buffers bufs = new Buffers(ByteBuffer.allocate(2), ByteBuffer.allocate(4), ByteBuffer.allocate(4));
boolean rc = bufs.read(ch);
System.out.println("bufs = " + bufs);
assert rc;
assert bufs.position() == 3;
assert bufs.limit() == 3;
for (// reset position so data can be read
ByteBuffer b : // reset position so data can be read
bufs) b.clear();
short version = bufs.get(0).getShort(0);
assert version == Version.version;
int num2 = bufs.get(1).getInt(0);
assert num2 == num;
byte[] tmp = new byte[4];
bufs.get(2).get(tmp, 0, tmp.length);
assert Arrays.equals(tmp, cookie);
}
Aggregations