|
20 | 20 | import static org.mockito.Mockito.mock;
|
21 | 21 | import static org.mockito.Mockito.times;
|
22 | 22 | import static org.mockito.Mockito.verify;
|
| 23 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
23 | 24 | import static org.mockito.Mockito.when;
|
24 | 25 | import static software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.EXECUTE_FUTURE_KEY;
|
25 | 26 | import static software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.PROTOCOL_FUTURE;
|
26 | 27 | import static software.amazon.awssdk.http.nio.netty.internal.ChannelAttributeKey.REQUEST_CONTEXT_KEY;
|
27 | 28 |
|
28 | 29 | import io.netty.buffer.ByteBufAllocator;
|
29 | 30 | import io.netty.buffer.EmptyByteBuf;
|
| 31 | +import io.netty.buffer.Unpooled; |
30 | 32 | import io.netty.channel.ChannelHandlerContext;
|
31 | 33 | import io.netty.channel.EventLoopGroup;
|
32 | 34 | import io.netty.handler.codec.http.DefaultHttpContent;
|
|
43 | 45 | import org.junit.runner.RunWith;
|
44 | 46 | import org.mockito.Mock;
|
45 | 47 | import org.mockito.runners.MockitoJUnitRunner;
|
| 48 | +import org.reactivestreams.Publisher; |
46 | 49 | import org.reactivestreams.Subscriber;
|
47 | 50 | import org.reactivestreams.Subscription;
|
48 | 51 | import software.amazon.awssdk.http.Protocol;
|
@@ -155,6 +158,75 @@ public void errorOccurred_shouldInvokeResponseHandler() {
|
155 | 158 | verify(responseHandler).onError(exception);
|
156 | 159 | }
|
157 | 160 |
|
| 161 | + @Test |
| 162 | + public void subscriptionCancelled_upstreamPublisherCallsOnNext_httpContentReleased() { |
| 163 | + HttpContent firstContent = mock(HttpContent.class); |
| 164 | + when(firstContent.content()).thenReturn(Unpooled.EMPTY_BUFFER); |
| 165 | + |
| 166 | + HttpContent[] contentToIgnore = new HttpContent[8]; |
| 167 | + for (int i = 0; i < contentToIgnore.length; ++i) { |
| 168 | + contentToIgnore[i] = mock(HttpContent.class); |
| 169 | + when(contentToIgnore[i].content()).thenReturn(Unpooled.EMPTY_BUFFER); |
| 170 | + } |
| 171 | + |
| 172 | + Publisher<HttpContent> publisher = subscriber -> subscriber.onSubscribe(new Subscription() { |
| 173 | + @Override |
| 174 | + public void request(long l) { |
| 175 | + // We ignore any cancel signal and just publish all the content |
| 176 | + subscriber.onNext(firstContent); |
| 177 | + |
| 178 | + for (int i = 0; i < l && i < contentToIgnore.length; ++i) { |
| 179 | + subscriber.onNext(contentToIgnore[i]); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void cancel() { |
| 185 | + // no-op |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + DefaultStreamedHttpResponse streamedResponse = new DefaultStreamedHttpResponse(HttpVersion.HTTP_1_1, |
| 190 | + HttpResponseStatus.OK, publisher); |
| 191 | + |
| 192 | + Subscriber<ByteBuffer> subscriber = new Subscriber<ByteBuffer>() { |
| 193 | + private Subscription subscription; |
| 194 | + |
| 195 | + @Override |
| 196 | + public void onSubscribe(Subscription subscription) { |
| 197 | + this.subscription = subscription; |
| 198 | + subscription.request(Long.MAX_VALUE); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void onNext(ByteBuffer byteBuffer) { |
| 203 | + subscription.cancel(); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void onError(Throwable throwable) { |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public void onComplete() { |
| 212 | + } |
| 213 | + }; |
| 214 | + |
| 215 | + ResponseHandler.PublisherAdapter publisherAdapter = new ResponseHandler.PublisherAdapter(streamedResponse, ctx, |
| 216 | + requestContext, executeFuture); |
| 217 | + |
| 218 | + publisherAdapter.subscribe(subscriber); |
| 219 | + |
| 220 | + // First one should be accessed as normal |
| 221 | + verify(firstContent).content(); |
| 222 | + verify(firstContent).release(); |
| 223 | + |
| 224 | + for (int i = 0; i < contentToIgnore.length; ++i) { |
| 225 | + verify(contentToIgnore[i]).release(); |
| 226 | + verifyNoMoreInteractions(contentToIgnore[i]); |
| 227 | + } |
| 228 | + } |
| 229 | + |
158 | 230 | static final class TestSubscriber implements Subscriber<ByteBuffer> {
|
159 | 231 |
|
160 | 232 | private Subscription subscription;
|
|
0 commit comments