Skip to content

Commit

Permalink
add a test simulating the scenario when we read less than 6 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdumandag committed May 31, 2022
1 parent ebad80f commit 0ce0df0
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void testReadFramesInMultipleCallsToReadFrom_whenLastPieceIsSmall() {
buffer.get(part2, 0, 252);

// Message Length = 1000 + 6 bytes
// part1 = 750, part2 = 250, part3 = 4 bytes
// part1 = 750, part2 = 252, part3 = 4 bytes
byte[] part3 = new byte[buffer.remaining()];
buffer.get(part3);

Expand All @@ -165,6 +165,39 @@ public void testReadFramesInMultipleCallsToReadFrom_whenLastPieceIsSmall() {
assertFalse(iterator.hasNext());
}

@Test
public void testRead_whenTheFrameLengthAndFlagsNotReceivedAtFirst() {
ClientMessage.Frame frame = createFrameWithRandomBytes(100);

ClientMessage message = ClientMessage.createForEncode();
message.add(frame);

ByteBuffer buffer = writeToBuffer(message);
int capacity = buffer.capacity();
// Set limit to a small value so that we can simulate
// that the frame length and flags are not read yet.
buffer.limit(4);

ClientMessageReader reader = new ClientMessageReader(-1);

// should not be able to read with just 4 bytes of data
assertFalse(reader.readFrom(buffer, true));

buffer.limit(capacity);

// should be able to read when the rest of the data comes
assertTrue(reader.readFrom(buffer, true));

ClientMessage messageRead = reader.getClientMessage();
ClientMessage.ForwardFrameIterator iterator = messageRead.frameIterator();

assertTrue(iterator.hasNext());
ClientMessage.Frame frameRead = iterator.next();
assertArrayEquals(frame.content, frameRead.content);

assertFalse(iterator.hasNext());
}

private ClientMessage.Frame createFrameWithRandomBytes(int contentLength) {
byte[] content = new byte[contentLength];
random.nextBytes(content);
Expand Down

0 comments on commit 0ce0df0

Please sign in to comment.