-
Notifications
You must be signed in to change notification settings - Fork 21
fix: don't signal onComplete when a chunk is fully consumed by the ra… #523
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
Open
sharmabikram
wants to merge
1
commit into
main
Choose a base branch
from
shbikram/adjustedrange-premature-oncomplete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/test/java/software/amazon/encryption/s3/legacy/internal/AdjustedRangeSubscriberTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.encryption.s3.legacy.internal; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.reactivestreams.Subscriber; | ||
| import org.reactivestreams.Subscription; | ||
|
|
||
| public class AdjustedRangeSubscriberTest { | ||
|
|
||
| /** | ||
| * Records everything delivered downstream so tests can assert on bytes and | ||
| * terminal signals. | ||
| */ | ||
| private static class RecordingSubscriber implements Subscriber<ByteBuffer> { | ||
| final ByteArrayOutputStream data = new ByteArrayOutputStream(); | ||
| final AtomicInteger completeCount = new AtomicInteger(); | ||
| final AtomicInteger onNextCount = new AtomicInteger(); | ||
| Throwable error; | ||
|
|
||
| @Override | ||
| public void onSubscribe(Subscription s) { | ||
| } | ||
|
|
||
| @Override | ||
| public void onNext(ByteBuffer byteBuffer) { | ||
| onNextCount.incrementAndGet(); | ||
| byte[] b = new byte[byteBuffer.remaining()]; | ||
| byteBuffer.get(b); | ||
| data.write(b, 0, b.length); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable t) { | ||
| error = t; | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() { | ||
| completeCount.incrementAndGet(); | ||
| } | ||
| } | ||
|
|
||
| private static ByteBuffer bytes(int start, int length) { | ||
| byte[] b = new byte[length]; | ||
| for (int i = 0; i < length; i++) { | ||
| b[i] = (byte) (start + i); | ||
| } | ||
| return ByteBuffer.wrap(b); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFirstChunkSmallerThanSkipDoesNotCompleteOrThrow() throws Exception { | ||
| // rangeBeginning=20 => numBytesToSkip=20, virtualAvailable=100 | ||
| RecordingSubscriber downstream = new RecordingSubscriber(); | ||
| AdjustedRangeSubscriber subscriber = new AdjustedRangeSubscriber(downstream, 20L, 119L); | ||
|
|
||
| // First chunk (10 bytes) is smaller than the 20-byte skip. | ||
| subscriber.onNext(bytes(0, 10)); | ||
| assertEquals(0, downstream.completeCount.get()); | ||
| assertNull(downstream.error); | ||
| assertEquals(0, downstream.data.size()); | ||
|
|
||
| // Second chunk (10 bytes) exactly finishes the skip; still no data delivered. | ||
| subscriber.onNext(bytes(10, 10)); | ||
| assertEquals(0, downstream.completeCount.get()); | ||
| assertEquals(0, downstream.data.size()); | ||
|
|
||
| // Third chunk carries the actual payload, which must now be delivered. | ||
| subscriber.onNext(bytes(100, 100)); | ||
| assertArrayEquals(bytes(100, 100).array(), downstream.data.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyFirstChunkIsSkippedNotTreatedAsCompletion() throws Exception { | ||
| RecordingSubscriber downstream = new RecordingSubscriber(); | ||
| AdjustedRangeSubscriber subscriber = new AdjustedRangeSubscriber(downstream, 20L, 119L); | ||
|
|
||
| // CipherSubscriber can emit an empty buffer; it must not complete the stream. | ||
| subscriber.onNext(ByteBuffer.allocate(0)); | ||
| assertEquals(0, downstream.completeCount.get()); | ||
| assertNull(downstream.error); | ||
|
|
||
| subscriber.onNext(bytes(0, 20)); // finish the skip | ||
| subscriber.onNext(bytes(50, 100)); | ||
| assertArrayEquals(bytes(50, 100).array(), downstream.data.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSkipOnlyChunkSignalsEmptyOnNextToKeepDemandFlowing() throws Exception { | ||
| // Under one-at-a-time (request(1)) demand, a chunk fully consumed by the skip must still | ||
| // signal the wrapped subscriber, or downstream waits forever for the next element. The | ||
| // subscriber forwards an empty buffer (no in-range data, but the demand chain advances). | ||
| RecordingSubscriber downstream = new RecordingSubscriber(); | ||
| AdjustedRangeSubscriber subscriber = new AdjustedRangeSubscriber(downstream, 20L, 119L); | ||
|
|
||
| subscriber.onNext(bytes(0, 10)); // 10 bytes < 20-byte skip: entirely skipped | ||
|
|
||
| assertEquals(1, downstream.onNextCount.get(), "skip-only chunk must forward exactly one onNext"); | ||
| assertEquals(0, downstream.data.size(), "the forwarded onNext must carry no in-range bytes"); | ||
| assertEquals(0, downstream.completeCount.get(), "skip-only chunk must not complete the stream"); | ||
| assertNull(downstream.error); | ||
| } | ||
|
|
||
| @Test | ||
| public void testChunkLargerThanSkipDeliversRemainder() throws Exception { | ||
| RecordingSubscriber downstream = new RecordingSubscriber(); | ||
| AdjustedRangeSubscriber subscriber = new AdjustedRangeSubscriber(downstream, 20L, 119L); | ||
|
|
||
| // A single 120-byte chunk: 20 skipped, 100 delivered. | ||
| subscriber.onNext(bytes(0, 120)); | ||
| assertArrayEquals(bytes(20, 100).array(), downstream.data.toByteArray()); | ||
| assertFalse(downstream.completeCount.get() == 0); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure we still need to signal something to the subscriber. I don't know what it would be but it feels really weird that we would go from telling the subscriber "done" to not telling the subscriber anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe signal wrappedSubscriber's
onNextwith an empty buffer? Since that's really what's going on here