/** * Tests that complete messages are delivered even when the message arrives one byte at a time * from the network. Then tests that a second message can still be received all-at-once. */ public void testPartialOneByteReceives() { int len = 32; byte[] expected = getByteSequence(len); ByteBuffer buf = ByteBuffer.allocate(len + 4, false); buf.putInt(len); buf.put(expected); buf = buf.asReadOnlyBuffer(); buf.flip(); for (; buf.hasRemaining(); buf.skip(1)) { assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); ByteBuffer singleByte = buf.slice().limit(1); harness.recv(singleByte); } assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); byte[] actual = receivedMessages.get(0); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); harness.recv(buf.rewind()); assertEquals(0, sentMessages.size()); assertEquals(2, receivedMessages.size()); actual = receivedMessages.get(1); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); }
/** Tests handling of exceptions during dispatch. */ public void testReceiveHandlingExceptionPartial() { int len1 = 400; int len2 = 600; int len = len1 + len2; byte[] expected = getByteSequence(len); ByteBuffer buf = ByteBuffer.allocate(len + 4, false); buf.putInt(len); buf.put(expected); buf = buf.asReadOnlyBuffer(); buf.flip(); assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); ByteBuffer part1 = buf.slice().limit(len1 + 4); ByteBuffer part2 = ByteBuffer.allocate(len2 + len2 + 4, false); buf.rewind(); buf.skip(len1 + 4); part2.put(buf); buf.rewind(); buf.skip(len1 + 4); part2.putInt(len2); part2.put(buf); part2.flip(); assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); harness.recv(part1); assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); RuntimeException expectedEx = new RuntimeException("Dummy exception for testing filter recv"); harness.setExceptionOnNextCompleteMessage(expectedEx); // This recv should process the second message. harness.recv(part2); assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); byte[] actual = receivedMessages.get(0); assertEquals(len2, actual.length); }
/** Tests that zero-byte receives have no effect on complete message delivery. */ public void testPartialReceiveNoBytes() { int len = 1000; sizeBuf.putInt(len).flip(); harness.recv(sizeBuf.asReadOnlyBuffer()); ByteBuffer emptyBuf = sizeBuf.slice().limit(0).asReadOnlyBuffer(); harness.recv(emptyBuf); assertEquals(0, sentMessages.size()); assertEquals(0, receivedMessages.size()); byte[] expected = getByteSequence(len); ByteBuffer buf = ByteBuffer.wrap(expected).asReadOnlyBuffer(); harness.recv(buf); assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); byte[] actual = receivedMessages.get(0); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); harness.recv(emptyBuf); assertEquals(0, sentMessages.size()); assertEquals(1, receivedMessages.size()); harness.recv(sizeBuf.rewind().asReadOnlyBuffer()); harness.recv(buf.rewind()); assertEquals(0, sentMessages.size()); assertEquals(2, receivedMessages.size()); actual = receivedMessages.get(1); assertTrue("Incorrect recv!", Arrays.equals(actual, expected)); }