[Java]Fix testSetResource bug (#11064)

Co-authored-by: 灵洵 <fengbin.ffb@antfin.com>
This commit is contained in:
fangfengbin
2020-09-28 17:53:08 +08:00
committed by GitHub
parent 25ac8f9aa5
commit dd97f0c30d
@@ -20,19 +20,23 @@ public class DynamicResourceTest extends BaseTest {
// Call a task in advance to warm up the cluster to avoid being too slow to start workers.
TestUtils.warmUpCluster();
String resourceName = "A";
ObjectRef<String> obj = Ray.task(DynamicResourceTest::sayHi)
.setResource("A", 10.0)
.setResource(resourceName, 10.0)
.remote();
WaitResult<String> result = Ray.wait(ImmutableList.of(obj), 1, 1000);
Assert.assertEquals(result.getReady().size(), 0);
Ray.setResource("A", 10.0);
Ray.setResource(resourceName, 10.0);
boolean resourceReady = TestUtils.waitForCondition(() -> {
List<NodeInfo> nodes = Ray.getRuntimeContext().getAllNodeInfo();
if (nodes.size() != 1) {
// NOTE: GCS updates node resources asynchronously.
// If we directly get the value of "A" for comparison without check whether "A" exists or not,
// it maybe lead to NPE.
if (nodes.size() != 1 || !nodes.get(0).resources.containsKey(resourceName)) {
return false;
}
return (0 == Double.compare(10.0, nodes.get(0).resources.get("A")));
return (0 == Double.compare(10.0, nodes.get(0).resources.get(resourceName)));
}, 2000);
Assert.assertTrue(resourceReady);