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-05-11 13:29:29 +02:00
|
|
|
import java.lang.Iterable;
|
2011-03-11 10:46:36 -07:00
|
|
|
import akka.japi.Function;
|
2011-05-10 19:19:13 +02:00
|
|
|
import akka.japi.Function2;
|
2011-03-11 10:46:36 -07:00
|
|
|
import akka.japi.Procedure;
|
2011-03-01 15:23:29 -07:00
|
|
|
import scala.Some;
|
|
|
|
|
import scala.Right;
|
2011-05-10 19:19:13 +02:00
|
|
|
import static akka.dispatch.Futures.*;
|
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";
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-11 13:29:29 +02:00
|
|
|
Future<Iterable<String>> futureList = sequence(listFutures);
|
2011-04-15 13:09:53 -06:00
|
|
|
|
|
|
|
|
assertEquals(futureList.get(), listExpected);
|
2011-03-01 15:23:29 -07:00
|
|
|
}
|
|
|
|
|
|
2011-05-10 19:19:13 +02:00
|
|
|
// TODO: Improve this test, perhaps with an Actor
|
|
|
|
|
@Test public void foldForJavaApiMustWork() {
|
|
|
|
|
LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
|
|
|
|
|
StringBuilder expected = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
expected.append("test");
|
|
|
|
|
listFutures.add(future(new Callable<String>() {
|
|
|
|
|
public String call() {
|
|
|
|
|
return "test";
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> result = fold("", 15000,listFutures, new Function2<String,String,String>(){
|
|
|
|
|
public String apply(String r, String t) {
|
|
|
|
|
return r + t;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assertEquals(result.get(), expected.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test public void reduceForJavaApiMustWork() {
|
|
|
|
|
LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
|
|
|
|
|
StringBuilder expected = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
expected.append("test");
|
|
|
|
|
listFutures.add(future(new Callable<String>() {
|
|
|
|
|
public String call() {
|
|
|
|
|
return "test";
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> result = reduce(listFutures, 15000, new Function2<String,String,String>(){
|
|
|
|
|
public String apply(String r, String t) {
|
|
|
|
|
return r + t;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assertEquals(result.get(), expected.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test public void traverseForJavaApiMustWork() {
|
|
|
|
|
LinkedList<String> listStrings = new LinkedList<String>();
|
|
|
|
|
LinkedList<String> expectedStrings = new LinkedList<String>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
expectedStrings.add("TEST");
|
|
|
|
|
listStrings.add("test");
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-11 13:29:29 +02:00
|
|
|
Future<Iterable<String>> result = traverse(listStrings, new Function<String,Future<String>>(){
|
2011-05-10 19:19:13 +02:00
|
|
|
public Future<String> apply(final String r) {
|
|
|
|
|
return future(new Callable<String>() {
|
|
|
|
|
public String call() {
|
|
|
|
|
return r.toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assertEquals(result.get(), expectedStrings);
|
|
|
|
|
}
|
2011-03-01 15:23:29 -07:00
|
|
|
}
|