Skip to content

Commit

Permalink
Merge branch 'hh_complemented_mostOf'
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 26, 2024
2 parents a7348a7 + cf09707 commit cb16f5b
Show file tree
Hide file tree
Showing 6 changed files with 1,166 additions and 36 deletions.
369 changes: 368 additions & 1 deletion cffu-core/src/main/java/io/foldright/cffu/Cffu.java

Large diffs are not rendered by default.

280 changes: 273 additions & 7 deletions cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java

Large diffs are not rendered by default.

338 changes: 315 additions & 23 deletions cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Supplier;

import static io.foldright.cffu.CompletableFutureUtils.failedFuture;
import static io.foldright.cffu.CompletableFutureUtils.mRunAsync;
import static io.foldright.cffu.CompletableFutureUtils.mRunFastFailAsync;
import static io.foldright.cffu.CompletableFutureUtils.mSupplyAsync;
import static io.foldright.cffu.CompletableFutureUtils.mSupplyFastFailAsync;
import static io.foldright.cffu.CompletableFutureUtils.mSupplyMostSuccessAsync;
import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray;
import static io.foldright.test_utils.TestUtils.*;
import static java.util.concurrent.CompletableFuture.completedFuture;
Expand All @@ -30,6 +36,50 @@
*/
@SuppressWarnings("RedundantThrows")
class CffuFactoryTest {

////////////////////////////////////////////////////////////////////////////////
//# multi-actions(M*) methods
////////////////////////////////////////////////////////////////////////////////

@Test
void test_mRun() throws Exception {
final Runnable runnable = () -> sleep(100);

final long tick = System.currentTimeMillis();
@SuppressWarnings("unchecked")
Cffu<Void>[] cfs = new Cffu[]{
cffuFactory.mRunAsync(runnable, runnable),
cffuFactory.mRunFastFailAsync(runnable, runnable)
};

assertTrue(System.currentTimeMillis() - tick < 50);
for (Cffu<Void> cf : cfs) {
assertNull(cf.get());
}
}

@Test
void test_mSupply() throws Exception {
final Supplier<Integer> supplier = () -> {
sleep(100);
return n;
};

final long tick = System.currentTimeMillis();

@SuppressWarnings("unchecked")
Cffu<List<Integer>>[] cfs = new Cffu[]{
cffuFactory.mSupplyAsync(supplier, supplier),
cffuFactory.mSupplyFastFailAsync(supplier, supplier),
cffuFactory.mSupplyMostSuccessAsync(anotherN, 500, TimeUnit.MILLISECONDS, supplier, supplier)
};

assertTrue(System.currentTimeMillis() - tick < 50);
for (Cffu<List<Integer>> cf : cfs) {
assertEquals(Arrays.asList(n, n), cf.get());
}
}

///////////////////////////////////////////////////////////////////////////////
//# Factory Methods, equivalent to same name static methods of CompletableFuture
//
Expand Down Expand Up @@ -649,16 +699,24 @@ void test_tupleMSupplyAsync() throws Exception {
return n + n;
};
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyAsync(supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyAsync(executorService, supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyFastFailAsync(supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyFastFailAsync(executorService, supplier_n, supplier_s).get());

assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyAsync(supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyAsync(executorService, supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyFastFailAsync(executorService, supplier_n, supplier_s, supplier_d).get());

assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyAsync(supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyFastFailAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyFastFailAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
}

@Test
Expand All @@ -685,12 +743,16 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception {
return n + n;
};
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get());

assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get());

assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
110 changes: 105 additions & 5 deletions cffu-core/src/test/java/io/foldright/cffu/CffuTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand All @@ -33,42 +35,140 @@ class CffuTest {

private static CffuFactory forbidObtrudeMethodsCffuFactory;

////////////////////////////////////////////////////////////////////////////////
//# multi-actions(M*) methods
////////////////////////////////////////////////////////////////////////////////

@Test
void test_thenTupleMApplyAsync() throws Exception {
void test_thenMApply() throws Exception {
final Cffu<Integer> completed = cffuFactory.completedFuture(n);

final Function<Integer, Integer> function_n = (x) -> {
sleep(100);
return n;
};

final Function<Integer, String> function_s = (x) -> {

final long tick = System.currentTimeMillis();
@SuppressWarnings("unchecked")
Cffu<Void>[] cfs = new Cffu[]{
completed.thenMApplyFastFailAsync(completed, function_n, function_n),
completed.thenMApplyFastFailAsync(completed, executorService, function_n, function_n),
completed.thenMApplyMostSuccessAsync(completed, 100, 500, TimeUnit.MILLISECONDS, function_n, function_n),
completed.thenMApplyMostSuccessAsync(completed, 100, executorService, 500, TimeUnit.MILLISECONDS, function_n, function_n),
completed.thenMApplyAsync(completed, function_n, function_n),
completed.thenMApplyAsync(completed, executorService, function_n, function_n)
};

assertTrue(System.currentTimeMillis() - tick < 50);
for (Cffu<Void> cf : cfs) {
assertEquals(Arrays.asList(n, n), cf.get());
}
}

@Test
void test_thenMAccept() throws Exception {
final Cffu<Integer> completed = cffuFactory.completedFuture(n);
final Consumer<Integer> consumer = (x) -> {
assertEquals(n, x);
sleep(100);
};

final long tick = System.currentTimeMillis();

@SuppressWarnings("unchecked")
Cffu<List<Integer>>[] cfs = new Cffu[]{
completed.thenMAcceptAsync(completed, consumer, consumer),
completed.thenMAcceptAsync(completed, executorService, consumer, consumer),
completed.thenMAcceptFastFailAsync(completed, consumer, consumer),
completed.thenMAcceptFastFailAsync(completed, executorService, consumer, consumer)
};

assertTrue(System.currentTimeMillis() - tick < 50);
for (Cffu<List<Integer>> cf : cfs) {
assertNull(cf.get());
}
}

@Test
void test_thenMRun() throws Exception {
final Cffu<Integer> completed = cffuFactory.completedFuture(null);
final Runnable runnable = () -> {
sleep(100);
};

final long tick = System.currentTimeMillis();

@SuppressWarnings("unchecked")
Cffu<List<Integer>>[] cfs = new Cffu[]{
completed.thenMRunAsync(completed, runnable, runnable),
completed.thenMRunAsync(completed, executorService, runnable, runnable),
completed.thenMRunFastFailAsync(completed, runnable, runnable),
completed.thenMRunFastFailAsync(completed, executorService, runnable, runnable)
};

assertTrue(System.currentTimeMillis() - tick < 50);
for (Cffu<List<Integer>> cf : cfs) {
assertNull(cf.get());
}
}

////////////////////////////////////////////////////////////////////////////////
// Then-Tuple-Multi-Actions(thenTupleM*) Methods
////////////////////////////////////////////////////////////////////////////////

@Test
void test_thenTupleMApplyAsync() throws Exception {
final Cffu<Integer> completed = cffuFactory.completedFuture(n);
final Function<Integer, Integer> function_n = (x) -> {
sleep(10);
return n;
};

final Function<Integer, String> function_s = (x) -> {
sleep(10);
return s;
};

final Function<Integer, Double> function_d = (x) -> {
sleep(100);
sleep(10);
return d;
};
final Function<Integer, Integer> function_an = (x) -> {
sleep(100);
sleep(10);
return anotherN;
};
final Function<Integer, Integer> function_nn = (x) -> {
sleep(100);
sleep(10);
return n + n;
};
assertEquals(Tuple2.of(n, s), completed.thenTupleMApplyAsync(function_n, function_s).get());
assertEquals(Tuple2.of(n, s), completed.thenTupleMApplyAsync(executorService, function_n, function_s).get());
assertEquals(Tuple2.of(n, s), completed.thenTupleMApplyFastFailAsync(function_n, function_s).get());
assertEquals(Tuple2.of(n, s), completed.thenTupleMApplyFastFailAsync(executorService, function_n, function_s).get());
assertEquals(Tuple2.of(n, s), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s).get());
assertEquals(Tuple2.of(n, s), completed.thenTupleMApplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, function_n, function_s).get());

assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyAsync(function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyAsync(executorService, function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyFastFailAsync(function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyFastFailAsync(executorService, function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get());

assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyAsync(function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyAsync(executorService, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyFastFailAsync(function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyFastFailAsync(executorService, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyAsync(function_n, function_s, function_d, function_an, function_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyAsync(executorService, function_n, function_s, function_d, function_an, function_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyFastFailAsync(function_n, function_s, function_d, function_an, function_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyFastFailAsync(executorService, function_n, function_s, function_d, function_an, function_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get());
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,15 +809,58 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception {
sleep(10);
return n + n;
};
assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s).get());

assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d).get());

assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), tupleMSupplyMostSuccessAsync(defaultExecutor(), 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
}


@Test
void test_tupleMApplyMostSuccessAsync() throws Exception {
final CompletableFuture<Integer> completed = completedFuture(n);
final Function<Integer, Integer> function_n = (x) -> {
sleep(100);
return n;
};

final Function<Integer, String> function_s = (x) -> {
sleep(100);
return s;
};

final Function<Integer, Double> function_d = (x) -> {
sleep(100);
return d;
};
final Function<Integer, Integer> function_an = (x) -> {
sleep(100);
return anotherN;
};
final Function<Integer, Integer> function_nn = (x) -> {
sleep(100);
return n + n;
};
assertEquals(Tuple2.of(n, s), thenTupleMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, function_n, function_s).get());
assertEquals(Tuple2.of(n, s), thenTupleMApplyMostSuccessAsync(completed, defaultExecutor(), 500, TimeUnit.MILLISECONDS, function_n, function_s).get());

assertEquals(Tuple3.of(n, s, d), thenTupleMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get());
assertEquals(Tuple3.of(n, s, d), thenTupleMApplyMostSuccessAsync(completed, defaultExecutor(), 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d).get());

assertEquals(Tuple4.of(n, s, d, anotherN), thenTupleMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), thenTupleMApplyMostSuccessAsync(completed, defaultExecutor(), 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), thenTupleMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), thenTupleMApplyMostSuccessAsync(completed, defaultExecutor(), 500, TimeUnit.MILLISECONDS, function_n, function_s, function_d, function_an, function_nn).get());
}

@Test
void test_thenTupleMApplyAsync() throws Exception {
final CompletableFuture<Integer> completed = completedFuture(n);
Expand Down

0 comments on commit cb16f5b

Please sign in to comment.