use of io.helidon.webserver.utils.TestUtils.requestChunkAsString in project helidon by oracle.
the class RequestContentTest method multiThreadingFilterAndReaderTest.
@SuppressWarnings("unchecked")
@Test
public void multiThreadingFilterAndReaderTest() throws Exception {
CountDownLatch subscribedLatch = new CountDownLatch(1);
SubmissionPublisher<DataChunk> publisher = new SubmissionPublisher<>(Runnable::run, 10);
ForkJoinPool.commonPool().submit(() -> {
try {
if (!subscribedLatch.await(10, TimeUnit.SECONDS)) {
fail("Subscriber didn't subscribe in timely manner!");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted!", e);
}
publisher.submit(DataChunk.create("first".getBytes()));
publisher.submit(DataChunk.create("second".getBytes()));
publisher.submit(DataChunk.create("third".getBytes()));
publisher.close();
});
Request request = requestTestStub(Multi.create(publisher));
request.content().registerFilter(originalPublisher -> subscriberDelegate -> originalPublisher.subscribe(new Subscriber<DataChunk>() {
@Override
public void onSubscribe(Subscription subscription) {
subscriberDelegate.onSubscribe(subscription);
subscribedLatch.countDown();
}
@Override
public void onNext(DataChunk item) {
// mapping the on next call only
subscriberDelegate.onNext(DataChunk.create(requestChunkAsString(item).toUpperCase().getBytes()));
}
@Override
public void onError(Throwable throwable) {
subscriberDelegate.onError(throwable);
}
@Override
public void onComplete() {
subscriberDelegate.onComplete();
}
}));
request.content().registerReader(Iterable.class, (publisher1, clazz) -> {
fail("Iterable reader should have not been used!");
throw new IllegalStateException("unreachable code");
});
request.content().registerReader(ArrayList.class, (publisher1, clazz) -> {
fail("ArrayList reader should have not been used!");
throw new IllegalStateException("unreachable code");
});
request.content().registerReader(List.class, (publisher1, clazz) -> {
CompletableFuture<List> future = new CompletableFuture<>();
List<String> list = new CopyOnWriteArrayList<>();
publisher1.subscribe(new Subscriber<DataChunk>() {
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(Long.MAX_VALUE);
subscribedLatch.countDown();
}
@Override
public void onNext(DataChunk item) {
list.add(TestUtils.requestChunkAsString(item));
}
@Override
public void onError(Throwable throwable) {
fail("Received an exception: " + throwable.getMessage());
}
@Override
public void onComplete() {
future.complete(list);
}
});
return future;
});
List<String> result = (List<String>) request.content().as(List.class).toCompletableFuture().get(10, TimeUnit.SECONDS);
assertThat(result, hasItems(is("FIRST"), is("SECOND"), is("THIRD")));
}
Aggregations