|
16 | 16 | import org.opensearch.test.rest.OpenSearchRestTestCase;
|
17 | 17 | import org.junit.After;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.io.InterruptedIOException;
|
| 21 | +import java.io.UncheckedIOException; |
20 | 22 | import java.nio.ByteBuffer;
|
21 | 23 | import java.nio.charset.StandardCharsets;
|
22 | 24 | import java.time.Duration;
|
23 |
| -import java.util.concurrent.Executors; |
24 |
| -import java.util.concurrent.ScheduledExecutorService; |
25 |
| -import java.util.concurrent.TimeUnit; |
26 | 25 | import java.util.concurrent.atomic.AtomicInteger;
|
27 | 26 | import java.util.stream.Stream;
|
28 | 27 |
|
29 | 28 | import reactor.core.publisher.Flux;
|
| 29 | +import reactor.test.StepVerifier; |
30 | 30 | import reactor.test.subscriber.TestSubscriber;
|
31 | 31 |
|
32 |
| -import static org.hamcrest.CoreMatchers.anyOf; |
33 | 32 | import static org.hamcrest.CoreMatchers.equalTo;
|
34 |
| -import static org.hamcrest.CoreMatchers.instanceOf; |
35 |
| -import static org.hamcrest.CoreMatchers.not; |
36 |
| -import static org.hamcrest.collection.IsEmptyCollection.empty; |
37 | 33 |
|
38 | 34 | public class ReactorNetty4StreamingStressIT extends OpenSearchRestTestCase {
|
39 | 35 | @After
|
@@ -68,28 +64,15 @@ public void testCloseClientStreamingRequest() throws Exception {
|
68 | 64 | TestSubscriber<ByteBuffer> subscriber = TestSubscriber.create();
|
69 | 65 | streamingResponse.getBody().subscribe(subscriber);
|
70 | 66 |
|
71 |
| - final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); |
72 |
| - try { |
73 |
| - // Await for subscriber to receive at least one chunk |
74 |
| - assertBusy(() -> assertThat(subscriber.getReceivedOnNext(), not(empty()))); |
75 |
| - |
76 |
| - // Close client forceably |
77 |
| - executor.schedule(() -> { |
78 |
| - client().close(); |
79 |
| - return null; |
80 |
| - }, 2, TimeUnit.SECONDS); |
81 |
| - |
82 |
| - // Await for subscriber to terminate |
83 |
| - subscriber.block(Duration.ofSeconds(10)); |
84 |
| - assertThat( |
85 |
| - subscriber.expectTerminalError(), |
86 |
| - anyOf(instanceOf(InterruptedIOException.class), instanceOf(ConnectionClosedException.class)) |
87 |
| - ); |
88 |
| - } finally { |
89 |
| - executor.shutdown(); |
90 |
| - if (executor.awaitTermination(1, TimeUnit.SECONDS) == false) { |
91 |
| - executor.shutdownNow(); |
92 |
| - } |
93 |
| - } |
| 67 | + StepVerifier.create(Flux.from(streamingResponse.getBody()).map(b -> new String(b.array(), StandardCharsets.UTF_8))) |
| 68 | + .expectNextMatches(s -> s.contains("\"result\":\"created\"") && s.contains("\"_id\":\"1\"")) |
| 69 | + .then(() -> { |
| 70 | + try { |
| 71 | + client().close(); |
| 72 | + } catch (final IOException ex) { |
| 73 | + throw new UncheckedIOException(ex); |
| 74 | + } |
| 75 | + }) |
| 76 | + .expectErrorMatches(t -> t instanceof InterruptedIOException || t instanceof ConnectionClosedException); |
94 | 77 | }
|
95 | 78 | }
|
0 commit comments