/
TestUtils.java
65 lines (57 loc) · 1.85 KB
/
TestUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.ray.api;
import java.util.function.Supplier;
import org.ray.api.annotation.RayRemote;
import org.ray.runtime.AbstractRayRuntime;
import org.ray.runtime.config.RunMode;
import org.testng.Assert;
import org.testng.SkipException;
public class TestUtils {
private static final int WAIT_INTERVAL_MS = 5;
public static void skipTestUnderSingleProcess() {
AbstractRayRuntime runtime = (AbstractRayRuntime)Ray.internal();
if (runtime.getRayConfig().runMode == RunMode.SINGLE_PROCESS) {
throw new SkipException("This test doesn't work under single-process mode.");
}
}
/**
* Wait until the given condition is met.
*
* @param condition A function that predicts the condition.
* @param timeoutMs Timeout in milliseconds.
* @return True if the condition is met within the timeout, false otherwise.
*/
public static boolean waitForCondition(Supplier<Boolean> condition, int timeoutMs) {
int waitTime = 0;
while (true) {
if (condition.get()) {
return true;
}
try {
java.util.concurrent.TimeUnit.MILLISECONDS.sleep(WAIT_INTERVAL_MS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
waitTime += WAIT_INTERVAL_MS;
if (waitTime > timeoutMs) {
break;
}
}
return false;
}
@RayRemote
private static String hi() {
return "hi";
}
/**
* Warm up the cluster to make sure there's at least one idle worker.
*
* This is needed before calling `wait`. Because, in Travis CI, starting a new worker
* process could be slower than the wait timeout.
* TODO(hchen): We should consider supporting always reversing a certain number of
* idle workers in Raylet's worker pool.
*/
public static void warmUpCluster() {
RayObject<String> obj = Ray.call(TestUtils::hi);
Assert.assertEquals(obj.get(), "hi");
}
}