/akka-actor-tests/src/test/java/akka/dispatch/JavaFutureTests.java

https://github.com/andreum/akka · Java · 257 lines · 217 code · 38 blank · 2 comment · 8 complexity · 5fc8d17db3b6a24f4e3693d3a1211220 MD5 · raw file

  1. package akka.dispatch;
  2. import akka.actor.Timeout;
  3. import org.junit.Test;
  4. import static org.junit.Assert.*;
  5. import java.util.concurrent.Callable;
  6. import java.util.LinkedList;
  7. import java.lang.Iterable;
  8. import java.util.concurrent.CountDownLatch;
  9. import java.util.concurrent.TimeUnit;
  10. import akka.japi.Function;
  11. import akka.japi.Function2;
  12. import akka.japi.Procedure;
  13. import akka.japi.Option;
  14. import scala.Some;
  15. import scala.Right;
  16. import static akka.dispatch.Futures.*;
  17. public class JavaFutureTests {
  18. @Test public void mustBeAbleToMapAFuture() {
  19. Future<String> f1 = future(new Callable<String>() {
  20. public String call() {
  21. return "Hello";
  22. }
  23. });
  24. Future<String> f2 = f1.map(new Function<String, String>() {
  25. public String apply(String s) {
  26. return s + " World";
  27. }
  28. });
  29. assertEquals("Hello World", f2.get());
  30. }
  31. @Test public void mustBeAbleToExecuteAnOnResultCallback() throws Throwable {
  32. final CountDownLatch latch = new CountDownLatch(1);
  33. Promise<String> cf = new akka.dispatch.DefaultPromise<String>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  34. Future<String> f = cf;
  35. f.onResult(new Procedure<String>() {
  36. public void apply(String result) {
  37. if(result.equals("foo"))
  38. latch.countDown();
  39. }
  40. });
  41. cf.completeWithResult("foo");
  42. assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
  43. assertEquals(f.get(), "foo");
  44. }
  45. @Test public void mustBeAbleToExecuteAnOnExceptionCallback() throws Throwable {
  46. final CountDownLatch latch = new CountDownLatch(1);
  47. Promise<String> cf = new akka.dispatch.DefaultPromise<String>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  48. Future<String> f = cf;
  49. f.onException(new Procedure<Throwable>() {
  50. public void apply(Throwable t) {
  51. if(t instanceof NullPointerException)
  52. latch.countDown();
  53. }
  54. });
  55. Throwable exception = new NullPointerException();
  56. cf.completeWithException(exception);
  57. assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
  58. assertEquals(f.exception().get(), exception);
  59. }
  60. @Test public void mustBeAbleToExecuteAnOnTimeoutCallback() throws Throwable {
  61. final CountDownLatch latch = new CountDownLatch(1);
  62. Promise<String> cf = new akka.dispatch.DefaultPromise<String>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  63. Future<String> f = cf;
  64. f.onTimeout(new Procedure<Future<String>>() {
  65. public void apply(Future<String> future) {
  66. latch.countDown();
  67. }
  68. });
  69. assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
  70. assertTrue(f.value().isEmpty());
  71. }
  72. @Test public void mustBeAbleToExecuteAnOnCompleteCallback() throws Throwable {
  73. final CountDownLatch latch = new CountDownLatch(1);
  74. Promise<String> cf = new akka.dispatch.DefaultPromise<String>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  75. Future<String> f = cf;
  76. f.onComplete(new Procedure<Future<String>>() {
  77. public void apply(akka.dispatch.Future<String> future) {
  78. latch.countDown();
  79. }
  80. });
  81. cf.completeWithResult("foo");
  82. assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
  83. assertEquals(f.get(), "foo");
  84. }
  85. @Test public void mustBeAbleToForeachAFuture() throws Throwable {
  86. final CountDownLatch latch = new CountDownLatch(1);
  87. Promise<String> cf = new akka.dispatch.DefaultPromise<String>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  88. Future<String> f = cf;
  89. f.foreach(new Procedure<String>() {
  90. public void apply(String future) {
  91. latch.countDown();
  92. }
  93. });
  94. cf.completeWithResult("foo");
  95. assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
  96. assertEquals(f.get(), "foo");
  97. }
  98. @Test public void mustBeAbleToFlatMapAFuture() throws Throwable {
  99. final CountDownLatch latch = new CountDownLatch(1);
  100. Promise<String> cf = new akka.dispatch.DefaultPromise<String>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  101. cf.completeWithResult("1000");
  102. Future<String> f = cf;
  103. Future<Integer> r = f.flatMap(new Function<String, Future<Integer>>() {
  104. public Future<Integer> apply(String r) {
  105. latch.countDown();
  106. Promise<Integer> cf = new akka.dispatch.DefaultPromise<Integer>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  107. cf.completeWithResult(Integer.parseInt(r));
  108. return cf;
  109. }
  110. });
  111. assertEquals(f.get(), "1000");
  112. assertEquals(r.get().intValue(), 1000);
  113. assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
  114. }
  115. @Test public void mustBeAbleToFilterAFuture() throws Throwable {
  116. final CountDownLatch latch = new CountDownLatch(1);
  117. Promise<String> cf = new akka.dispatch.DefaultPromise<String>(1000, TimeUnit.MILLISECONDS, Dispatchers.defaultGlobalDispatcher());
  118. Future<String> f = cf;
  119. Future<String> r = f.filter(new Function<String, Boolean>() {
  120. public Boolean apply(String r) {
  121. latch.countDown();
  122. return r.equals("foo");
  123. }
  124. });
  125. cf.completeWithResult("foo");
  126. assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
  127. assertEquals(f.get(), "foo");
  128. assertEquals(r.get(), "foo");
  129. }
  130. // TODO: Improve this test, perhaps with an Actor
  131. @Test public void mustSequenceAFutureList() {
  132. LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
  133. LinkedList<String> listExpected = new LinkedList<String>();
  134. for (int i = 0; i < 10; i++) {
  135. listExpected.add("test");
  136. listFutures.add(future(new Callable<String>() {
  137. public String call() {
  138. return "test";
  139. }
  140. }));
  141. }
  142. Future<Iterable<String>> futureList = sequence(listFutures);
  143. assertEquals(futureList.get(), listExpected);
  144. }
  145. // TODO: Improve this test, perhaps with an Actor
  146. @Test public void foldForJavaApiMustWork() {
  147. LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
  148. StringBuilder expected = new StringBuilder();
  149. for (int i = 0; i < 10; i++) {
  150. expected.append("test");
  151. listFutures.add(future(new Callable<String>() {
  152. public String call() {
  153. return "test";
  154. }
  155. }));
  156. }
  157. Future<String> result = fold("", 15000,listFutures, new Function2<String,String,String>(){
  158. public String apply(String r, String t) {
  159. return r + t;
  160. }
  161. });
  162. assertEquals(result.get(), expected.toString());
  163. }
  164. @Test public void reduceForJavaApiMustWork() {
  165. LinkedList<Future<String>> listFutures = new LinkedList<Future<String>>();
  166. StringBuilder expected = new StringBuilder();
  167. for (int i = 0; i < 10; i++) {
  168. expected.append("test");
  169. listFutures.add(future(new Callable<String>() {
  170. public String call() {
  171. return "test";
  172. }
  173. }));
  174. }
  175. Future<String> result = reduce(listFutures, 15000, new Function2<String,String,String>(){
  176. public String apply(String r, String t) {
  177. return r + t;
  178. }
  179. });
  180. assertEquals(result.get(), expected.toString());
  181. }
  182. @Test public void traverseForJavaApiMustWork() {
  183. LinkedList<String> listStrings = new LinkedList<String>();
  184. LinkedList<String> expectedStrings = new LinkedList<String>();
  185. for (int i = 0; i < 10; i++) {
  186. expectedStrings.add("TEST");
  187. listStrings.add("test");
  188. }
  189. Future<Iterable<String>> result = traverse(listStrings, new Function<String,Future<String>>(){
  190. public Future<String> apply(final String r) {
  191. return future(new Callable<String>() {
  192. public String call() {
  193. return r.toUpperCase();
  194. }
  195. });
  196. }
  197. });
  198. assertEquals(result.get(), expectedStrings);
  199. }
  200. @Test public void findForJavaApiMustWork() {
  201. LinkedList<Future<Integer>> listFutures = new LinkedList<Future<Integer>>();
  202. for (int i = 0; i < 10; i++) {
  203. final Integer fi = i;
  204. listFutures.add(future(new Callable<Integer>() {
  205. public Integer call() {
  206. return fi;
  207. }
  208. }));
  209. }
  210. final Integer expect = 5;
  211. Future<Option<Integer>> f = Futures.find(listFutures, new Function<Integer,Boolean>() {
  212. public Boolean apply(Integer i) {
  213. return i == 5;
  214. }
  215. }, Timeout.getDefault());
  216. final Integer got = f.get().get();
  217. assertEquals(expect, got);
  218. }
  219. }