Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Aug 10, 2024
1 parent ef376e7 commit 94569bd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 130 deletions.
121 changes: 38 additions & 83 deletions cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,18 @@ void test_mRun() throws Exception {
cffuFactory.mRunAnySuccessAsync(runnable, runnable),
cffuFactory.mRunAnyAsync(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 = () -> {
snoreZzz();
return n;
};
final Supplier<Integer> supplier = supplyLater(n);

final long tick = System.currentTimeMillis();

@SuppressWarnings("unchecked")
Cffu<List<Integer>>[] cfs = new Cffu[]{
cffuFactory.mSupplyFastFailAsync(supplier, supplier),
Expand All @@ -90,23 +86,22 @@ void test_mSupply() throws Exception {
cffuFactory.mSupplyAsync(supplier, supplier),
cffuFactory.mSupplyAsync(executorService, supplier, supplier),
};

assertTrue(System.currentTimeMillis() - tick < 50);

for (Cffu<List<Integer>> cf : cfs) {
assertEquals(Arrays.asList(n, n), cf.get());
}

final long tick1 = System.currentTimeMillis();

@SuppressWarnings("unchecked")
Cffu<Integer>[] cfs1 = new Cffu[]{
cffuFactory.mSupplyAnySuccessAsync(supplier, supplier),
cffuFactory.mSupplyAnySuccessAsync(executorService, supplier, supplier),
cffuFactory.mSupplyAnyAsync(supplier, supplier),
cffuFactory.mSupplyAnyAsync(executorService, supplier, supplier),
};

assertTrue(System.currentTimeMillis() - tick1 < 50);

for (Cffu<Integer> cf : cfs1) {
assertEquals(n, cf.get());
}
Expand All @@ -119,54 +114,38 @@ void test_mSupply() throws Exception {

@Test
void test_tupleMSupplyAsync() throws Exception {
final Supplier<Integer> supplier_n = () -> {
snoreZzz();
return n;
};
final Supplier<String> supplier_s = () -> {
snoreZzz();
return s;
};

final Supplier<Double> supplier_d = () -> {
snoreZzz();
return d;
};
final Supplier<Integer> supplier_an = () -> {
snoreZzz();
return anotherN;
};
final Supplier<Integer> supplier_nn = () -> {
snoreZzz();
return n + n;
};
final Supplier<Integer> supplier_n = supplyLater(n);
final Supplier<String> supplier_s = supplyLater(s);
final Supplier<Double> supplier_d = supplyLater(d);
final Supplier<Integer> supplier_an = supplyLater(anotherN);
final Supplier<Integer> supplier_nnn = supplyLater(nnn);

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.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.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.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());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.tupleMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nnn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.tupleMSupplyFastFailAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nnn).get());

assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyAllSuccessAsync(supplier_n, supplier_s).get());
assertEquals(Tuple2.of(n, s), cffuFactory.tupleMSupplyAllSuccessAsync(executorService, supplier_n, supplier_s).get());
assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyAllSuccessAsync(supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple3.of(n, s, d), cffuFactory.tupleMSupplyAllSuccessAsync(executorService, supplier_n, supplier_s, supplier_d).get());
assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyAllSuccessAsync(supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.tupleMSupplyAllSuccessAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyAllSuccessAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyAllSuccessAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.tupleMSupplyAllSuccessAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nnn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.tupleMSupplyAllSuccessAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nnn).get());

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(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(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(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, nnn), cffuFactory.tupleMSupplyAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nnn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.tupleMSupplyAsync(executorService, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nnn).get());
}

@Test
Expand All @@ -190,7 +169,7 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception {
};
final Supplier<Integer> supplier_nn = () -> {
nap();
return n + n;
return nnn;
};
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());
Expand All @@ -201,8 +180,8 @@ void test_tupleMSupplyMostSuccessAsync() throws Exception {
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());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.tupleMSupplyMostSuccessAsync(100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.tupleMSupplyMostSuccessAsync(executorService, 100, TimeUnit.MILLISECONDS, supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get());
}

