Skip to content

Commit

Permalink
CffuFactory新增Multi-Actions(M*)实现及相关单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
huhaosumail committed Jun 25, 2024
1 parent 74f1c94 commit 49cac79
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
89 changes: 88 additions & 1 deletion cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,94 @@ public Cffu<Void> runAsync(Runnable action, Executor executor) {
// region## Multi-Actions(M*) Methods(create by actions)
////////////////////////////////////////////////////////////

// TODO: TO BE implemented!!

/**
* Returns a new CompletableFuture that is asynchronously completed
* by tasks running in the CompletableFuture's default asynchronous execution facility
* with the values obtained by calling the given Suppliers
* in the <strong>same order</strong> of the given Suppliers arguments.
* <p>
* This method is the same as {@link #mSupplyAsync(Supplier[])} except for the fast-fail behavior.
*
* @param suppliers the suppliers returning the value to be used to complete the returned CompletableFuture
* @param <T> the suppliers' return type
* @return the new CompletableFuture
* @see #allResultsOfFastFail(CompletionStage[])
* @see CompletableFuture#supplyAsync(Supplier)
*/
public <T> Cffu<List<T>> mSupplyFastFailAsync(Supplier<? extends T>... suppliers) {
return create(CompletableFutureUtils.mSupplyFastFailAsync(suppliers));
}

/**
* Returns a new CompletableFuture that is asynchronously completed
* by tasks running in the CompletableFuture's default asynchronous execution facility
* with the most values obtained by calling the given Suppliers
* in the given time({@code timeout}, aka as many results as possible in the given time)
* in the <strong>same order</strong> of the given Suppliers arguments.
* <p>
* If the given supplier is successful in the given time, the return result is the completed value;
* Otherwise the given valueIfNotSuccess.
*
* @param valueIfNotSuccess the value to return if not completed successfully
* @param timeout how long to wait in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
* @param suppliers the suppliers returning the value to be used to complete the returned CompletableFuture
* @param <T> the suppliers' return type
* @return the new CompletableFuture
* @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])
* @see CompletableFuture#supplyAsync(Supplier)
*/
public <T> Cffu<List<T>> mSupplyMostSuccessAsync(
@Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, Supplier<? extends T>... suppliers) {
return create(CompletableFutureUtils.mSupplyMostSuccessAsync(valueIfNotSuccess, timeout, unit, suppliers));
}

/**
* Returns a new CompletableFuture that is asynchronously completed
* by tasks running in the CompletableFuture's default asynchronous execution facility
* with the values obtained by calling the given Suppliers
* in the <strong>same order</strong> of the given Suppliers arguments.
*
* @param suppliers the suppliers returning the value to be used to complete the returned CompletableFuture
* @param <T> the suppliers' return type
* @return the new CompletableFuture
* @see #allResultsOf(CompletionStage[])
* @see CompletableFuture#supplyAsync(Supplier)
*/
public <T> Cffu<List<T>> mSupplyAsync(Supplier<? extends T>... suppliers) {
return create(CompletableFutureUtils.mSupplyAsync(suppliers));
}

/**
* Returns a new CompletableFuture that is asynchronously completed
* by tasks running in the CompletableFuture's default asynchronous execution facility
* after runs the given actions.
* <p>
* This method is the same as {@link #mRunAsync(Runnable...)} except for the fast-fail behavior.
*
* @param actions the actions to run before completing the returned CompletableFuture
* @return the new CompletableFuture
* @see #allOfFastFail(CompletionStage[])
* @see CompletableFuture#runAsync(Runnable)
*/
public Cffu<Void> mRunFastFailAsync(Runnable... actions) {
return create(CompletableFutureUtils.mRunFastFailAsync(actions));
}

/**
* Returns a new CompletableFuture that is asynchronously completed
* by tasks running in the CompletableFuture's default asynchronous execution facility
* after runs the given actions.
*
* @param actions the actions to run before completing the returned CompletableFuture
* @return the new CompletableFuture
* @see #allOf(CompletionStage[])
* @see CompletableFuture#runAsync(Runnable)
*/
public Cffu<Void> mRunAsync(Runnable... actions) {
return create(CompletableFutureUtils.mRunAsync(actions));
}

// endregion
////////////////////////////////////////////////////////////
Expand Down
50 changes: 50 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

0 comments on commit 49cac79

Please sign in to comment.