/**
   * 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);
  }
Beispiel #3
0
 /** {@inheritDoc} */
 public void decodeHeader() {
   // XXX check signature?
   // SIGNATURE, lets just skip
   fillBuffer(9);
   header = new FLVHeader();
   in.skip(3);
   header.setVersion(in.get());
   header.setTypeFlags(in.get());
   header.setDataOffset(in.getInt());
   if (log.isDebugEnabled()) {
     log.debug("Header: " + header.toString());
   }
 }