2011-03-01 15:23:29 -07:00
|
|
|
package akka.dispatch;
|
|
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
import java.util.concurrent.Callable;
|
2011-04-15 13:09:53 -06:00
|
|
|
import java.util.LinkedList;
|
2011-03-11 10:46:36 -07:00
|
|
|
import akka.japi.Function;
|
|
|
|
|
import akka.japi.Procedure;
|
2011-03-01 15:23:29 -07:00
|
|
|
import scala.Some;
|
|
|
|
|
import scala.Right;
|
|
|
|
|
import static akka.dispatch.Futures.future;
|
2011-04-15 13:09:53 -06:00
|
|
|
import static akka.dispatch.Futures.traverse;
|
|
|
|
|
import static akka.dispatch.Futures.sequence;
|
2011-03-01 15:23:29 -07:00
|
|
|
|
2011-04-15 13:09:53 -06:00
|
|
|
public class JavaFutureTests {
|
2011-03-01 15:23:29 -07:00
|
|
|
|
|
|
|
|
@Test public void mustBeAbleToMapAFuture() {
|
2011-04-15 13:09:53 -06:00
|
|
|
Future<String> f1 = future(new Callable<String>() {
|
2011-03-01 15:23:29 -07:00
|
|
|
public String call() {
|
|
|
|
|
return "Hello";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2011-04-15 13:09:53 -06:00
|
|
|
Future<String> f2 = f1.map(new Function<String, String>() {
|
2011-03-01 15:23:29 -07:00
|
|
|
public String apply(String s) {
|
|
|
|
|
return s + " World";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2011-04-15 13:09:53 -06:00
|
|
|
assertEquals("Hello World", f2.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Improve this test, perhaps with an Actor
|
|
|
|
|
@Test public void mustSequenceAFutureList() {
|
|
|
|
|
LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
|
|
|
|
|
LinkedList<String> listExpected = new LinkedList<String>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
listExpected.add("test");
|
|
|
|
|
listFutures.add(future(new Callable<String>() {
|
|
|
|
|
public String call() {
|
|
|
|
|
return "test";
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<LinkedList<String>> futureList = sequence(listFutures);
|
|
|
|
|
|
|
|
|
|
assertEquals(futureList.get(), listExpected);
|
2011-03-01 15:23:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|