Search in sources :

Example 6 with Subscriber

use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.

the class SingleTest method testToFutureDoubleOnNext.

@Test
public void testToFutureDoubleOnNext() throws InterruptedException, ExecutionException {
    Single<String> single = new CompletionSingle<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                    subscriber.onNext("foo");
                    subscriber.onNext("bar");
                    subscriber.onComplete();
                }

                @Override
                public void cancel() {
                }
            });
        }
    };
    Future<String> future = single.toStage().toCompletableFuture();
    assertThat(future.isDone(), is(equalTo(true)));
    assertThat(future.get(), is(equalTo("foo")));
}
Also used : Subscriber(java.util.concurrent.Flow.Subscriber) Subscription(java.util.concurrent.Flow.Subscription) Test(org.junit.jupiter.api.Test)

Example 7 with Subscriber

use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.

the class MultiPartDecoderTest method testContentAcrossChunks.

@Test
public void testContentAcrossChunks() {
    String boundary = "boundary";
    final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "this-is-the-1st-slice-of-the-body\n").getBytes();
    final byte[] chunk2 = ("this-is-the-2nd-slice-of-the-body\n" + "--" + boundary + "--").getBytes();
    final CountDownLatch latch = new CountDownLatch(2);
    Consumer<BodyPart> consumer = (part) -> {
        latch.countDown();
        assertThat(part.headers().values("Content-Id"), hasItems("part1"));
        DataChunkSubscriber subscriber = new DataChunkSubscriber();
        part.content().subscribe(subscriber);
        subscriber.content().thenAccept(body -> {
            latch.countDown();
            assertThat(body, is(equalTo("this-is-the-1st-slice-of-the-body\n" + "this-is-the-2nd-slice-of-the-body")));
        });
    };
    BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.INFINITE, consumer);
    partsPublisher(boundary, List.of(chunk1, chunk2)).subscribe(testSubscriber);
    try {
        boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
        assertThat(b, is(equalTo(true)));
    } catch (CompletionException error) {
        assertThat(error, is(nullValue()));
    }
    waitOnLatch(latch);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CoreMatchers.hasItems(org.hamcrest.CoreMatchers.hasItems) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Publisher(java.util.concurrent.Flow.Publisher) Subscriber(java.util.concurrent.Flow.Subscriber) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Multi(io.helidon.common.reactive.Multi) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CompletionException(java.util.concurrent.CompletionException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 8 with Subscriber

use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.

the class MultiPartDecoderTest method testOnePartInOneChunk.

@Test
public void testOnePartInOneChunk() {
    String boundary = "boundary";
    final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "body 1\n" + "--" + boundary + "--").getBytes();
    final CountDownLatch latch = new CountDownLatch(2);
    final CompletableFuture<Void> testDone = new CompletableFuture<>();
    Consumer<BodyPart> consumer = (part) -> {
        latch.countDown();
        assertThat(part.headers().values("Content-Id"), hasItems("part1"));
        DataChunkSubscriber subscriber = new DataChunkSubscriber();
        part.content().subscribe(subscriber);
        subscriber.content().thenAccept(body -> {
            assertThat(body, is(equalTo("body 1")));
            latch.countDown();
            if (latch.getCount() == 0) {
                testDone.complete(null);
            }
        }).exceptionally((ex) -> {
            testDone.completeExceptionally(ex);
            return null;
        });
    };
    BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.INFINITE, consumer);
    partsPublisher(boundary, chunk1).subscribe(testSubscriber);
    testDone.orTimeout(5, TimeUnit.SECONDS).join();
    try {
        boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
        assertThat(b, is(equalTo(true)));
    } catch (CompletionException error) {
        assertThat(error, is(nullValue()));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CoreMatchers.hasItems(org.hamcrest.CoreMatchers.hasItems) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Publisher(java.util.concurrent.Flow.Publisher) Subscriber(java.util.concurrent.Flow.Subscriber) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Multi(io.helidon.common.reactive.Multi) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test)

Example 9 with Subscriber

use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.

the class MultiPartDecoderTest method testTwoPartsInOneChunk.

