Search in sources :

Example 1 with RetrySchema

use of io.helidon.common.reactive.RetrySchema in project helidon by oracle.

the class ReadableByteChannelPublisherTest method negativeDelay.

@Test
void negativeDelay() throws Exception {
    PeriodicalChannel pc = createChannelWithNoAvailableData(10, 1);
    RetrySchema schema = (i, delay) -> i >= 3 ? -10 : 0;
    ReadableByteChannelPublisher publisher = new ReadableByteChannelPublisher(pc, schema);
    // assert
    try {
        ContentReaders.readBytes(publisher).get(5, TimeUnit.SECONDS);
        fail("Did not throw expected ExecutionException!");
    } catch (ExecutionException e) {
        assertThat(e.getCause(), instanceOf(TimeoutException.class));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Publisher(java.util.concurrent.Flow.Publisher) LazyValue(io.helidon.common.LazyValue) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Single(io.helidon.common.reactive.Single) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) IntFunction(java.util.function.IntFunction) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) Set(java.util.Set) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) RetrySchema(io.helidon.common.reactive.RetrySchema) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Subscriber(java.util.concurrent.Flow.Subscriber) RetrySchema(io.helidon.common.reactive.RetrySchema) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 2 with RetrySchema

use of io.helidon.common.reactive.RetrySchema in project helidon by oracle.

the class ReadableByteChannelPublisherTest method onClosedInProgress.

@Test
@Disabled("This test uses a sleep, so could cause issues on slow environments")
void onClosedInProgress() throws Exception {
    PeriodicalChannel pc = createChannelWithNoAvailableData(5, 2);
    RetrySchema schema = RetrySchema.constant(TimeUnit.SECONDS.toMillis(2));
    ReadableByteChannelPublisher publisher = new ReadableByteChannelPublisher(pc, schema);
    // start reading (this will cause 2 second delay)
    Single<byte[]> data = ContentReaders.readBytes(publisher);
    // run the stream
    data.thenRun(() -> {
    });
    Thread.sleep(1000);
    // immediately close the channel, so we fail reading
    pc.close();
    CompletionException c = assertThrows(CompletionException.class, () -> data.await(5, TimeUnit.SECONDS));
    assertThat(c.getCause(), instanceOf(ClosedChannelException.class));
    LazyValue<ScheduledExecutorService> executor = publisher.executor();
    assertThat("Executor should have been used", executor.isLoaded(), is(true));
    assertThat("Executor should have been shut down", executor.get().isShutdown(), is(true));
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) RetrySchema(io.helidon.common.reactive.RetrySchema) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

RetrySchema (io.helidon.common.reactive.RetrySchema)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 CompletionException (java.util.concurrent.CompletionException)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Disabled (org.junit.jupiter.api.Disabled)2 Test (org.junit.jupiter.api.Test)2 LazyValue (io.helidon.common.LazyValue)1 DataChunk (io.helidon.common.http.DataChunk)1 Single (io.helidon.common.reactive.Single)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Publisher (java.util.concurrent.Flow.Publisher)1 Subscriber (java.util.concurrent.Flow.Subscriber)1 Subscription (java.util.concurrent.Flow.Subscription)1