Skip to content

Commit a88f1f1

Browse files
committed
[java] Return appropriate Http response code if Grid is not ready
Fixes #10391
1 parent ab34768 commit a88f1f1

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

java/src/org/openqa/selenium/grid/router/GridStatusHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.concurrent.Executors;
4141
import java.util.concurrent.TimeoutException;
4242

43+
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
4344
import static java.util.concurrent.TimeUnit.SECONDS;
4445
import static java.util.stream.Collectors.toList;
4546
import static org.openqa.selenium.grid.data.Availability.UP;
@@ -145,10 +146,15 @@ public HttpResponse execute(HttpRequest req) {
145146

146147
HttpResponse res = new HttpResponse()
147148
.setContent(asJson(ImmutableMap.of("value", value.build())));
149+
if (ready) {
150+
span.setStatus(Status.OK);
151+
} else {
152+
res.setStatus(HTTP_UNAVAILABLE);
153+
span.setStatus(Status.UNAVAILABLE);
154+
}
148155
HTTP_RESPONSE.accept(span, res);
149156
HTTP_RESPONSE_EVENT.accept(attributeMap, res);
150157
attributeMap.put("grid.status", EventAttribute.setValue(ready));
151-
span.setStatus(Status.OK);
152158
span.addEvent("Computed grid status", attributeMap);
153159
return res;
154160
}

java/test/org/openqa/selenium/grid/router/RouterTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openqa.selenium.grid.router;
1919

20+
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertNotNull;
2223
import static org.openqa.selenium.grid.data.Availability.DOWN;
@@ -128,6 +129,13 @@ public void shouldListAnEmptyDistributorAsMeaningTheGridIsNotReady() {
128129
assertFalse((Boolean) status.get("ready"));
129130
}
130131

132+
@Test
133+
public void shouldReturnServerErrorCodeWhenGridIsNotReady() {
134+
HttpResponse response = getStatusHttpResponse(router);
135+
assertNotNull(response);
136+
assertEquals(503, response.getStatus());
137+
}
138+
131139
@Test
132140
public void addingANodeThatIsDownMeansTheGridIsNotReady() throws URISyntaxException {
133141
Capabilities capabilities = new ImmutableCapabilities("cheese", "peas");
@@ -179,4 +187,8 @@ private Map<String, Object> getStatus(Router router) {
179187
assertNotNull(status);
180188
return status;
181189
}
190+
191+
private HttpResponse getStatusHttpResponse(Router router) {
192+
return router.execute(new HttpRequest(GET, "/status"));
193+
}
182194
}

0 commit comments

Comments
 (0)