/** * Issues a request * - if there is enough room for another request, and we aren't already waiting on one and * - if we have not read up to the configured endOffset. */ private void issueRequestIfNeeded() { //compute read length based on current offset up to which the events are read. int updatedReadLength = computeReadLength(offset + buffer.dataAvailable()); if (!receivedEndOfSegment && !receivedTruncated && updatedReadLength > 0 && outstandingRequest == null) { log.trace("Issuing read request for segment {} of {} bytes", getSegmentId(), updatedReadLength); outstandingRequest = asyncInput.read(offset + buffer.dataAvailable(), updatedReadLength); } }
@Test public void testReadWithEndOffsetWithDataGreaterThanBuffer() throws Exception { byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); int wireDataSize = wireData.remaining(); //size of the data with header size. int bufferSize = wireDataSize / 2; //buffer is half the data length AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); when(mockAsyncInputStream.read(0, bufferSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, bufferSize)))); when(mockAsyncInputStream.read(bufferSize, wireDataSize - bufferSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), bufferSize, false, false, ByteBufferUtils.slice(wireData, bufferSize, wireDataSize - bufferSize)))); //Create a SegmentInputStream where the Buffer can hold only part of the data. @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, wireDataSize, bufferSize); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data), read); //verify we are reading the data. verify(mockAsyncInputStream, times(1)).read(0L, bufferSize); verify(mockAsyncInputStream, times(1)).read(bufferSize, wireDataSize - bufferSize); }
@Test public void testReadWithEndOffsetWithSmallerReads() throws Exception { byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); int wireDataSize = wireData.remaining(); //size of the data with header size. AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); when(mockAsyncInputStream.read(0, wireDataSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, 2)))); when(mockAsyncInputStream.read(2, 16)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 2, false, false, ByteBufferUtils.slice(wireData, 2, wireDataSize - 2)))); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, wireDataSize, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data), read); //verify we are reading the data. verify(mockAsyncInputStream, times(1)).read(0L, wireDataSize); verify(mockAsyncInputStream, times(1)).read(2L, wireDataSize - 2); }
@Test(expected = EndOfSegmentException.class) public void testReadWithEndOffset() throws Exception { byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); int wireDataSize = wireData.remaining(); //size of the data with header size. AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); when(mockAsyncInputStream.read(0, wireDataSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, wireDataSize, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data), read); //verify we are reading the data. verify(mockAsyncInputStream, times(1)).read(0L, wireDataSize); //ensure there is one invocation. stream.read(); // this should throw EndOfSegmentExceptiono as we have reached the endOffset }
when(mockAsyncInputStream.read(0, bufferSize)).thenReturn( completedFuture(new SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); when(mockAsyncInputStream.read(wireDataSize, expectedReadSize)).thenReturn( completedFuture(new SegmentRead(segment.getScopedName(), wireDataSize, false, false, wireData.slice()))); verify(mockAsyncInputStream, times(1)).read(0L, bufferSize); verify(mockAsyncInputStream, times(1)).read(wireDataSize, expectedReadSize); when(mockAsyncInputStream.read(0, wireDataSize)).thenReturn( completedFuture(new SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); verify(mockAsyncInputStream, times(1)).read(0L, wireDataSize); when(mockAsyncInputStream.read(0, 100)).thenReturn( completedFuture(new SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); @Cleanup verify(mockAsyncInputStream, times(1)).read(0L, 100);