[Java] Add runtime context (#4194)

This commit is contained in:
Wang Qing
2019-03-05 20:25:29 +08:00
committed by Hao Chen
parent c73d5086f3
commit a116b7f646
12 changed files with 238 additions and 2 deletions
@@ -24,6 +24,16 @@ public class ActorReconstructionTest extends BaseTest {
protected int value = 0;
private boolean wasCurrentActorReconstructed = false;
public Counter() {
wasCurrentActorReconstructed = Ray.getRuntimeContext().wasCurrentActorReconstructed();
}
public boolean wasCurrentActorReconstructed() {
return wasCurrentActorReconstructed;
}
public int increase() {
value += 1;
return value;
@@ -48,6 +58,8 @@ public class ActorReconstructionTest extends BaseTest {
Ray.call(Counter::increase, actor).get();
}
Assert.assertFalse(Ray.call(Counter::wasCurrentActorReconstructed, actor).get());
// Kill the actor process.
int pid = Ray.call(Counter::getPid, actor).get();
Runtime.getRuntime().exec("kill -9 " + pid);
@@ -58,6 +70,8 @@ public class ActorReconstructionTest extends BaseTest {
int value = Ray.call(Counter::increase, actor).get();
Assert.assertEquals(value, 4);
Assert.assertTrue(Ray.call(Counter::wasCurrentActorReconstructed, actor).get());
// Kill the actor process again.
pid = Ray.call(Counter::getPid, actor).get();
Runtime.getRuntime().exec("kill -9 " + pid);
@@ -0,0 +1,52 @@
package org.ray.api.test;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.annotation.RayRemote;
import org.ray.api.id.UniqueId;
import org.testng.Assert;
import org.testng.annotations.Test;
public class RuntimeContextTest extends BaseTest {
private static UniqueId DRIVER_ID =
UniqueId.fromHexString("0011223344556677889900112233445566778899");
private static String RAYLET_SOCKET_NAME = "/tmp/ray/test/raylet_socket";
private static String OBJECT_STORE_SOCKET_NAME = "/tmp/ray/test/object_store_socket";
@Override
public void beforeInitRay() {
System.setProperty("ray.driver.id", DRIVER_ID.toString());
System.setProperty("ray.raylet.socket-name", RAYLET_SOCKET_NAME);
System.setProperty("ray.object-store.socket-name", OBJECT_STORE_SOCKET_NAME);
}
@Test
public void testRuntimeContextInDriver() {
Assert.assertEquals(DRIVER_ID, Ray.getRuntimeContext().getCurrentDriverId());
Assert.assertEquals(RAYLET_SOCKET_NAME, Ray.getRuntimeContext().getRayletSocketName());
Assert.assertEquals(OBJECT_STORE_SOCKET_NAME,
Ray.getRuntimeContext().getObjectStoreSocketName());
}
@RayRemote
public static class RuntimeContextTester {
public String testRuntimeContext(UniqueId actorId) {
Assert.assertEquals(DRIVER_ID, Ray.getRuntimeContext().getCurrentDriverId());
Assert.assertEquals(actorId, Ray.getRuntimeContext().getCurrentActorId());
Assert.assertEquals(RAYLET_SOCKET_NAME, Ray.getRuntimeContext().getRayletSocketName());
Assert.assertEquals(OBJECT_STORE_SOCKET_NAME,
Ray.getRuntimeContext().getObjectStoreSocketName());
return "ok";
}
}
@Test
public void testRuntimeContextInActor() {
RayActor<RuntimeContextTester> actor = Ray.createActor(RuntimeContextTester::new);
Assert.assertEquals("ok",
Ray.call(RuntimeContextTester::testRuntimeContext, actor, actor.getId()).get());
}
}