diff --git a/java/api/src/main/java/io/ray/api/options/BaseTaskOptions.java b/java/api/src/main/java/io/ray/api/options/BaseTaskOptions.java index 6b2bc7aa9..32fb4285e 100644 --- a/java/api/src/main/java/io/ray/api/options/BaseTaskOptions.java +++ b/java/api/src/main/java/io/ray/api/options/BaseTaskOptions.java @@ -16,9 +16,17 @@ public abstract class BaseTaskOptions { public BaseTaskOptions(Map resources) { for (Map.Entry entry : resources.entrySet()) { - if (entry.getValue().compareTo(0.0) <= 0) { - throw new IllegalArgumentException(String.format("Resource capacity should be " + - "positive, but got resource %s = %f.", entry.getKey(), entry.getValue())); + if (entry.getValue() == null || entry.getValue().compareTo(0.0) <= 0) { + throw new IllegalArgumentException(String.format("Resource values should be " + + "positive. Specified resource: %s = %s.", entry.getKey(), entry.getValue())); + } + // Note: A resource value should be an integer if it is greater than 1.0. + // e.g. 3.0 is a valid resource value, but 3.5 is not. + if (entry.getValue().compareTo(1.0) >= 0 + && entry.getValue().compareTo(Math.floor(entry.getValue())) != 0) { + throw new IllegalArgumentException(String.format("A resource value should be " + + "an integer if it is greater than 1.0. Specified resource: %s = %s.", + entry.getKey(), entry.getValue())); } } this.resources = resources; diff --git a/java/test/src/main/java/io/ray/test/BaseTaskOptionsTest.java b/java/test/src/main/java/io/ray/test/BaseTaskOptionsTest.java new file mode 100644 index 000000000..481ffd25f --- /dev/null +++ b/java/test/src/main/java/io/ray/test/BaseTaskOptionsTest.java @@ -0,0 +1,49 @@ +package io.ray.test; + +import com.google.common.collect.ImmutableMap; +import io.ray.api.options.BaseTaskOptions; +import java.util.HashMap; +import java.util.Map; +import org.testng.annotations.Test; + +public class BaseTaskOptionsTest { + + public static class MockActorCreationOptions extends BaseTaskOptions { + public MockActorCreationOptions(Map resources) { + super(resources); + } + } + + @Test + public void testLegalResources() { + Map resources = ImmutableMap.of( + "CPU", 0.5, "GPU", 3.0, "memory", 1024.0, "A", 4294967296.0); + new MockActorCreationOptions(resources); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void testIllegalResourcesWithNullValue() { + Map resources = new HashMap<>(); + resources.put("CPU", null); + new MockActorCreationOptions(resources); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void testIllegalResourcesWithZeroValue() { + Map resources = ImmutableMap.of("CPU", 0.0); + new MockActorCreationOptions(resources); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void testIllegalResourcesWithNegativeValue() { + Map resources = ImmutableMap.of("CPU", -1.0); + new MockActorCreationOptions(resources); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void testIllegalResourcesWithNonIntegerValue() { + Map resources = ImmutableMap.of("CPU", 3.5); + new MockActorCreationOptions(resources); + } + +} \ No newline at end of file