Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODORDERS-853: VertxFutureRepeater.repeat failure handling #1008

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.folio.completablefuture;

import java.util.function.Function;
import java.util.function.Supplier;

import io.vertx.core.Future;
Expand All @@ -15,12 +14,12 @@ private VertxFutureRepeater(){
* Return the result of the succeeding task, or the last run's failure if all runs fail.
*/
public static <T> Future<T> repeat(int max, Supplier<Future<T>> task) {
Future<T> future = task.get();
for (int i = 1; i < max; i++) {
future = future.map(Future::succeededFuture)
.onFailure(t -> task.get())
.compose(Function.identity());
}
return future;
return task.get()
.recover(e -> {
if (max <= 1) {
return Future.failedFuture(e);
}
return repeat(max - 1, task);
});
}
}
4 changes: 3 additions & 1 deletion src/test/java/org/folio/DataImportTestSuite.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.folio;

import org.folio.completablefuture.VertxFutureRepeaterTest;
import org.folio.di.CacheTest;
import org.folio.service.caches.InventoryCacheTest;
import org.folio.service.caches.JobExecutionTotalRecordsCacheTest;
Expand All @@ -16,7 +17,8 @@
JobExecutionTotalRecordsCacheTest.class,
OrderIdStorageServiceImplTest.class,
CreateOrderEventHandlerTest.class,
OrderPostProcessingEventHandlerTest.class
OrderPostProcessingEventHandlerTest.class,
VertxFutureRepeaterTest.class
})
public class DataImportTestSuite {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
import static io.vertx.core.Future.failedFuture;
import static io.vertx.core.Future.succeededFuture;
import static org.folio.completablefuture.VertxFutureRepeater.repeat;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import io.vertx.core.Future;

class CompletableFutureRepeaterTest {
public class VertxFutureRepeaterTest {

private final static Future<String> a = succeededFuture("a");
private final static Future<String> b = succeededFuture("b");
Expand All @@ -34,14 +30,10 @@ private Supplier<Future<String>> task(Future<String> ...completableFutures) {
return iterator::next;
}

private void assertThrowsCause(Class<?> clazz, Executable executable) {
assertThat(assertThrows(Throwable.class, executable).getCause(), is(instanceOf(clazz)));
}

@Test
void fail() {
assertThrowsCause(Fail1.class, () -> repeat(1, task(fail1, a, b)).result());
assertThrowsCause(Fail2.class, () -> repeat(2, task(fail1, fail2, a, b)).result());
assertThat(repeat(1, task(fail1, a, b)).cause(), is(instanceOf(Fail1.class)));
assertThat(repeat(2, task(fail1, fail2, a, b)).cause(), is(instanceOf(Fail2.class)));
}

@Test
Expand Down
Loading