[Java] improve Java API module (#2783)

API module (`ray/java/api` dir) includes all public APIs provided by Ray, it should be the only module that normal Ray users need to face.

The purpose of this PR to first improve the code quality of the API module. Subsequent PRs will improve other modules later. The changes of this PR include the following aspects: 
1) Only keep interfaces in api module, to hide implementation details from users and fix circular dependencies among modules.
2) Document everything in the api module. 
3) Improve naming.
4) Add more tests for API. 
5) Also fix/improve related code in other modules.
6) Remove some unused code.

(Apologize for posting such a large PR. Java worker code has been lack of maintenance for a while. There're a lot of code quality issues that need to be fixed. We plan to use a couple of large PRs to address them. After that, future changes will come in small PRs.)
This commit is contained in:
Hao Chen
2018-09-03 02:51:16 +08:00
committed by Robert Nishihara
parent 2691b3a11a
commit 3b0a2c4197
98 changed files with 2232 additions and 2158 deletions
@@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
import org.ray.api.test.MyRunner;
@RunWith(MyRunner.class)
@@ -14,7 +14,7 @@ public class ActorPressTest extends RayBenchmarkTest {
@Test
public void singleLatencyTest() {
int times = 10;
RayActor<ActorPressTest.Adder> adder = Ray.create(ActorPressTest.Adder.class);
RayActor<ActorPressTest.Adder> adder = Ray.createActor(ActorPressTest.Adder.class);
super.singleLatencyTest(times, adder);
}
@@ -22,7 +22,7 @@ public class ActorPressTest extends RayBenchmarkTest {
public void maxTest() {
int clientNum = 2;
int totalNum = 20;
RayActor<ActorPressTest.Adder> adder = Ray.create(ActorPressTest.Adder.class);
RayActor<ActorPressTest.Adder> adder = Ray.createActor(ActorPressTest.Adder.class);
PressureTestParameter pressureTestParameter = new PressureTestParameter();
pressureTestParameter.setClientNum(clientNum);
pressureTestParameter.setTotalNum(totalNum);
@@ -36,7 +36,7 @@ public class ActorPressTest extends RayBenchmarkTest {
int clientNum = 2;
int totalQps = 2;
int duration = 3;
RayActor<ActorPressTest.Adder> adder = Ray.create(ActorPressTest.Adder.class);
RayActor<ActorPressTest.Adder> adder = Ray.createActor(ActorPressTest.Adder.class);
PressureTestParameter pressureTestParameter = new PressureTestParameter();
pressureTestParameter.setClientNum(clientNum);
pressureTestParameter.setTotalQps(totalQps);
@@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
import org.ray.api.test.MyRunner;
@RunWith(MyRunner.class)
@@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
import org.ray.api.test.MyRunner;
@RunWith(MyRunner.class)
@@ -10,7 +10,7 @@ import org.junit.Assert;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
import org.ray.util.logger.RayLog;
public abstract class RayBenchmarkTest<T> implements Serializable {
@@ -5,7 +5,7 @@ import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
import org.ray.api.test.MyRunner;
@RunWith(MyRunner.class)
@@ -1,152 +1,59 @@
package org.ray.api.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.UniqueID;
import org.ray.api.annotation.RayRemote;
import org.ray.api.function.RayFunc2;
import org.ray.api.id.UniqueId;
@RunWith(MyRunner.class)
public class ActorTest {
@RayRemote
public static Integer sayWorld(Integer n, RayActor<ActorTest.Adder> adder) {
RayObject<Integer> result = Ray.call(ActorTest.Adder::add, adder, 1);
return result.get() + n;
public static class Counter {
private int value = 0;
public int incr(int delta) {
value += delta;
return value;
}
}
@RayRemote
public static int testActorAsFirstParameter(RayActor<Counter> actor, int delta) {
RayObject<Integer> res = Ray.call(Counter::incr, actor, delta);
return res.get();
}
@RayRemote
public static int testActorAsSecondParameter(int delta, RayActor<Counter> actor) {
RayObject<Integer> res = Ray.call(Counter::incr, actor, delta);
return res.get();
}
@Test
public void test() {
RayActor<ActorTest.Adder> adder = Ray.create(ActorTest.Adder.class);
Ray.call(Adder::set, adder, 10);
RayObject<Integer> result = Ray.call(Adder::add, adder, 1);
Assert.assertEquals(11, (int) result.get());
RayActor<Adder> secondAdder = Ray.create(Adder.class);
RayObject<Integer> result2 = Ray.call(Adder::add, secondAdder, 1);
Assert.assertEquals(1, (int) result2.get());
RayObject<Integer> result3 = Ray.call(Adder::add2, 1);
Assert.assertEquals(2, (int) result3.get());
RayObject<Integer> result4 = Ray.call(ActorTest::sayWorld, 2, adder);
Assert.assertEquals(14, (int) result4.get());
RayActor<Adder2> adder2 = Ray.create(Adder2.class);
Ray.call(Adder2::setAdder, adder2, adder);
RayObject<Integer> result5 = Ray.call(Adder2::increase, adder2);
Assert.assertEquals(1, (int) result5.get());
List list = new ArrayList<>();
list.add(adder);
Ray.call(Adder2::setAdderList, adder2, list);
RayObject<Integer> result7 = Ray.call(Adder2::testActorList, adder2);
Assert.assertEquals(14, (int) result7.get());
List tempList = new ArrayList<>();
tempList.add(result);
Ray.call(Adder::setObjectList, adder, tempList);
RayObject<Integer> result8 = Ray.call(Adder::testObjectList, adder);
Assert.assertEquals(11, (int) result8.get());
public void testCreateAndCallActor() {
// Test creating an actor
RayActor<Counter> actor = Ray.createActor(Counter.class);
Assert.assertNotEquals(actor.getId(), UniqueId.NIL);
// Test calling an actor
RayFunc2<Counter, Integer, Integer> f = Counter::incr;
Assert.assertEquals(Integer.valueOf(1), Ray.call(f, actor, 1).get());
Assert.assertEquals(Integer.valueOf(11), Ray.call(Counter::incr, actor, 10).get());
}
@RayRemote
public static class Adder {
private List<RayObject<Integer>> objectList;
private Integer sum = 0;
public static Integer add2(Integer n) {
return n + 1;
}
public Integer set(Integer n) {
sum = n;
return sum;
}
public Integer increase() {
return (++sum);
}
public Integer add(Integer n) {
return (sum += n);
}
public Integer setObjectList(List<RayObject<Integer>> objectList) {
this.objectList = objectList;
return 1;
}
public Integer testObjectList() {
return ((RayObject<Integer>) objectList.get(0)).get();
}
}
@RayRemote
public static class Adder2 {
private RayActor<Adder> adder;
private List<RayActor<Adder>> adderList;
private UniqueID id;
private Integer sum = 0;
public static Integer add2(Adder a, Integer n) {
return n + 1;
}
public Integer set(Integer n) {
sum = n;
return sum;
}
public Integer increase() {
RayObject<Integer> result = Ray.call(Adder::increase, adder);
Assert.assertEquals(13, (int) result.get());
return (++sum);
}
public Integer testActorList() {
RayActor<Adder> temp = adderList.get(0);
RayObject<Integer> result = Ray.call(Adder::increase, temp);
return result.get();
}
public Integer add(Integer n) {
return (sum += n);
}
public RayActor<Adder> getAdder() {
return adder;
}
public Integer setAdder(RayActor<Adder> adder) {
this.adder = adder;
return 0;
}
public UniqueID getId() {
return id;
}
public Integer setId(UniqueID id) {
this.id = id;
adder = new RayActor<>(id);
return 0;
}
public Integer setAdderList(List<RayActor<Adder>> adderList) {
this.adderList = adderList;
return 0;
}
@Test
public void testPassActorAsParameter() {
RayActor<Counter> actor = Ray.createActor(Counter.class);
RayFunc2<RayActor, Integer, Integer> f = ActorTest::testActorAsFirstParameter;
Assert.assertEquals(Integer.valueOf(1),
Ray.call(ActorTest::testActorAsFirstParameter, actor, 1).get());
Assert.assertEquals(Integer.valueOf(11),
Ray.call(ActorTest::testActorAsSecondParameter, 10, actor).get());
}
}
@@ -3,7 +3,7 @@ package org.ray.api.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
@RunWith(MyRunner.class)
public class EchoTest {
@@ -5,7 +5,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
/**
* Hello world.
@@ -8,9 +8,9 @@ import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
import org.ray.api.funcs.RayFunc0;
import org.ray.api.funcs.RayFunc1;
import org.ray.api.funcs.RayFunc3;
import org.ray.api.function.RayFunc0;
import org.ray.api.function.RayFunc1;
import org.ray.api.function.RayFunc3;
import org.ray.util.MethodId;
import org.ray.util.logger.RayLog;
@@ -4,7 +4,7 @@ package org.ray.api.test;
import java.lang.reflect.Method;
import org.junit.Assert;
import org.junit.Test;
import org.ray.api.funcs.RayFunc3;
import org.ray.api.function.RayFunc3;
import org.ray.util.MethodId;
import org.ray.util.logger.RayLog;
@@ -0,0 +1,32 @@
package org.ray.api.test;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayObject;
import org.ray.api.id.UniqueId;
/**
* Test putting and getting objects.
*/
@RunWith(MyRunner.class)
public class ObjectStoreTest {
@Test
public void testPutAndGet() {
RayObject<Integer> obj = Ray.put(1);
Assert.assertEquals(1, (int) obj.get());
}
@Test
public void testGetMultipleObjects() {
List<Integer> ints = ImmutableList.of(1, 2, 3, 4, 5);
List<UniqueId> ids = ints.stream().map(obj -> Ray.put(obj).getId())
.collect(Collectors.toList());
Assert.assertEquals(ints, Ray.get(ids));
}
}
@@ -2,24 +2,28 @@ package org.ray.api.test;
import org.junit.Assert;
import org.junit.Test;
import org.ray.api.annotation.RayRemote;
import org.ray.spi.model.RayActorMethods;
import org.ray.util.logger.RayLog;
public class RayActorMethodsTest {
@Test
public void testActor() throws Exception {
RayActorMethods methods = RayActorMethods
.fromClass(ActorTest.Adder.class.getName(), RayActorMethodsTest.class.getClassLoader());
RayLog.core.info(methods.toString());
Assert.assertEquals(methods.functions.size(), 5);
Assert.assertEquals(methods.staticFunctions.size(), 1);
@RayRemote
public static class ExampleActor {
RayActorMethods methods2 = RayActorMethods
.fromClass(ActorTest.Adder2.class.getName(), RayActorMethodsTest.class.getClassLoader());
RayLog.core.info(methods2.toString());
public void func1() {}
Assert.assertEquals(methods2.functions.size(), 9);
Assert.assertEquals(methods2.staticFunctions.size(), 1);
public void func2() {}
public static void func3() {}
}
}
@Test
public void testActorMethods() {
RayActorMethods methods = RayActorMethods
.fromClass(ExampleActor.class.getName(), RayActorMethodsTest.class.getClassLoader());
RayLog.core.info(methods.toString());
Assert.assertEquals(methods.functions.size(), 2);
Assert.assertEquals(methods.staticFunctions.size(), 1);
}
}
@@ -0,0 +1,133 @@
package org.ray.api.test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.annotation.RayRemote;
/**
* Test Ray.call API
*/
@RunWith(MyRunner.class)
public class RayCallTest {
@RayRemote
private static int testInt(int val) {
return val;
}
@RayRemote
private static byte testByte(byte val) {
return val;
}
@RayRemote
private static short testShort(short val) {
return val;
}
@RayRemote
private static long testLong(long val) {
return val;
}
@RayRemote
private static double testDouble(double val) {
return val;
}
@RayRemote
private static float testFloat(float val) {
return val;
}
@RayRemote
private static boolean testBool(boolean val) {
return val;
}
@RayRemote
private static String testString(String val) {
return val;
}
@RayRemote
private static List<Integer> testList(List<Integer> val) {
return val;
}
@RayRemote
private static Map<String, Integer> testMap(Map<String, Integer> val) {
return val;
}
/**
* Test calling and returning different types.
*/
@Test
public void testType() {
Assert.assertEquals(1, (int) Ray.call(RayCallTest::testInt, 1).get());
Assert.assertEquals(1, (byte) Ray.call(RayCallTest::testByte, (byte) 1).get());
Assert.assertEquals(1, (short) Ray.call(RayCallTest::testShort, (short) 1).get());
Assert.assertEquals(1, (long) Ray.call(RayCallTest::testLong, 1L).get());
Assert.assertEquals(1.0, Ray.call(RayCallTest::testDouble, 1.0).get(), 0.0);
Assert.assertEquals(1.0f, Ray.call(RayCallTest::testFloat, 1.0f).get(), 0.0);
Assert.assertEquals(true, Ray.call(RayCallTest::testBool, true).get());
Assert.assertEquals("foo", Ray.call(RayCallTest::testString, "foo").get());
List<Integer> list = ImmutableList.of(1, 2, 3);
Assert.assertEquals(list, Ray.call(RayCallTest::testList, list).get());
Map<String, Integer> map = ImmutableMap.of("1", 1, "2", 2);
Assert.assertEquals(map, Ray.call(RayCallTest::testMap, map).get());
}
@RayRemote
private static int testNoParam() {
return 0;
}
@RayRemote
private static int testOneParam(int a) {
return a;
}
@RayRemote
private static int testTwoParams(int a, int b) {
return a + b;
}
@RayRemote
private static int testThreeParams(int a, int b, int c) {
return a + b + c;
}
@RayRemote
private static int testFourParams(int a, int b, int c, int d) {
return a + b + c + d;
}
@RayRemote
private static int testFiveParams(int a, int b, int c, int d, int e) {
return a + b + c + d + e;
}
@RayRemote
private static int testSixParams(int a, int b, int c, int d, int e, int f) {
return a + b + c + d + e + f;
}
@Test
public void testNumberOfParameters() {
Assert.assertEquals(0, (int) Ray.call(RayCallTest::testNoParam).get());
Assert.assertEquals(1, (int) Ray.call(RayCallTest::testOneParam, 1).get());
Assert.assertEquals(2, (int) Ray.call(RayCallTest::testTwoParams, 1, 1).get());
Assert.assertEquals(3, (int) Ray.call(RayCallTest::testThreeParams, 1, 1, 1).get());
Assert.assertEquals(4, (int) Ray.call(RayCallTest::testFourParams, 1, 1, 1, 1).get());
Assert.assertEquals(5, (int) Ray.call(RayCallTest::testFiveParams, 1, 1, 1, 1, 1).get());
Assert.assertEquals(6, (int) Ray.call(RayCallTest::testSixParams, 1, 1, 1, 1, 1, 1).get());
}
}
@@ -26,9 +26,9 @@ public class RayMethodsTest {
RayObject<String> s2Id = Ray.put(String.valueOf("World!"));
RayObject<Object> n1Id = Ray.put(null);
WaitResult<String> res = Ray.wait(ImmutableList.of(s1Id, s2Id), 2);
WaitResult<String> res = Ray.wait(ImmutableList.of(s1Id, s2Id), 2, 1000);
List<String> ss = res.getReadyOnes().stream().map(RayObject::get).collect(Collectors.toList());
List<String> ss = res.getReady().stream().map(RayObject::get).collect(Collectors.toList());
int i1 = i1Id.get();
double f1 = f1Id.get();
Object n1 = n1Id.get();
@@ -40,12 +40,5 @@ public class RayMethodsTest {
Assert.assertEquals(3.14, f1, Double.MIN_NORMAL);
Assert.assertNull(n1);
// metadata test
RayObject<Integer> vid = Ray.put(643, "test metadata");
Integer v = vid.get();
String m = vid.getMeta();
Assert.assertEquals(643L, v.longValue());
Assert.assertEquals("test metadata", m);
}
}
@@ -1,41 +0,0 @@
package org.ray.api.test;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.util.RemoteFunction;
@RunWith(MyRunner.class)
public class RemoteLambdaTest {
public static <T> String remoteToString(T o) {
return o.toString();
}
@Test
public void test() {
RemoteFunction<String, String> f0 = RemoteLambdaTest::remoteToString;
byte[] bytes = SerializationUtils.serialize(f0);
//System.out.println(new String(bytes));
//Object m = SerializationUtils.deserialize(bytes);
RemoteFunction<Integer, Integer> f = x -> {
System.out.println("remote function " + x);
return x + 1;
};
RemoteFunction<Integer, Integer> f2 = SerializationUtils.clone(f);
Assert.assertEquals(101, (int) f2.apply(100));
Integer y = 100;
RemoteFunction<Integer, Integer> f3 = x -> {
System.out.println("remote function " + x);
return x + y + 1;
};
RemoteFunction<Integer, Integer> f4 = SerializationUtils.clone(f3);
Assert.assertEquals(201, (int) f4.apply(100));
Assert.assertEquals(201, (int) f4.apply(100));
}
}
@@ -8,10 +8,10 @@ import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.WaitResult;
import org.ray.core.RayRuntime;
import org.ray.util.ResourceItem;
import org.ray.api.annotation.RayRemote;
import org.ray.api.annotation.ResourceItem;
import org.ray.core.AbstractRayRuntime;
/**
* Resources Management Test.
@@ -49,7 +49,7 @@ public class ResourcesManagementTest {
@Test
public void testMethods() {
Assume.assumeTrue(RayRuntime.getParams().use_raylet);
Assume.assumeTrue(AbstractRayRuntime.getParams().use_raylet);
// This is a case that can satisfy required resources.
RayObject<Integer> result1 = Ray.call(ResourcesManagementTest::echo1, 100);
Assert.assertEquals(100, (int) result1.get());
@@ -58,25 +58,25 @@ public class ResourcesManagementTest {
final RayObject<Integer> result2 = Ray.call(ResourcesManagementTest::echo2, 200);
WaitResult<Integer> waitResult = Ray.wait(ImmutableList.of(result2), 1, 1000);
Assert.assertEquals(0, waitResult.getReadyOnes().size());
Assert.assertEquals(1, waitResult.getRemainOnes().size());
Assert.assertEquals(0, waitResult.getReady().size());
Assert.assertEquals(1, waitResult.getUnready().size());
}
@Test
public void testActors() {
Assume.assumeTrue(RayRuntime.getParams().use_raylet);
Assume.assumeTrue(AbstractRayRuntime.getParams().use_raylet);
// This is a case that can satisfy required resources.
RayActor<ResourcesManagementTest.Echo1> echo1 = Ray.create(Echo1.class);
RayActor<ResourcesManagementTest.Echo1> echo1 = Ray.createActor(Echo1.class);
final RayObject<Integer> result1 = Ray.call(Echo1::echo, echo1, 100);
Assert.assertEquals(100, (int) result1.get());
// This is a case that can't satisfy required resources.
RayActor<ResourcesManagementTest.Echo2> echo2 = Ray.create(Echo2.class);
RayActor<ResourcesManagementTest.Echo2> echo2 = Ray.createActor(Echo2.class);
final RayObject<Integer> result2 = Ray.call(Echo2::echo, echo2, 100);
WaitResult<Integer> waitResult = Ray.wait(ImmutableList.of(result2), 1, 1000);
Assert.assertEquals(0, waitResult.getReadyOnes().size());
Assert.assertEquals(1, waitResult.getRemainOnes().size());
Assert.assertEquals(0, waitResult.getReady().size());
Assert.assertEquals(1, waitResult.getUnready().size());
}
}
@@ -1,39 +0,0 @@
package org.ray.api.test;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayRemote;
@RunWith(MyRunner.class)
public class RpcTest {
@RayRemote
public static Integer with0Params() {
return 0;
}
@RayRemote
public static Integer with1Params(Integer x) {
return x;
}
@RayRemote
public static Integer with2Params(Integer x, Integer y) {
return x + y;
}
@RayRemote
public static Integer with3Params(Integer x, Integer y, Integer z) {
return x + y + z;
}
@Test
public void test() {
Assert.assertEquals(0, (int) Ray.call(RpcTest::with0Params).get());
Assert.assertEquals(1, (int) Ray.call(RpcTest::with1Params, 1).get());
Assert.assertEquals(3, (int) Ray.call(RpcTest::with2Params, 1, 2).get());
Assert.assertEquals(6, (int) Ray.call(RpcTest::with3Params, 1, 2, 3).get());
}
}
@@ -4,7 +4,6 @@ import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
import org.ray.api.Ray;
import org.ray.core.RayRuntime;
public class TestListener extends RunListener {
@@ -15,6 +14,6 @@ public class TestListener extends RunListener {
@Override
public void testRunFinished(Result result) {
RayRuntime.getInstance().cleanUp();
Ray.shutdown();
}
}
}
@@ -1,82 +0,0 @@
package org.ray.api.test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayRemote;
/**
* Test returning different types.
*/
@RunWith(MyRunner.class)
public class TypesTest {
@RayRemote
private static int testInt() {
return 1;
}
@RayRemote
private static byte testByte() {
return 1;
}
@RayRemote
private static short testShort() {
return 1;
}
@RayRemote
private static long testLong() {
return 1;
}
@RayRemote
private static double testDouble() {
return 1;
}
@RayRemote
private static float testFloat() {
return 1;
}
@RayRemote
private static boolean testBool() {
return true;
}
@RayRemote
private static String testString() {
return "foo";
}
@RayRemote
private static List<Integer> testList() {
return ImmutableList.of(1, 2, 3);
}
@RayRemote
private static Map<String, Integer> testMap() {
return ImmutableMap.of("1", 1, "2", 2);
}
@Test
public void test() {
Assert.assertEquals(1, (int) Ray.call(TypesTest::testInt).get());
Assert.assertEquals(1, (byte) Ray.call(TypesTest::testByte).get());
Assert.assertEquals(1, (short) Ray.call(TypesTest::testShort).get());
Assert.assertEquals(1, (long) Ray.call(TypesTest::testLong).get());
Assert.assertEquals(1.0, Ray.call(TypesTest::testDouble).get(), 0.0);
Assert.assertEquals(1.0f, Ray.call(TypesTest::testFloat).get(), 0.0);
Assert.assertEquals(true, Ray.call(TypesTest::testBool).get());
Assert.assertEquals("foo", Ray.call(TypesTest::testString).get());
Assert.assertEquals(ImmutableList.of(1, 2, 3), Ray.call(TypesTest::testList).get());
Assert.assertEquals(ImmutableMap.of("1", 1, "2", 2), Ray.call(TypesTest::testMap).get());
}
}
@@ -6,7 +6,7 @@ import javax.xml.bind.DatatypeConverter;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.UniqueID;
import org.ray.api.id.UniqueId;
import org.ray.core.UniqueIdHelper;
@RunWith(MyRunner.class)
@@ -15,12 +15,12 @@ public class UniqueIdTest {
@Test
public void testConstructUniqueId() {
// Test `fromHexString()`
UniqueID id1 = UniqueID.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
UniqueId id1 = UniqueId.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
Assert.assertEquals("00000000123456789ABCDEF123456789ABCDEF00", id1.toString());
Assert.assertFalse(id1.isNil());
try {
UniqueID id2 = UniqueID.fromHexString("000000123456789ABCDEF123456789ABCDEF00");
UniqueId id2 = UniqueId.fromHexString("000000123456789ABCDEF123456789ABCDEF00");
// This shouldn't be happened.
Assert.assertTrue(false);
} catch (IllegalArgumentException e) {
@@ -28,7 +28,7 @@ public class UniqueIdTest {
}
try {
UniqueID id3 = UniqueID.fromHexString("GGGGGGGGGGGGG");
UniqueId id3 = UniqueId.fromHexString("GGGGGGGGGGGGG");
// This shouldn't be happened.
Assert.assertTrue(false);
} catch (IllegalArgumentException e) {
@@ -38,13 +38,13 @@ public class UniqueIdTest {
// Test `fromByteBuffer()`
byte[] bytes = DatatypeConverter.parseHexBinary("0123456789ABCDEF0123456789ABCDEF01234567");
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 0, 20);
UniqueID id4 = UniqueID.fromByteBuffer(byteBuffer);
UniqueId id4 = UniqueId.fromByteBuffer(byteBuffer);
Assert.assertTrue(Arrays.equals(bytes, id4.getBytes()));
Assert.assertEquals("0123456789ABCDEF0123456789ABCDEF01234567", id4.toString());
// Test `genNil()`
UniqueID id6 = UniqueID.genNil();
UniqueId id6 = UniqueId.genNil();
Assert.assertEquals("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", id6.toString());
Assert.assertTrue(id6.isNil());
}
@@ -52,9 +52,9 @@ public class UniqueIdTest {
@Test
public void testComputeReturnId() {
// Mock a taskId, and the lowest 4 bytes should be 0.
UniqueID taskId = UniqueID.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
UniqueId taskId = UniqueId.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
UniqueID returnId = UniqueIdHelper.computeReturnId(taskId, 1);
UniqueId returnId = UniqueIdHelper.computeReturnId(taskId, 1);
Assert.assertEquals("01000000123456789ABCDEF123456789ABCDEF00", returnId.toString());
returnId = UniqueIdHelper.computeReturnId(taskId, 0x01020304);
@@ -63,8 +63,8 @@ public class UniqueIdTest {
@Test
public void testComputeTaskId() {
UniqueID objId = UniqueID.fromHexString("34421980123456789ABCDEF123456789ABCDEF00");
UniqueID taskId = UniqueIdHelper.computeTaskId(objId);
UniqueId objId = UniqueId.fromHexString("34421980123456789ABCDEF123456789ABCDEF00");
UniqueId taskId = UniqueIdHelper.computeTaskId(objId);
Assert.assertEquals("00000000123456789ABCDEF123456789ABCDEF00", taskId.toString());
}
@@ -72,9 +72,9 @@ public class UniqueIdTest {
@Test
public void testComputePutId() {
// Mock a taskId, the lowest 4 bytes should be 0.
UniqueID taskId = UniqueID.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
UniqueId taskId = UniqueId.fromHexString("00000000123456789ABCDEF123456789ABCDEF00");
UniqueID putId = UniqueIdHelper.computePutId(taskId, 1);
UniqueId putId = UniqueIdHelper.computePutId(taskId, 1);
Assert.assertEquals("FFFFFFFF123456789ABCDEF123456789ABCDEF00", putId.toString());
putId = UniqueIdHelper.computePutId(taskId, 0x01020304);
@@ -7,45 +7,40 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.ray.api.Ray;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.WaitResult;
import org.ray.api.annotation.RayRemote;
@RunWith(MyRunner.class)
public class WaitTest {
@RayRemote
public static String hi() {
private static String hi() {
return "hi";
}
@RayRemote
public static String delayHi() {
private static String delayedHi() {
try {
Thread.sleep(100 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
return "hi";
}
@Test
public void test() {
RayObject<String> obj1 = Ray.call(WaitTest::hi);
RayObject<String> obj2 = Ray.call(WaitTest::delayHi);
RayObject<String> obj2 = Ray.call(WaitTest::delayedHi);
List<RayObject<String>> waitfor = ImmutableList.of(obj1, obj2);
WaitResult<String> waitResult = Ray.wait(waitfor, 2, 2 * 1000);
List<RayObject<String>> readys = waitResult.getReadyOnes();
List<RayObject<String>> waitList = ImmutableList.of(obj1, obj2);
WaitResult<String> waitResult = Ray.wait(waitList, 2, 2 * 1000);
if (!readys.isEmpty()) {
Assert.assertEquals(1, waitResult.getReadyOnes().size());
Assert.assertEquals(1, waitResult.getRemainOnes().size());
Assert.assertEquals("hi", readys.get(0).get());
} else {
Assert.assertEquals(0, waitResult.getReadyOnes().size());
Assert.assertEquals(2, waitResult.getRemainOnes().size());
}
List<RayObject<String>> readyList = waitResult.getReady();
Assert.assertEquals(1, waitResult.getReady().size());
Assert.assertEquals(1, waitResult.getUnready().size());
Assert.assertEquals("hi", readyList.get(0).get());
}
}
@@ -6,7 +6,7 @@ import java.util.List;
import org.junit.Assert;
import org.ray.api.Ray;
import org.ray.api.RayObject;
import org.ray.api.RayRemote;
import org.ray.api.annotation.RayRemote;
import org.ray.util.FileUtil;
/**