// endregion
Expand Down Expand Up @@ -423,33 +402,21 @@ void test_anySuccessOf__trivial_case() throws Exception {
assertEquals(n, cffuFactory.anySuccessOf(
cffuFactory.newIncompleteCffu(),
cffuFactory.newIncompleteCffu(),
cffuFactory.supplyAsync(() -> {
snoreZzz();
return anotherN;
}),
cffuFactory.supplyAsync(supplyLater(anotherN)),
cffuFactory.completedFuture(n)
).get());
// success then failed
assertEquals(n, cffuFactory.anySuccessOf(
cffuFactory.newIncompleteCffu(),
cffuFactory.newIncompleteCffu(),
cffuFactory.supplyAsync(() -> {
snoreZzz();
throw rte;
}),
cffuFactory.supplyAsync(supplyLater(rte)),
cffuFactory.completedFuture(n)
).get());

// all success
assertEquals(n, cffuFactory.anySuccessOf(
cffuFactory.supplyAsync(() -> {
snoreZzz();
return anotherN;
}),
cffuFactory.supplyAsync(() -> {
snoreZzz();
return anotherN;
}),
cffuFactory.supplyAsync(supplyLater(anotherN)),
cffuFactory.supplyAsync(supplyLater(anotherN)),
cffuFactory.completedFuture(n)
).get());

Expand All @@ -459,33 +426,21 @@ void test_anySuccessOf__trivial_case() throws Exception {
assertEquals(n, cffuFactory.anySuccessOf(
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
snoreZzz();
return anotherN;
}),
CompletableFuture.supplyAsync(supplyLater(anotherN)),
completedFuture(n)
).get());
// success then failed
assertEquals(n, cffuFactory.anySuccessOf(
incompleteCf(),
incompleteCf(),
CompletableFuture.supplyAsync(() -> {
snoreZzz();
throw rte;
}),
CompletableFuture.supplyAsync(supplyLater(rte)),
completedFuture(n)
).get());

// all success
assertEquals(n, cffuFactory.anySuccessOf(
CompletableFuture.supplyAsync(() -> {
snoreZzz();
return anotherN;
}),
CompletableFuture.supplyAsync(() -> {
snoreZzz();
return anotherN;
}),
CompletableFuture.supplyAsync(supplyLater(anotherN)),
CompletableFuture.supplyAsync(supplyLater(anotherN)),
completedFuture(n)
).get());

Expand Down Expand Up @@ -519,12 +474,12 @@ void test_allTupleFastFailOf() throws Exception {
completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleFastFailOf(
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.allTupleFastFailOf(
completedFuture(n),
completedFuture(s),
completedFuture(d),
completedFuture(anotherN),
completedFuture(n + n)
completedFuture(nnn)
).get());

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -547,12 +502,12 @@ void test_allTupleFastFailOf() throws Exception {
cffuFactory.completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleFastFailOf(
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.allTupleFastFailOf(
cffuFactory.completedFuture(n),
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d),
cffuFactory.completedFuture(anotherN),
cffuFactory.completedFuture(n + n)
cffuFactory.completedFuture(nnn)
).get());
}

Expand Down Expand Up @@ -597,12 +552,12 @@ void test_allSuccessTupleOf() throws Exception {
completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allSuccessTupleOf(
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.allSuccessTupleOf(
completedFuture(n),
completedFuture(s),
completedFuture(d),
completedFuture(anotherN),
completedFuture(n + n)
completedFuture(nnn)
).get());

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -625,12 +580,12 @@ void test_allSuccessTupleOf() throws Exception {
cffuFactory.completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allSuccessTupleOf(
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.allSuccessTupleOf(
cffuFactory.completedFuture(n),
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d),
cffuFactory.completedFuture(anotherN),
cffuFactory.completedFuture(n + n)
cffuFactory.completedFuture(nnn)
).get());
}

Expand Down Expand Up @@ -688,12 +643,12 @@ void test_allTupleOf() throws Exception {
completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleOf(
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.allTupleOf(
completedFuture(n),
completedFuture(s),
completedFuture(d),
completedFuture(anotherN),
completedFuture(n + n)
completedFuture(nnn)
).get());

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -716,12 +671,12 @@ void test_allTupleOf() throws Exception {
cffuFactory.completedFuture(anotherN)
).get());

assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleOf(
assertEquals(Tuple5.of(n, s, d, anotherN, nnn), cffuFactory.allTupleOf(
cffuFactory.completedFuture(n),
cffuFactory.completedFuture(s),
cffuFactory.completedFuture(d),
cffuFactory.completedFuture(anotherN),
cffuFactory.completedFuture(n + n)
cffuFactory.completedFuture(nnn)
).get());
}

Expand Down
Loading

0 comments on commit 94569bd

Please sign in to comment.