|
@@ -234,4 +234,53 @@ public class ActionListenerTests extends ESTestCase {
|
|
|
assertThat(onFailureListener.isDone(), equalTo(true));
|
|
|
assertThat(expectThrows(ExecutionException.class, onFailureListener::get).getCause(), instanceOf(IOException.class));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that map passes the output of the function to its delegate listener and that exceptions in the function are propagated to the
|
|
|
+ * onFailure handler. Also verify that exceptions from ActionListener.onResponse does not invoke onFailure, since it is the
|
|
|
+ * responsibility of the ActionListener implementation (the client of the API) to handle exceptions in onResponse and onFailure.
|
|
|
+ */
|
|
|
+ public void testMap() {
|
|
|
+ AtomicReference<Exception> exReference = new AtomicReference<>();
|
|
|
+
|
|
|
+ ActionListener<String> listener = new ActionListener<>() {
|
|
|
+ @Override
|
|
|
+ public void onResponse(String s) {
|
|
|
+ if (s == null) {
|
|
|
+ throw new IllegalArgumentException("simulate onResponse exception");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ exReference.set(e);
|
|
|
+ if (e instanceof IllegalArgumentException) {
|
|
|
+ throw (IllegalArgumentException) e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ ActionListener<Boolean> mapped = ActionListener.map(listener, b -> {
|
|
|
+ if (b == null) {
|
|
|
+ return null;
|
|
|
+ } else if (b) {
|
|
|
+ throw new IllegalStateException("simulate map function exception");
|
|
|
+ } else {
|
|
|
+ return b.toString();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ AssertionError assertionError = expectThrows(AssertionError.class, () -> mapped.onResponse(null));
|
|
|
+ assertThat(assertionError.getCause().getCause(), instanceOf(IllegalArgumentException.class));
|
|
|
+ assertNull(exReference.get());
|
|
|
+ mapped.onResponse(false);
|
|
|
+ assertNull(exReference.get());
|
|
|
+ mapped.onResponse(true);
|
|
|
+ assertThat(exReference.get(), instanceOf(IllegalStateException.class));
|
|
|
+
|
|
|
+ assertionError = expectThrows(AssertionError.class, () -> mapped.onFailure(new IllegalArgumentException()));
|
|
|
+ assertThat(assertionError.getCause().getCause(), instanceOf(IllegalArgumentException.class));
|
|
|
+ assertThat(exReference.get(), instanceOf(IllegalArgumentException.class));
|
|
|
+ mapped.onFailure(new IllegalStateException());
|
|
|
+ assertThat(exReference.get(), instanceOf(IllegalStateException.class));
|
|
|
+ }
|
|
|
}
|