Removing FutureFactory and reintroducing Futures (for Java API)

This commit is contained in:
Viktor Klang 2011-12-12 14:39:10 +01:00
parent 0b6a1a0e54
commit 7eced71a85
2 changed files with 46 additions and 52 deletions

View file

@ -23,14 +23,12 @@ import akka.testkit.AkkaSpec;
public class JavaFutureTests {
private static ActorSystem system;
private volatile static FutureFactory ff;
private static Timeout t;
@BeforeClass
public static void beforeAll() {
system = ActorSystem.create("JavaFutureTests", AkkaSpec.testConf());
t = system.settings().ActorTimeout();
ff = new FutureFactory(system.dispatcher());
}
@AfterClass
@ -42,11 +40,11 @@ public class JavaFutureTests {
@Test
public void mustBeAbleToMapAFuture() {
Future<String> f1 = ff.future(new Callable<String>() {
Future<String> f1 = Futures.future(new Callable<String>() {
public String call() {
return "Hello";
}
});
}, system.dispatcher());
Future<String> f2 = f1.map(new Function<String, String>() {
public String apply(String s) {
@ -60,7 +58,7 @@ public class JavaFutureTests {
@Test
public void mustBeAbleToExecuteAnOnResultCallback() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = ff.<String>promise();
Promise<String> cf = Futures.promise(system.dispatcher());
Future<String> f = cf;
f.onResult(new Procedure<String>() {
public void apply(String result) {
@ -77,7 +75,7 @@ public class JavaFutureTests {
@Test
public void mustBeAbleToExecuteAnOnExceptionCallback() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = ff.<String>promise();
Promise<String> cf = Futures.promise(system.dispatcher());
Future<String> f = cf;
f.onException(new Procedure<Throwable>() {
public void apply(Throwable t) {
@ -95,7 +93,7 @@ public class JavaFutureTests {
@Test
public void mustBeAbleToExecuteAnOnCompleteCallback() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = ff.<String>promise();
Promise<String> cf = Futures.promise(system.dispatcher());
Future<String> f = cf;
f.onComplete(new Procedure<Future<String>>() {
public void apply(akka.dispatch.Future<String> future) {
@ -111,7 +109,7 @@ public class JavaFutureTests {
@Test
public void mustBeAbleToForeachAFuture() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = ff.<String>promise();
Promise<String> cf = Futures.promise(system.dispatcher());
Future<String> f = cf;
f.foreach(new Procedure<String>() {
public void apply(String future) {
@ -127,13 +125,13 @@ public class JavaFutureTests {
@Test
public void mustBeAbleToFlatMapAFuture() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = ff.<String>promise();
Promise<String> cf = Futures.promise(system.dispatcher());
cf.completeWithResult("1000");
Future<String> f = cf;
Future<Integer> r = f.flatMap(new Function<String, Future<Integer>>() {
public Future<Integer> apply(String r) {
latch.countDown();
Promise<Integer> cf = ff.<Integer>promise();
Promise<Integer> cf = Futures.promise(system.dispatcher());
cf.completeWithResult(Integer.parseInt(r));
return cf;
}
@ -147,7 +145,7 @@ public class JavaFutureTests {
@Test
public void mustBeAbleToFilterAFuture() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
Promise<String> cf = ff.<String>promise();
Promise<String> cf = Futures.promise(system.dispatcher());
Future<String> f = cf;
Future<String> r = f.filter(new Function<String, Boolean>() {
public Boolean apply(String r) {
@ -170,14 +168,14 @@ public class JavaFutureTests {
for (int i = 0; i < 10; i++) {
listExpected.add("test");
listFutures.add(ff.future(new Callable<String>() {
listFutures.add(Futures.future(new Callable<String>() {
public String call() {
return "test";
}
}));
}, system.dispatcher()));
}
Future<Iterable<String>> futureList = ff.sequence(listFutures);
Future<Iterable<String>> futureList = Futures.sequence(listFutures, system.dispatcher());
assertEquals(futureList.get(), listExpected);
}
@ -190,18 +188,18 @@ public class JavaFutureTests {
for (int i = 0; i < 10; i++) {
expected.append("test");
listFutures.add(ff.future(new Callable<String>() {
listFutures.add(Futures.future(new Callable<String>() {
public String call() {
return "test";
}
}));
}, system.dispatcher()));
}
Future<String> result = ff.fold("", listFutures, new Function2<String, String, String>() {
Future<String> result = Futures.fold("", listFutures, new Function2<String, String, String>() {
public String apply(String r, String t) {
return r + t;
}
});
}, system.dispatcher());
assertEquals(result.get(), expected.toString());
}
@ -213,18 +211,18 @@ public class JavaFutureTests {
for (int i = 0; i < 10; i++) {
expected.append("test");
listFutures.add(ff.future(new Callable<String>() {
listFutures.add(Futures.future(new Callable<String>() {
public String call() {
return "test";
}
}));
}, system.dispatcher()));
}
Future<String> result = ff.reduce(listFutures, new Function2<String, String, String>() {
Future<String> result = Futures.reduce(listFutures, new Function2<String, String, String>() {
public String apply(String r, String t) {
return r + t;
}
});
}, system.dispatcher());
assertEquals(result.get(), expected.toString());
}
@ -239,15 +237,15 @@ public class JavaFutureTests {
listStrings.add("test");
}
Future<Iterable<String>> result = ff.traverse(listStrings, new Function<String, Future<String>>() {
Future<Iterable<String>> result = Futures.traverse(listStrings, new Function<String, Future<String>>() {
public Future<String> apply(final String r) {
return ff.future(new Callable<String>() {
return Futures.future(new Callable<String>() {
public String call() {
return r.toUpperCase();
}
});
}, system.dispatcher());
}
});
}, system.dispatcher());
assertEquals(result.get(), expectedStrings);
}
@ -257,25 +255,25 @@ public class JavaFutureTests {
LinkedList<Future<Integer>> listFutures = new LinkedList<Future<Integer>>();
for (int i = 0; i < 10; i++) {
final Integer fi = i;
listFutures.add(ff.future(new Callable<Integer>() {
listFutures.add(Futures.future(new Callable<Integer>() {
public Integer call() {
return fi;
}
}));
}, system.dispatcher()));
}
final Integer expect = 5;
Future<Option<Integer>> f = ff.find(listFutures, new Function<Integer, Boolean>() {
Future<Option<Integer>> f = Futures.find(listFutures, new Function<Integer, Boolean>() {
public Boolean apply(Integer i) {
return i == 5;
}
});
}, system.dispatcher());
assertEquals(expect, Block.sync(f, Duration.create(5, TimeUnit.SECONDS)));
}
@Test
public void BlockMustBeCallable() {
Promise<String> p = ff.<String>promise();
Promise<String> p = Futures.promise(system.dispatcher());
Duration d = Duration.create(1, TimeUnit.SECONDS);
p.completeWithResult("foo");
Block.on(p, d);