Skip to content

Commit

Permalink
Adds non-blocking poll() for BytesSupplier (#2478)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Mar 24, 2023
1 parent fb304a3 commit 6f5bb95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions api/src/main/java/ai/djl/modality/ChunkedBytesSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public byte[] nextChunk(long timeout, TimeUnit unit) throws InterruptedException
return data.getAsBytes();
}

/**
* Retrieves and removes the head of chunk or returns {@code null} if data is not available.
*
* @return the head of chunk or returns {@code null} if data is not available
*/
public byte[] poll() {
BytesSupplier data = queue.poll();
return data == null ? null : data.getAsBytes();
}

/** {@inheritDoc} */
@Override
public byte[] getAsBytes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ChunkedBytesSupplierTest {
@Test
public void test() throws InterruptedException, IOException {
ChunkedBytesSupplier supplier = new ChunkedBytesSupplier();
Assert.assertNull(supplier.poll());
Assert.assertThrows(() -> supplier.nextChunk(1, TimeUnit.MICROSECONDS));

supplier.appendContent(new byte[] {1, 2}, false);
Expand All @@ -37,7 +38,7 @@ public void test() throws InterruptedException, IOException {
data.appendContent(new byte[] {1, 2}, true);

Assert.assertTrue(data.hasNext());
Assert.assertEquals(data.nextChunk(1, TimeUnit.MILLISECONDS).length, 0);
Assert.assertEquals(data.poll().length, 0);
Assert.assertEquals(data.nextChunk(1, TimeUnit.MILLISECONDS), new byte[] {1, 2});

Assert.assertFalse(data.hasNext());
Expand Down

0 comments on commit 6f5bb95

Please sign in to comment.