mirror of
https://github.com/wassname/ray.git
synced 2026-07-18 12:40:56 +08:00
[Java] Fix getCurrentActorId in multi-threading scenario. (#5506)
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user