use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.
the class ExponentialBackoff method randomExponentialBackoffFunctionExpireAt.
/**
* Computes a method that would delay <em>ticks</em> using an exponential backoff.
* Will keep retrying until an expiration time.
* The last attempt will start before the expiration time.
*
* @param expireAt absolute time in millis that specifies when to give up.
* @param firstBackoff the delay of the first backoff
* @param maxBackoff the max backoff
* @param jitterFactor the jitter factor in [0, 1]
* @param executor the executor used for the delay
* @return the function
*/
public static Function<Multi<Throwable>, Publisher<Long>> randomExponentialBackoffFunctionExpireAt(long expireAt, Duration firstBackoff, Duration maxBackoff, double jitterFactor, ScheduledExecutorService executor) {
validate(firstBackoff, maxBackoff, jitterFactor, executor);
AtomicInteger index = new AtomicInteger();
return t -> t.onItem().transformToUni(failure -> {
int iteration = index.incrementAndGet();
Duration delay = getNextDelay(firstBackoff, maxBackoff, jitterFactor, iteration);
long checkTime = System.currentTimeMillis() + delay.toMillis();
if (checkTime > expireAt) {
return Uni.createFrom().failure(new IllegalStateException("Retries exhausted : " + iteration + " attempts against " + checkTime + "/" + expireAt + " expiration", failure));
}
return Uni.createFrom().item((long) iteration).onItem().delayIt().onExecutor(executor).by(delay);
}).concatenate();
}
use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.
the class MultiReplayTest method raceBetweenPushAndCancel.
@Test
void raceBetweenPushAndCancel() throws InterruptedException, TimeoutException {
ExecutorService pool = Executors.newCachedThreadPool();
try {
final int N = 32;
CountDownLatch startLatch = new CountDownLatch(N);
CountDownLatch endLatch = new CountDownLatch(N);
Multi<Long> upstream = Multi.createFrom().<Long>emitter(emitter -> {
try {
startLatch.await();
} catch (InterruptedException e) {
emitter.fail(e);
}
long i = 0;
while (endLatch.getCount() != 0) {
emitter.emit(i++);
}
emitter.complete();
}).runSubscriptionOn(pool);
Multi<Long> replay = Multi.createBy().replaying().ofMulti(upstream).runSubscriptionOn(pool);
CopyOnWriteArrayList<List<Long>> items = new CopyOnWriteArrayList<>();
for (int i = 0; i < N; i++) {
AssertSubscriber<Long> sub = replay.subscribe().withSubscriber(AssertSubscriber.create());
pool.submit(() -> {
startLatch.countDown();
randomSleep();
sub.request(Long.MAX_VALUE);
randomSleep();
sub.cancel();
items.add(sub.getItems());
endLatch.countDown();
});
}
if (!endLatch.await(10, TimeUnit.SECONDS)) {
throw new TimeoutException("The test did not finish within 10 seconds");
}
assertThat(items).hasSize(N);
items.forEach(list -> {
if (list.isEmpty()) {
// Might happen due to subscriber timing
return;
}
assertThat(list).isNotEmpty();
AtomicLong prev = new AtomicLong(list.get(0));
list.stream().skip(1).forEach(n -> {
assertThat(n).isEqualTo(prev.get() + 1);
prev.set(n);
});
});
} finally {
pool.shutdownNow();
}
}
use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.
the class MultiFromResourceFromUniTest method testThatStreamSupplierEmittingAFailureCallsOnFailure.
@Test
public void testThatStreamSupplierEmittingAFailureCallsOnFailure() {
FakeTransactionalResource resource = new FakeTransactionalResource();
Multi<String> multi = Multi.createFrom().<FakeTransactionalResource, String>resource(() -> resource, r -> Multi.createFrom().failure(new IOException("boom"))).withFinalizer(FakeTransactionalResource::commit, FakeTransactionalResource::rollback, FakeTransactionalResource::cancel);
multi.subscribe().withSubscriber(AssertSubscriber.create(20)).assertFailedWith(IOException.class, "boom");
assertThat(resource.subscribed).isFalse();
assertThat(resource.onCompleteSubscribed).isFalse();
assertThat(resource.onCancelSubscribed).isFalse();
assertThat(resource.onFailureSubscribed).isTrue();
assertThat(resource.failure.get()).isInstanceOf(IOException.class);
}
use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.
the class MultiCacheTest method testCachingWithFailure.
@Test
public void testCachingWithFailure() {
AtomicInteger count = new AtomicInteger();
Multi<Integer> multi = Multi.createFrom().<Integer>emitter(emitter -> emitter.emit(count.incrementAndGet()).emit(count.incrementAndGet()).fail(new IOException("boom-" + count.incrementAndGet()))).cache();
multi.subscribe().withSubscriber(AssertSubscriber.create(2)).assertItems(1, 2).assertFailedWith(IOException.class, "boom-3");
multi.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE)).assertItems(1, 2).assertFailedWith(IOException.class, "boom-3");
}
use of io.smallrye.mutiny.Multi in project smallrye-mutiny by smallrye.
the class MultiConvertFromTest method testCreatingFromAMaybeNeverEmitting.
@Test
public void testCreatingFromAMaybeNeverEmitting() {
AtomicBoolean cancelled = new AtomicBoolean();
Multi<Integer> multi = Multi.createFrom().converter(MultiRx3Converters.fromMaybe(), Maybe.<Integer>never().doOnDispose(() -> cancelled.set(true)));
assertThat(multi).isNotNull();
multi.subscribe().with(i -> {
}).cancel();
assertThat(cancelled).isTrue();
}
Aggregations