[Java] Fix getCurrentActorId in multi-threading scenario. (#5506)

This commit is contained in:
Kai Yang
2019-08-23 17:56:10 +08:00
committed by Hao Chen
parent b520f6141e
commit 7812dd5636
3 changed files with 50 additions and 20 deletions
@@ -15,6 +15,7 @@ import org.ray.api.RayObject;
import org.ray.api.TestUtils;
import org.ray.api.WaitResult;
import org.ray.api.annotation.RayRemote;
import org.ray.api.id.ActorId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
@@ -42,6 +43,33 @@ public class MultiThreadingTest extends BaseTest {
}
}
@RayRemote
public static class ActorIdTester {
private final ActorId actorId;
public ActorIdTester() {
actorId = Ray.getRuntimeContext().getCurrentActorId();
Assert.assertNotEquals(actorId, ActorId.NIL);
}
@RayRemote
public ActorId getCurrentActorId() {
final ActorId[] result = new ActorId[1];
Thread thread = new Thread(() -> {
result[0] = Ray.getRuntimeContext().getCurrentActorId();
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Assert.assertEquals(result[0], actorId);
return result[0];
}
}
public static String testMultiThreading() {
Random random = new Random();
// Test calling normal functions.
@@ -105,6 +133,14 @@ public class MultiThreadingTest extends BaseTest {
Assert.assertEquals("ok", obj.get());
}
@Test
public void testGetCurrentActorId() {
TestUtils.skipTestUnderSingleProcess();
RayActor<ActorIdTester> actorIdTester = Ray.createActor(ActorIdTester::new);
ActorId actorId = Ray.call(ActorIdTester::getCurrentActorId, actorIdTester).get();
Assert.assertEquals(actorId, actorIdTester.getId());
}
private static void runTestCaseInMultipleThreads(Runnable testCase, int numRepeats) {
ExecutorService service = Executors.newFixedThreadPool(NUM_THREADS);