21
21
import com .facebook .airlift .node .testing .TestingNodeModule ;
22
22
import com .facebook .presto .ClientRequestFilterModule ;
23
23
import com .facebook .presto .router .cluster .ClusterManager ;
24
+ import com .facebook .presto .router .cluster .RemoteClusterInfo ;
24
25
import com .facebook .presto .router .cluster .RequestInfo ;
25
26
import com .facebook .presto .server .MockHttpServletRequest ;
26
27
import com .facebook .presto .server .security .ServerSecurityModule ;
38
39
39
40
import java .io .File ;
40
41
import java .net .URI ;
42
+ import java .time .Instant ;
41
43
import java .util .ArrayList ;
42
44
import java .util .List ;
43
45
import java .util .Optional ;
46
+ import java .util .concurrent .TimeUnit ;
47
+ import java .util .concurrent .TimeoutException ;
48
+ import java .util .function .Supplier ;
44
49
45
50
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 ;
46
55
import static org .testng .Assert .assertFalse ;
47
56
import static org .testng .Assert .assertTrue ;
48
57
@@ -99,9 +108,28 @@ public void tearDownServer()
99
108
}
100
109
}
101
110
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
+
102
130
@ Test
103
131
public void testHealthChecks ()
104
- throws InterruptedException
132
+ throws InterruptedException , TimeoutException
105
133
{
106
134
TestingPrestoServer server0 = prestoServers .get (0 );
107
135
TestingPrestoServer server1 = prestoServers .get (1 );
@@ -113,25 +141,22 @@ public void testHealthChecks()
113
141
assertTrue (healthyDestinations .contains (server2 .getBaseUrl ()));
114
142
115
143
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 );
122
148
123
149
healthyDestinations = getDestinations (3 );
124
150
assertFalse (healthyDestinations .contains (server0 .getBaseUrl ()));
125
151
assertTrue (healthyDestinations .contains (server1 .getBaseUrl ()));
126
152
assertTrue (healthyDestinations .contains (server2 .getBaseUrl ()));
127
153
128
154
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 );
135
160
136
161
healthyDestinations = getDestinations (3 );
137
162
assertTrue (healthyDestinations .contains (server0 .getBaseUrl ()));
0 commit comments