-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAsyncTest.java
More file actions
240 lines (195 loc) · 6.29 KB
/
AsyncTest.java
File metadata and controls
240 lines (195 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.alexn.async;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class AsyncTest {
private final int count = 10000;
private ExecutorService ec;
@Before
public void setup() {
ec = Executors.newFixedThreadPool(4);
}
@After
public void tearDown() {
ec.shutdown();
}
@Test public void eval() {
Async<Integer> fa = Async.eval(() -> 1 + 1);
assertEquals(await(fa, ec).intValue(), 2);
// Repeating the evaluation should yield the same value
assertEquals(await(fa, ec).intValue(), 2);
}
/**
* TIP: If the `times` equality test fails, it means the returned
* instance has shared mutable state, possibly caching the result.
*
* It shouldn't. `Async` is similar with Java's `Future`, but it
* needs to behave like a function that doesn't do any caching.
*/
@Test public void evalIsNotMemoized() {
final AtomicInteger times = new AtomicInteger(0);
Async<Integer> fa = Async.eval(() -> {
times.incrementAndGet();
return 1 + 1;
});
assertEquals(await(fa, ec).intValue(), 2);
assertEquals(await(fa, ec).intValue(), 2);
assertEquals(times.get(), 2);
}
@Test public void toFuture() throws Exception {
final AtomicInteger times = new AtomicInteger(0);
Async<Integer> task = Async.eval(() -> {
times.incrementAndGet();
return 1 + 1;
});
assertEquals(task.toFuture(ec).get().intValue(), 2);
assertEquals(task.toFuture(ec).get().intValue(), 2);
// Should not do memoization
assertEquals(times.get(), 2);
}
@Test public void fromFuture() {
final AtomicInteger times = new AtomicInteger(0);
final Async<Integer> f =
Async.fromFuture(() ->
CompletableFuture.supplyAsync(times::incrementAndGet, ec)
);
assertEquals(await(f, ec).intValue(), 1);
assertEquals(await(f, ec).intValue(), 2);
assertEquals(await(f, ec).intValue(), 3);
}
@Test public void mapIdentity() {
Async<Integer> task = Async
.eval(() -> 1 + 1)
.map(x -> x);
assertEquals(await(task, ec).intValue(), 2);
}
@Test public void mapAssociativity() {
Async<Integer> lh = Async
.eval(() -> 1 + 1)
.map(x -> x * 2)
.map(x -> x + 2);
Async<Integer> rh = Async
.eval(() -> 1 + 1)
.map(x -> x * 2 + 2);
assertEquals(await(lh, ec).intValue(), await(rh, ec).intValue());
}
/**
* TIP: if the above tests don't fail, but this one does, it means
* that you haven't used the `executor` for scheduling the transformation
* of the result. Use the `executor` to make `map` stack safe.
*/
@Test public void mapIsStackSafe() {
final int count = 10000;
Async<Integer> ref = Async.eval(() -> 0);
for (int i = 0; i < count; i++) {
ref = ref.map(x -> x + 1);
}
assertEquals(await(ref, ec).intValue(), count);
}
@Test public void flatMapRightIdentity() {
Async<Integer> lh = Async
.eval(() -> 1 + 1)
.flatMap(x -> Async.eval(() -> x));
assertEquals(await(lh, ec).intValue(), 2);
}
@Test public void flatMapAssociativity() {
Async<Integer> lh = Async
.eval(() -> 1 + 1)
.flatMap(x -> Async.eval(() -> x * 2))
.flatMap(x -> Async.eval(() -> x + 2));
Async<Integer> rh = Async
.eval(() -> 1 + 1)
.flatMap(x -> Async
.eval(() -> x * 2)
.flatMap(y -> Async.eval(() -> y + 2)));
assertEquals(await(lh, ec).intValue(), await(rh, ec).intValue());
}
/**
* TIP: if the above tests don't fail, but this one does, it means
* that you haven't used the `executor` for scheduling the transformation
* of the result. Use the `executor` to make `flatMap` stack safe.
*/
@Test public void flatMapIsStackSafe() {
final int count = 10000;
Async<Integer> ref = Async.eval(() -> 0);
for (int i = 0; i < count; i++) {
ref = ref.flatMap(x -> Async.eval(() -> x + 1));
}
assertEquals(await(ref, ec).intValue(), count);
}
@Test
public void sequence() {
final ArrayList<Async<Integer>> list = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
list.add(Async.eval(() -> 1 + 1));
}
Async<Integer> sum = Async.sequence(list)
.map(l -> l.stream().reduce(0, Integer::sum));
assertEquals(await(sum, ec).intValue(), count * 2);
}
@Test
public void parMap2() throws InterruptedException, ExecutionException {
final CountDownLatch workersStarted = new CountDownLatch(2);
final CountDownLatch release = new CountDownLatch(1);
final Async<Integer> task = Async.eval(() -> {
workersStarted.countDown();
try {
assertTrue(release.await(10L, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return 10;
});
final CompletableFuture<Integer> result =
Async
.parMap2(task, task, Integer::sum)
.toFuture(ec);
assertTrue(workersStarted.await(10L, TimeUnit.SECONDS));
release.countDown();
assertEquals(result.get().intValue(), 20);
}
@Test
public void parallel() {
final ArrayList<Async<Integer>> list = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
list.add(Async.eval(() -> 1 + 1));
}
Async<Integer> sum = Async.parallel(list)
.map(l -> l.stream().reduce(0, Integer::sum));
assertEquals(await(sum, ec).intValue(), count * 2);
}
public static <A> A await(Async<A> fa, Executor e) {
BlockingCallback<A> cb = new BlockingCallback<>();
fa.run(e, cb);
return cb.get();
}
static class BlockingCallback<A> implements Callback<A> {
private A value = null;
private Throwable e = null;
private final CountDownLatch l;
public BlockingCallback() {
this.l = new CountDownLatch(1);
}
@Override
public void onSuccess(A value) {
this.value = value;
l.countDown();
}
@Override
public void onError(Throwable e) {
this.e = e;
l.countDown();
}
public A get() {
try { l.await(3, TimeUnit.SECONDS); }
catch (InterruptedException ignored) {}
if (e != null) throw new RuntimeException(e);
return this.value;
}
}
}