Search in sources :

Example 1 with TestUtils.requestChunkAsString

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")));
}
Also used : SubmissionPublisher(java.util.concurrent.SubmissionPublisher) TestUtils.requestChunkAsString(io.helidon.webserver.utils.TestUtils.requestChunkAsString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) Subscriber(java.util.concurrent.Flow.Subscriber) DataChunk(io.helidon.common.http.DataChunk) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Subscription(java.util.concurrent.Flow.Subscription) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

DataChunk (io.helidon.common.http.DataChunk)1 TestUtils.requestChunkAsString (io.helidon.webserver.utils.TestUtils.requestChunkAsString)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Subscriber (java.util.concurrent.Flow.Subscriber)1 Subscription (java.util.concurrent.Flow.Subscription)1 SubmissionPublisher (java.util.concurrent.SubmissionPublisher)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1 Test (org.junit.jupiter.api.Test)1