Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds non-blocking poll() for BytesSupplier #2478

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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