2323import static org .openqa .selenium .grid .data .Availability .DOWN ;
2424import static org .openqa .selenium .grid .data .Availability .UP ;
2525import static org .openqa .selenium .json .Json .MAP_TYPE ;
26+ import static org .openqa .selenium .remote .http .Contents .reader ;
2627import static org .openqa .selenium .remote .http .HttpMethod .GET ;
2728
2829import org .junit .Before ;
4950import org .openqa .selenium .grid .testing .TestSessionFactory ;
5051import org .openqa .selenium .grid .web .CombinedHandler ;
5152import org .openqa .selenium .grid .web .Values ;
53+ import org .openqa .selenium .json .Json ;
54+ import org .openqa .selenium .json .JsonInput ;
5255import org .openqa .selenium .remote .http .HttpClient ;
5356import org .openqa .selenium .remote .http .HttpRequest ;
5457import org .openqa .selenium .remote .http .HttpResponse ;
5558import org .openqa .selenium .remote .tracing .DefaultTestTracer ;
5659import org .openqa .selenium .remote .tracing .Tracer ;
5760import org .openqa .selenium .support .ui .FluentWait ;
5861
62+ import java .io .IOException ;
63+ import java .io .Reader ;
5964import java .net .URI ;
6065import java .net .URISyntaxException ;
6166import java .time .Duration ;
@@ -73,6 +78,8 @@ public class RouterTest {
7378 private Distributor distributor ;
7479 private Router router ;
7580 private Secret registrationSecret ;
81+ private HttpClient .Factory clientFactory ;
82+ private Json JSON = new Json ();
7683
7784 private static void waitUntilReady (Router router , Duration duration ) {
7885 new FluentWait <>(router )
@@ -85,13 +92,23 @@ private static void waitUntilReady(Router router, Duration duration) {
8592 });
8693 }
8794
95+ private static void waitUntilNotReady (Router router , Duration duration ) {
96+ new FluentWait <>(router )
97+ .withTimeout (duration )
98+ .pollingEvery (Duration .ofMillis (100 ))
99+ .until (r -> {
100+ HttpResponse response = r .execute (new HttpRequest (GET , "/status" ));
101+ return response .getStatus ()!=200 ;
102+ });
103+ }
104+
88105 @ Before
89106 public void setUp () {
90107 tracer = DefaultTestTracer .createTracer ();
91108 bus = new GuavaEventBus ();
92109
93110 handler = new CombinedHandler ();
94- HttpClient . Factory clientFactory = new PassthroughHttpClient .Factory (handler );
111+ clientFactory = new PassthroughHttpClient .Factory (handler );
95112
96113 sessions = new LocalSessionMap (tracer , bus );
97114 handler .addHandler (sessions );
@@ -124,7 +141,9 @@ public void setUp() {
124141 }
125142
126143 @ Test
127- public void shouldListAnEmptyDistributorAsMeaningTheGridIsNotReady () {
144+ public void shouldListAnEmptyDistributorAsMeaningTheGridIsNotReady () throws IOException {
145+ HttpResponse response = getStatusHttpResponse (router );
146+ assertNotNull (response );
128147 Map <String , Object > status = getStatus (router );
129148 assertFalse ((Boolean ) status .get ("ready" ));
130149 }
@@ -137,21 +156,44 @@ public void shouldReturnServerErrorCodeWhenGridIsNotReady() {
137156 }
138157
139158 @ Test
140- public void addingANodeThatIsDownMeansTheGridIsNotReady () throws URISyntaxException {
159+ public void addingANodeThatIsDownMeansTheGridIsNotReady ()
160+ throws URISyntaxException , IOException {
161+ distributor = new LocalDistributor (
162+ tracer ,
163+ bus ,
164+ clientFactory ,
165+ sessions ,
166+ queue ,
167+ new DefaultSlotSelector (),
168+ registrationSecret ,
169+ Duration .ofSeconds (1 ),
170+ false ,
171+ Duration .ofSeconds (5 ));
172+ handler .addHandler (distributor );
173+
174+ router = new Router (tracer , clientFactory , sessions , queue , distributor );
175+
141176 Capabilities capabilities = new ImmutableCapabilities ("cheese" , "peas" );
142177 URI uri = new URI ("http://exmaple.com" );
143178
144- AtomicReference <Availability > isUp = new AtomicReference <>(DOWN );
179+ AtomicReference <Availability > isUp = new AtomicReference <>(UP );
145180
146181 Node node = LocalNode .builder (tracer , bus , uri , uri , registrationSecret )
147- .add (capabilities , new TestSessionFactory ((id , caps ) -> new Session (id , uri , new ImmutableCapabilities (), caps , Instant .now ())))
182+ .add (capabilities , new TestSessionFactory (
183+ (id , caps ) -> new Session (id , uri , new ImmutableCapabilities (), caps , Instant .now ())))
148184 .advanced ()
149185 .healthCheck (() -> new HealthCheck .Result (isUp .get (), "TL;DR" ))
150186 .build ();
151187 distributor .add (node );
152188
189+ waitUntilReady (router , Duration .ofSeconds (5 ));
190+ isUp .set (DOWN );
191+ waitUntilNotReady (router , Duration .ofSeconds (5 ));
192+
193+ HttpResponse response = getStatusHttpResponse (router );
194+ assertNotNull (response );
153195 Map <String , Object > status = getStatus (router );
154- assertFalse (status . toString (), (Boolean ) status .get ("ready" ));
196+ assertFalse ((Boolean ) status .get ("ready" ));
155197 }
156198
157199 @ Test
@@ -181,9 +223,21 @@ public void ifNodesHaveSpareSlotsButAlreadyHaveMaxSessionsGridIsNotReady() {
181223
182224 }
183225
184- private Map <String , Object > getStatus (Router router ) {
226+ private Map <String , Object > getStatus (Router router ) throws IOException {
185227 HttpResponse response = router .execute (new HttpRequest (GET , "/status" ));
186- Map <String , Object > status = Values .get (response , MAP_TYPE );
228+ Map <String , Object > status = null ;
229+ try (Reader reader = reader (response );
230+ JsonInput input = JSON .newInput (reader )) {
231+ input .beginObject ();
232+
233+ while (input .hasNext ()) {
234+ if ("value" .equals (input .nextName ())) {
235+ status = input .read (MAP_TYPE );
236+ } else {
237+ input .skipValue ();
238+ }
239+ }
240+ }
187241 assertNotNull (status );
188242 return status ;
189243 }
0 commit comments