@Test
public void testTwoPartsInOneChunk() {
    String boundary = "boundary";
    final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "body 1\n" + "--" + boundary + "\n" + "Content-Id: part2\n" + "\n" + "body 2\n" + "--" + boundary + "--").getBytes();
    final CountDownLatch latch = new CountDownLatch(4);
    Consumer<BodyPart> consumer = (part) -> {
        latch.countDown();
        if (latch.getCount() == 3) {
            assertThat(part.headers().values("Content-Id"), hasItems("part1"));
            DataChunkSubscriber subscriber = new DataChunkSubscriber();
            part.content().subscribe(subscriber);
            subscriber.content().thenAccept(body -> {
                latch.countDown();
                assertThat(body, is(equalTo("body 1")));
            });
        } else {
            assertThat(part.headers().values("Content-Id"), hasItems("part2"));
            DataChunkSubscriber subscriber = new DataChunkSubscriber();
            part.content().subscribe(subscriber);
            subscriber.content().thenAccept(body -> {
                latch.countDown();
                assertThat(body, is(equalTo("body 2")));
            });
        }
    };
    BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.INFINITE, consumer);
    partsPublisher(boundary, chunk1).subscribe(testSubscriber);
    try {
        boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
        assertThat(b, is(equalTo(true)));
    } catch (CompletionException error) {
        assertThat(error, is(nullValue()));
    }
    waitOnLatch(latch);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CoreMatchers.hasItems(org.hamcrest.CoreMatchers.hasItems) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Publisher(java.util.concurrent.Flow.Publisher) Subscriber(java.util.concurrent.Flow.Subscriber) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Multi(io.helidon.common.reactive.Multi) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CompletionException(java.util.concurrent.CompletionException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 10 with Subscriber

use of java.util.concurrent.Flow.Subscriber in project helidon by oracle.

the class MultiPartDecoderTest method testMulitiplePartsWithOneByOneSubscriber.

@Test
public void testMulitiplePartsWithOneByOneSubscriber() {
    String boundary = "boundary";
    final byte[] chunk1 = ("--" + boundary + "\n" + "Content-Id: part1\n" + "\n" + "body 1\n" + "--" + boundary + "\n" + "Content-Id: part2\n" + "\n" + "body 2\n" + "--" + boundary + "--").getBytes();
    final CountDownLatch latch = new CountDownLatch(4);
    Consumer<BodyPart> consumer = (part) -> {
        latch.countDown();
        if (latch.getCount() == 3) {
            assertThat(part.headers().values("Content-Id"), hasItems("part1"));
            DataChunkSubscriber subscriber = new DataChunkSubscriber();
            part.content().subscribe(subscriber);
            subscriber.content().thenAccept(body -> {
                latch.countDown();
                assertThat(body, is(equalTo("body 1")));
            });
        } else {
            assertThat(part.headers().values("Content-Id"), hasItems("part2"));
            DataChunkSubscriber subscriber = new DataChunkSubscriber();
            part.content().subscribe(subscriber);
            subscriber.content().thenAccept(body -> {
                latch.countDown();
                assertThat(body, is(equalTo("body 2")));
            });
        }
    };
    BodyPartSubscriber testSubscriber = new BodyPartSubscriber(SUBSCRIBER_TYPE.ONE_BY_ONE, consumer);
    partsPublisher(boundary, chunk1).subscribe(testSubscriber);
    try {
        boolean b = testSubscriber.complete.orTimeout(200, TimeUnit.MILLISECONDS).join();
        assertThat(b, is(equalTo(true)));
    } catch (CompletionException error) {
        assertThat(error, is(nullValue()));
    }
    waitOnLatch(latch);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CoreMatchers.hasItems(org.hamcrest.CoreMatchers.hasItems) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Publisher(java.util.concurrent.Flow.Publisher) Subscriber(java.util.concurrent.Flow.Subscriber) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Multi(io.helidon.common.reactive.Multi) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) CompletionException(java.util.concurrent.CompletionException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

Subscriber (java.util.concurrent.Flow.Subscriber)11 Test (org.junit.jupiter.api.Test)11 DataChunk (io.helidon.common.http.DataChunk)10 Subscription (java.util.concurrent.Flow.Subscription)10 CompletableFuture (java.util.concurrent.CompletableFuture)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 TimeoutException (java.util.concurrent.TimeoutException)8 CoreMatchers.is (org.hamcrest.CoreMatchers.is)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Multi (io.helidon.common.reactive.Multi)7 CompletionException (java.util.concurrent.CompletionException)7 CompletionStage (java.util.concurrent.CompletionStage)7 Publisher (java.util.concurrent.Flow.Publisher)7 Consumer (java.util.function.Consumer)7 CoreMatchers.hasItems (org.hamcrest.CoreMatchers.hasItems)7 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)7 CoreMatchers.nullValue (org.hamcrest.CoreMatchers.nullValue)7 Matchers.equalTo (org.hamcrest.Matchers.equalTo)7