|
25 | 25 | import java.util.Arrays; |
26 | 26 | import java.util.Collections; |
27 | 27 | import java.util.List; |
| 28 | +import java.util.Objects; |
28 | 29 | import java.util.concurrent.*; |
29 | 30 | import java.util.concurrent.atomic.AtomicBoolean; |
30 | 31 | import java.util.concurrent.atomic.AtomicInteger; |
|
36 | 37 | */ |
37 | 38 | public class FutureTest extends FutureTestBase { |
38 | 39 |
|
| 40 | + @Test |
| 41 | + public void testAllSucceededWithEightFutures() { |
| 42 | + testAllSucceeded(Future::all); |
| 43 | + } |
| 44 | + |
| 45 | + private void testAllSucceeded(EightFunction<Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, CompositeFuture> all) { |
| 46 | + Promise<String> p1 = Promise.promise(); |
| 47 | + Future<String> f1 = p1.future(); |
| 48 | + Promise<String> p2 = Promise.promise(); |
| 49 | + Future<String> f2 = p2.future(); |
| 50 | + Promise<String> p3 = Promise.promise(); |
| 51 | + Future<String> f3 = p3.future(); |
| 52 | + Promise<String> p4 = Promise.promise(); |
| 53 | + Future<String> f4 = p4.future(); |
| 54 | + Promise<String> p5 = Promise.promise(); |
| 55 | + Future<String> f5 = p5.future(); |
| 56 | + Promise<String> p6 = Promise.promise(); |
| 57 | + Future<String> f6 = p6.future(); |
| 58 | + Promise<String> p7 = Promise.promise(); |
| 59 | + Future<String> f7 = p7.future(); |
| 60 | + Promise<String> p8 = Promise.promise(); |
| 61 | + Future<String> f8 = p8.future(); |
| 62 | + CompositeFuture composite = all.apply(f1, f2, f3, f4, f5, f6, f7, f8); |
| 63 | + Checker<CompositeFuture> checker = new Checker<>(composite); |
| 64 | + p1.complete("f1"); |
| 65 | + p2.complete("f2"); |
| 66 | + p3.complete("f3"); |
| 67 | + p4.complete("f4"); |
| 68 | + p5.complete("f5"); |
| 69 | + p6.complete("f6"); |
| 70 | + p7.complete("f7"); |
| 71 | + p8.complete("f8"); |
| 72 | + checker.assertSucceeded(composite); |
| 73 | + assertEquals("f1", composite.<String>resultAt(0)); |
| 74 | + assertEquals("f2", composite.<String>resultAt(1)); |
| 75 | + assertEquals("f3", composite.<String>resultAt(2)); |
| 76 | + assertEquals("f4", composite.<String>resultAt(3)); |
| 77 | + assertEquals("f5", composite.<String>resultAt(4)); |
| 78 | + assertEquals("f6", composite.<String>resultAt(5)); |
| 79 | + assertEquals("f7", composite.<String>resultAt(6)); |
| 80 | + assertEquals("f8", composite.<String>resultAt(7)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testAnySucceededWithEightFutures() { |
| 85 | + testAnySucceeded(Future::any); |
| 86 | + } |
| 87 | + |
| 88 | + private void testAnySucceeded(EightFunction<Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, CompositeFuture> any) { |
| 89 | + Promise<String> p1 = Promise.promise(); |
| 90 | + Future<String> f1 = p1.future(); |
| 91 | + Promise<String> p2 = Promise.promise(); |
| 92 | + Future<String> f2 = p2.future(); |
| 93 | + Promise<String> p3 = Promise.promise(); |
| 94 | + Future<String> f3 = p3.future(); |
| 95 | + Promise<String> p4 = Promise.promise(); |
| 96 | + Future<String> f4 = p4.future(); |
| 97 | + Promise<String> p5 = Promise.promise(); |
| 98 | + Future<String> f5 = p5.future(); |
| 99 | + Promise<String> p6 = Promise.promise(); |
| 100 | + Future<String> f6 = p6.future(); |
| 101 | + Promise<String> p7 = Promise.promise(); |
| 102 | + Future<String> f7 = p7.future(); |
| 103 | + Promise<String> p8 = Promise.promise(); |
| 104 | + Future<String> f8 = p8.future(); |
| 105 | + CompositeFuture composite = any.apply(f1, f2, f3, f4, f5, f6, f7, f8); |
| 106 | + Checker<CompositeFuture> checker = new Checker<>(composite); |
| 107 | + p1.complete("f1"); |
| 108 | + p2.complete("f2"); |
| 109 | + p3.complete("f3"); |
| 110 | + p4.complete("f4"); |
| 111 | + p5.complete("f5"); |
| 112 | + p6.complete("f6"); |
| 113 | + p7.complete("f7"); |
| 114 | + p8.complete("f8"); |
| 115 | + checker.assertSucceeded(composite); |
| 116 | + assertEquals("f1", composite.<String>resultAt(0)); |
| 117 | + assertEquals("f2", composite.<String>resultAt(1)); |
| 118 | + assertEquals("f3", composite.<String>resultAt(2)); |
| 119 | + assertEquals("f4", composite.<String>resultAt(3)); |
| 120 | + assertEquals("f5", composite.<String>resultAt(4)); |
| 121 | + assertEquals("f6", composite.<String>resultAt(5)); |
| 122 | + assertEquals("f7", composite.<String>resultAt(6)); |
| 123 | + assertEquals("f8", composite.<String>resultAt(7)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testJoinSucceededWithEightFutures() { |
| 128 | + testJoinSucceeded(Future::join); |
| 129 | + } |
| 130 | + |
| 131 | + private void testJoinSucceeded(EightFunction<Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, Future<String>, CompositeFuture> join) { |
| 132 | + Promise<String> p1 = Promise.promise(); |
| 133 | + Future<String> f1 = p1.future(); |
| 134 | + Promise<String> p2 = Promise.promise(); |
| 135 | + Future<String> f2 = p2.future(); |
| 136 | + Promise<String> p3 = Promise.promise(); |
| 137 | + Future<String> f3 = p3.future(); |
| 138 | + Promise<String> p4 = Promise.promise(); |
| 139 | + Future<String> f4 = p4.future(); |
| 140 | + Promise<String> p5 = Promise.promise(); |
| 141 | + Future<String> f5 = p5.future(); |
| 142 | + Promise<String> p6 = Promise.promise(); |
| 143 | + Future<String> f6 = p6.future(); |
| 144 | + Promise<String> p7 = Promise.promise(); |
| 145 | + Future<String> f7 = p7.future(); |
| 146 | + Promise<String> p8 = Promise.promise(); |
| 147 | + Future<String> f8 = p8.future(); |
| 148 | + CompositeFuture composite = join.apply(f1, f2, f3, f4, f5, f6, f7, f8); |
| 149 | + Checker<CompositeFuture> checker = new Checker<>(composite); |
| 150 | + p1.complete("f1"); |
| 151 | + p2.complete("f2"); |
| 152 | + p3.complete("f3"); |
| 153 | + p4.complete("f4"); |
| 154 | + p5.complete("f5"); |
| 155 | + p6.complete("f6"); |
| 156 | + p7.complete("f7"); |
| 157 | + p8.complete("f8"); |
| 158 | + checker.assertSucceeded(composite); |
| 159 | + assertEquals("f1", composite.<String>resultAt(0)); |
| 160 | + assertEquals("f2", composite.<String>resultAt(1)); |
| 161 | + assertEquals("f3", composite.<String>resultAt(2)); |
| 162 | + assertEquals("f4", composite.<String>resultAt(3)); |
| 163 | + assertEquals("f5", composite.<String>resultAt(4)); |
| 164 | + assertEquals("f6", composite.<String>resultAt(5)); |
| 165 | + assertEquals("f7", composite.<String>resultAt(6)); |
| 166 | + assertEquals("f8", composite.<String>resultAt(7)); |
| 167 | + } |
| 168 | + |
39 | 169 | @Test |
40 | 170 | public void testCreateWithHandler() { |
41 | 171 | AtomicInteger count = new AtomicInteger(); |
@@ -1927,4 +2057,16 @@ public void testOnCompleteParameterTypeIsContravariant(Future<String> fut, Compl |
1927 | 2057 | String cq = res; |
1928 | 2058 | }); |
1929 | 2059 | } |
| 2060 | + |
| 2061 | + @FunctionalInterface |
| 2062 | + private interface EightFunction<A, B, C, D, E, F, G, H, R> { |
| 2063 | + |
| 2064 | + R apply(A a, B b, C c, D d, E e, F f, G g, H h); |
| 2065 | + |
| 2066 | + default <V> EightFunction<A, B, C, D, E, F, G, H, V> andThen( |
| 2067 | + Function<? super R, ? extends V> after) { |
| 2068 | + Objects.requireNonNull(after); |
| 2069 | + return (a, b, c, d, e, f, g, h) -> after.apply(apply(a, b, c, d, e, f, g, h)); |
| 2070 | + } |
| 2071 | + } |
1930 | 2072 | } |
0 commit comments