Skip to content

Commit c152806

Browse files
ZacBlancoauden-woolfson
authored andcommitted
Add timeouts to presto-router TestHealthChecks
1 parent db01390 commit c152806

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

presto-router/src/test/java/com/facebook/presto/router/TestHealthChecks.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.facebook.airlift.node.testing.TestingNodeModule;
2222
import com.facebook.presto.ClientRequestFilterModule;
2323
import com.facebook.presto.router.cluster.ClusterManager;
24+
import com.facebook.presto.router.cluster.RemoteClusterInfo;
2425
import com.facebook.presto.router.cluster.RequestInfo;
2526
import com.facebook.presto.server.MockHttpServletRequest;
2627
import com.facebook.presto.server.security.ServerSecurityModule;
@@ -38,11 +39,19 @@
3839

3940
import java.io.File;
4041
import java.net.URI;
42+
import java.time.Instant;
4143
import java.util.ArrayList;
4244
import java.util.List;
4345
import java.util.Optional;
46+
import java.util.concurrent.TimeUnit;
47+
import java.util.concurrent.TimeoutException;
48+
import java.util.function.Supplier;
4449

4550
import static com.facebook.presto.router.TestingRouterUtil.getConfigFile;
51+
import static com.google.common.base.Preconditions.checkArgument;
52+
import static java.lang.Thread.sleep;
53+
import static java.time.Instant.now;
54+
import static java.util.concurrent.TimeUnit.MINUTES;
4655
import static org.testng.Assert.assertFalse;
4756
import static org.testng.Assert.assertTrue;
4857

@@ -99,9 +108,28 @@ public void tearDownServer()
99108
}
100109
}
101110

111+
static void waitUntil(Supplier<Boolean> condition, int value, TimeUnit unit)
112+
throws TimeoutException, InterruptedException
113+
{
114+
checkArgument(value > 0, "timeout value must be greater than 0");
115+
Instant start = now();
116+
long timeoutMillis = unit.toMillis(value);
117+
long sleepMillis = Math.min(timeoutMillis / 10, 50);
118+
Instant deadline = start.plusMillis(timeoutMillis);
119+
while (true) {
120+
if (condition.get()) {
121+
return;
122+
}
123+
if (now().isAfter(deadline)) {
124+
throw new TimeoutException();
125+
}
126+
sleep(sleepMillis);
127+
}
128+
}
129+
102130
@Test
103131
public void testHealthChecks()
104-
throws InterruptedException
132+
throws InterruptedException, TimeoutException
105133
{
106134
TestingPrestoServer server0 = prestoServers.get(0);
107135
TestingPrestoServer server1 = prestoServers.get(1);
@@ -113,25 +141,22 @@ public void testHealthChecks()
113141
assertTrue(healthyDestinations.contains(server2.getBaseUrl()));
114142

115143
server0.stopResponding();
116-
while (
117-
clusterManager.getRemoteClusterInfos().get(server0.getBaseUrl()).isHealthy()
118-
|| !clusterManager.getRemoteClusterInfos().get(server1.getBaseUrl()).isHealthy()
119-
|| !clusterManager.getRemoteClusterInfos().get(server2.getBaseUrl()).isHealthy()) {
120-
Thread.sleep(10);
121-
}
144+
waitUntil(() ->
145+
!clusterManager.getRemoteClusterInfos().get(server0.getBaseUrl()).isHealthy()
146+
&& clusterManager.getRemoteClusterInfos().get(server1.getBaseUrl()).isHealthy()
147+
&& clusterManager.getRemoteClusterInfos().get(server2.getBaseUrl()).isHealthy(), 2, MINUTES);
122148

123149
healthyDestinations = getDestinations(3);
124150
assertFalse(healthyDestinations.contains(server0.getBaseUrl()));
125151
assertTrue(healthyDestinations.contains(server1.getBaseUrl()));
126152
assertTrue(healthyDestinations.contains(server2.getBaseUrl()));
127153

128154
server0.startResponding();
129-
while (
130-
!clusterManager.getRemoteClusterInfos().get(server0.getBaseUrl()).isHealthy()
131-
|| !clusterManager.getRemoteClusterInfos().get(server1.getBaseUrl()).isHealthy()
132-
|| !clusterManager.getRemoteClusterInfos().get(server2.getBaseUrl()).isHealthy()) {
133-
Thread.sleep(10);
134-
}
155+
waitUntil(() -> prestoServers.stream()
156+
.map(TestingPrestoServer::getBaseUrl)
157+
.map(clusterManager.getRemoteClusterInfos()::get)
158+
.allMatch(RemoteClusterInfo::isHealthy),
159+
2, MINUTES);
135160

136161
healthyDestinations = getDestinations(3);
137162
assertTrue(healthyDestinations.contains(server0.getBaseUrl()));

0 commit comments

Comments
 (0)