|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
19 | 19 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.verify; |
20 | 22 |
|
21 | 23 | import io.reactivex.Flowable;
|
22 | 24 | import java.io.IOException;
|
23 | 25 | import java.nio.ByteBuffer;
|
24 | 26 | import java.util.Arrays;
|
25 | 27 | import java.util.List;
|
| 28 | +import java.util.Optional; |
| 29 | +import java.util.concurrent.CompletableFuture; |
26 | 30 | import java.util.stream.Collectors;
|
27 | 31 | import java.util.stream.Stream;
|
28 | 32 | import org.junit.Test;
|
29 | 33 | import org.reactivestreams.Publisher;
|
30 | 34 | import org.reactivestreams.Subscriber;
|
| 35 | +import org.reactivestreams.Subscription; |
31 | 36 | import software.amazon.awssdk.core.async.AsyncRequestBody;
|
| 37 | +import software.amazon.awssdk.crt.CrtRuntimeException; |
32 | 38 |
|
33 | 39 | public class RequestDataSupplierAdapterTest {
|
34 | 40 |
|
@@ -156,4 +162,56 @@ public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
|
156 | 162 | assertThat(readBuffer).isEqualTo(expectedBufferContent);
|
157 | 163 | }
|
158 | 164 | }
|
| 165 | + |
| 166 | + @Test |
| 167 | + public void onException_cancelsSubscription() { |
| 168 | + Subscription subscription = mock(Subscription.class); |
| 169 | + |
| 170 | + AsyncRequestBody requestBody = new AsyncRequestBody() { |
| 171 | + @Override |
| 172 | + public Optional<Long> contentLength() { |
| 173 | + return Optional.empty(); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void subscribe(Subscriber<? super ByteBuffer> subscriber) { |
| 178 | + subscriber.onSubscribe(subscription); |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | + RequestDataSupplierAdapter adapter = new RequestDataSupplierAdapter(requestBody); |
| 183 | + |
| 184 | + // getRequestBytes() triggers a subscribe() on the publisher |
| 185 | + adapter.getRequestBytes(ByteBuffer.allocate(0)); |
| 186 | + |
| 187 | + adapter.onException(new CrtRuntimeException("error")); |
| 188 | + |
| 189 | + verify(subscription).cancel(); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void onFinished_cancelsSubscription() { |
| 194 | + Subscription subscription = mock(Subscription.class); |
| 195 | + |
| 196 | + AsyncRequestBody requestBody = new AsyncRequestBody() { |
| 197 | + @Override |
| 198 | + public Optional<Long> contentLength() { |
| 199 | + return Optional.empty(); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public void subscribe(Subscriber<? super ByteBuffer> subscriber) { |
| 204 | + subscriber.onSubscribe(subscription); |
| 205 | + } |
| 206 | + }; |
| 207 | + |
| 208 | + RequestDataSupplierAdapter adapter = new RequestDataSupplierAdapter(requestBody); |
| 209 | + |
| 210 | + // getRequestBytes() triggers a subscribe() on the publisher |
| 211 | + adapter.getRequestBytes(ByteBuffer.allocate(0)); |
| 212 | + |
| 213 | + adapter.onFinished(); |
| 214 | + |
| 215 | + verify(subscription).cancel(); |
| 216 | + } |
159 | 217 | }
|
0 commit comments