[Java] Fix bug when actor creation task fails (#3740)

* [Java] Fix bug when actor creation task fails

* remove imports
This commit is contained in:
Hao Chen
2019-01-14 11:09:15 +08:00
committed by Yuhong Guo
parent 27c20a41a9
commit 1bb20badec
3 changed files with 95 additions and 8 deletions
@@ -0,0 +1,63 @@
package org.ray.api.test;
import org.junit.Assert;
import org.junit.Test;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.exception.RayException;
public class FailureTest extends BaseTest {
private static final String EXCEPTION_MESSAGE = "Oops";
public static int badFunc() {
throw new RuntimeException(EXCEPTION_MESSAGE);
}
public static class BadActor {
public BadActor(boolean failOnCreation) {
if (failOnCreation) {
throw new RuntimeException(EXCEPTION_MESSAGE);
}
}
public int func() {
throw new RuntimeException(EXCEPTION_MESSAGE);
}
}
private static void assertTaskFail(RayObject<?> rayObject) {
try {
rayObject.get();
Assert.fail("Task didn't fail.");
} catch (RayException e) {
e.printStackTrace();
Throwable rootCause = e.getCause();
while (rootCause.getCause() != null) {
rootCause = rootCause.getCause();
}
Assert.assertTrue(rootCause instanceof RuntimeException);
Assert.assertEquals(rootCause.getMessage(), EXCEPTION_MESSAGE);
}
}
@Test
public void testNormalTaskFailure() {
assertTaskFail(Ray.call(FailureTest::badFunc));
}
@Test
public void testActorCreationFailure() {
RayActor<BadActor> actor = Ray.createActor(BadActor::new, true);
assertTaskFail(Ray.call(BadActor::func, actor));
}
@Test
public void testActorTaskFailure() {
RayActor<BadActor> actor = Ray.createActor(BadActor::new, false);
assertTaskFail(Ray.call(BadActor::func, actor));
}
}