Skip to content

Commit 215c0fd

Browse files
authored
[java] Close resources in tests (#11557)
1 parent 74a85d8 commit 215c0fd

File tree

9 files changed

+322
-295
lines changed

9 files changed

+322
-295
lines changed

java/src/org/openqa/selenium/grid/distributor/local/LocalDistributor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122

123123
@ManagedService(objectName = "org.seleniumhq.grid:type=Distributor,name=LocalDistributor",
124124
description = "Grid 4 node distributor")
125-
public class LocalDistributor extends Distributor implements Closeable {
125+
public class LocalDistributor extends Distributor implements AutoCloseable {
126126

127127
private static final Logger LOG = Logger.getLogger(LocalDistributor.class.getName());
128128

java/test/org/openqa/selenium/events/ZeroMqEventBusTest.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,37 @@
3333
class ZeroMqEventBusTest {
3434

3535
@Test
36-
void shouldEnsureMessagesRequireSecret() throws InterruptedException, ExecutionException, TimeoutException {
36+
void shouldEnsureMessagesRequireSecret()
37+
throws InterruptedException, ExecutionException, TimeoutException {
3738
String publish = "inproc://zmqebt-publish";
3839
String subscribe = "inproc://zmqebt-subscribe";
3940

4041
ZContext context = new ZContext();
41-
EventBus good = ZeroMqEventBus.create(context, publish, subscribe, true, new Secret("cheese"));
42-
EventBus alsoGood = ZeroMqEventBus.create(context, publish, subscribe, false, new Secret("cheese"));
43-
EventBus bad = ZeroMqEventBus.create(context, publish, subscribe, false, new Secret("peas"));
42+
try (
43+
EventBus good = ZeroMqEventBus.create(context, publish, subscribe, true, new Secret("cheese"));
44+
EventBus alsoGood = ZeroMqEventBus.create(context, publish, subscribe, false, new Secret("cheese"));
45+
EventBus bad = ZeroMqEventBus.create(context, publish, subscribe, false, new Secret("peas"))) {
4446

45-
RuntimeException errorException = new RuntimeException("oh noes!");
46-
EventName eventName = new EventName("evt");
47-
CompletableFuture<String> future = new CompletableFuture<>();
48-
good.addListener(new EventListener<>(eventName, String.class, future::complete));
49-
good.addListener(ZeroMqEventBus.onRejectedEvent(evt -> future.completeExceptionally(errorException)));
47+
RuntimeException errorException = new RuntimeException("oh noes!");
48+
EventName eventName = new EventName("evt");
49+
CompletableFuture<String> future = new CompletableFuture<>();
50+
good.addListener(new EventListener<>(eventName, String.class, future::complete));
51+
good.addListener(
52+
ZeroMqEventBus.onRejectedEvent(evt -> future.completeExceptionally(errorException)));
5053

51-
alsoGood.fire(new Event(eventName, "tasty"));
54+
alsoGood.fire(new Event(eventName, "tasty"));
5255

53-
String value = future.get(5, SECONDS);
54-
assertThat(value).isEqualTo("tasty");
56+
String value = future.get(5, SECONDS);
57+
assertThat(value).isEqualTo("tasty");
5558

56-
CompletableFuture<String> badFuture = new CompletableFuture<>();
57-
good.addListener(new EventListener<>(eventName, String.class, badFuture::complete));
58-
good.addListener(ZeroMqEventBus.onRejectedEvent(evt -> badFuture.completeExceptionally(errorException)));
59-
bad.fire(new Event(eventName, "not tasty"));
59+
CompletableFuture<String> badFuture = new CompletableFuture<>();
60+
good.addListener(new EventListener<>(eventName, String.class, badFuture::complete));
61+
good.addListener(
62+
ZeroMqEventBus.onRejectedEvent(evt -> badFuture.completeExceptionally(errorException)));
63+
bad.fire(new Event(eventName, "not tasty"));
6064

61-
Assertions.assertThatThrownBy(() -> badFuture.get(5, SECONDS)).getCause().isSameAs(errorException);
65+
Assertions.assertThatThrownBy(() -> badFuture.get(5, SECONDS)).getCause()
66+
.isSameAs(errorException);
67+
}
6268
}
6369
}

java/test/org/openqa/selenium/grid/distributor/AddingNodesTest.java

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void shouldBeAbleToRegisterACustomNode() throws URISyntaxException {
163163
c -> new Session(
164164
new SessionId(UUID.randomUUID()), sessionUri, stereotype, c, Instant.now()));
165165

166-
Distributor local = new LocalDistributor(
166+
try (LocalDistributor local = new LocalDistributor(
167167
tracer,
168168
bus,
169169
new PassthroughHttpClient.Factory(node),
@@ -174,16 +174,19 @@ void shouldBeAbleToRegisterACustomNode() throws URISyntaxException {
174174
Duration.ofMinutes(5),
175175
false,
176176
Duration.ofSeconds(5),
177-
newSessionThreadPoolSize);
177+
newSessionThreadPoolSize)) {
178178

179-
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
179+
distributor =
180+
new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl,
181+
registrationSecret);
180182

181-
distributor.add(node);
183+
distributor.add(node);
182184

183-
wait.until(obj -> distributor.getStatus().hasCapacity());
185+
wait.until(obj -> distributor.getStatus().hasCapacity());
184186

185-
NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());
186-
assertEquals(1, getStereotypes(status).get(CAPS).intValue());
187+
NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());
188+
assertEquals(1, getStereotypes(status).get(CAPS).intValue());
189+
}
187190
}
188191

189192
@Test
@@ -197,7 +200,7 @@ void shouldBeAbleToRegisterNodesByListeningForEvents() throws URISyntaxException
197200
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
198201
.build();
199202

200-
Distributor local = new LocalDistributor(
203+
try (LocalDistributor local = new LocalDistributor(
201204
tracer,
202205
bus,
203206
new PassthroughHttpClient.Factory(node),
@@ -208,16 +211,19 @@ void shouldBeAbleToRegisterNodesByListeningForEvents() throws URISyntaxException
208211
Duration.ofMinutes(5),
209212
false,
210213
Duration.ofSeconds(5),
211-
newSessionThreadPoolSize);
214+
newSessionThreadPoolSize)) {
212215

213-
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
216+
distributor =
217+
new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl,
218+
registrationSecret);
214219

215-
bus.fire(new NodeStatusEvent(node.getStatus()));
220+
bus.fire(new NodeStatusEvent(node.getStatus()));
216221

217-
wait.until(obj -> distributor.getStatus().hasCapacity());
222+
wait.until(obj -> distributor.getStatus().hasCapacity());
218223

219-
NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());
220-
assertEquals(1, getStereotypes(status).get(CAPS).intValue());
224+
NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());
225+
assertEquals(1, getStereotypes(status).get(CAPS).intValue());
226+
}
221227
}
222228

223229
@Test
@@ -241,7 +247,7 @@ void shouldKeepOnlyOneNodeWhenTwoRegistrationsHaveTheSameUriByListeningForEvents
241247
handler.addHandler(firstNode);
242248
handler.addHandler(secondNode);
243249

244-
Distributor local = new LocalDistributor(
250+
try (LocalDistributor local = new LocalDistributor(
245251
tracer,
246252
bus,
247253
new PassthroughHttpClient.Factory(handler),
@@ -252,18 +258,21 @@ void shouldKeepOnlyOneNodeWhenTwoRegistrationsHaveTheSameUriByListeningForEvents
252258
Duration.ofMinutes(5),
253259
false,
254260
Duration.ofSeconds(5),
255-
newSessionThreadPoolSize);
261+
newSessionThreadPoolSize)) {
256262

257-
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
263+
distributor =
264+
new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl,
265+
registrationSecret);
258266

259-
bus.fire(new NodeStatusEvent(firstNode.getStatus()));
260-
bus.fire(new NodeStatusEvent(secondNode.getStatus()));
267+
bus.fire(new NodeStatusEvent(firstNode.getStatus()));
268+
bus.fire(new NodeStatusEvent(secondNode.getStatus()));
261269

262-
wait.until(obj -> distributor.getStatus());
270+
wait.until(obj -> distributor.getStatus());
263271

264-
Set<NodeStatus> nodes = distributor.getStatus().getNodes();
272+
Set<NodeStatus> nodes = distributor.getStatus().getNodes();
265273

266-
assertEquals(1, nodes.size());
274+
assertEquals(1, nodes.size());
275+
}
267276
}
268277

269278
@Test
@@ -278,7 +287,7 @@ void distributorShouldUpdateStateOfExistingNodeWhenNodePublishesStateChange()
278287
(id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))
279288
.build();
280289

281-
Distributor local = new LocalDistributor(
290+
try (LocalDistributor local = new LocalDistributor(
282291
tracer,
283292
bus,
284293
new PassthroughHttpClient.Factory(node),
@@ -289,40 +298,43 @@ void distributorShouldUpdateStateOfExistingNodeWhenNodePublishesStateChange()
289298
Duration.ofMinutes(5),
290299
false,
291300
Duration.ofSeconds(5),
292-
newSessionThreadPoolSize);
301+
newSessionThreadPoolSize)) {
293302

294-
distributor = new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl, registrationSecret);
303+
distributor =
304+
new RemoteDistributor(tracer, new PassthroughHttpClient.Factory(local), externalUrl,
305+
registrationSecret);
295306

296-
bus.fire(new NodeStatusEvent(node.getStatus()));
307+
bus.fire(new NodeStatusEvent(node.getStatus()));
297308

298-
// Start empty
299-
wait.until(obj -> distributor.getStatus().hasCapacity());
309+
// Start empty
310+
wait.until(obj -> distributor.getStatus().hasCapacity());
300311

301-
NodeStatus nodeStatus = getOnlyElement(distributor.getStatus().getNodes());
302-
assertEquals(1, getStereotypes(nodeStatus).get(CAPS).intValue());
303-
304-
// Craft a status that makes it look like the node is busy, and post it on the bus.
305-
NodeStatus status = node.getStatus();
306-
NodeStatus crafted = new NodeStatus(
307-
status.getNodeId(),
308-
status.getExternalUri(),
309-
status.getMaxSessionCount(),
310-
ImmutableSet.of(
311-
new Slot(
312-
new SlotId(status.getNodeId(), UUID.randomUUID()),
313-
CAPS,
314-
Instant.now(),
315-
new Session(
316-
new SessionId(UUID.randomUUID()), sessionUri, CAPS, CAPS, Instant.now()))),
317-
UP,
318-
Duration.ofSeconds(10),
319-
status.getVersion(),
320-
status.getOsInfo());
321-
322-
bus.fire(new NodeStatusEvent(crafted));
323-
324-
// We claimed the only slot is filled. Life is good.
325-
wait.until(obj -> !distributor.getStatus().hasCapacity());
312+
NodeStatus nodeStatus = getOnlyElement(distributor.getStatus().getNodes());
313+
assertEquals(1, getStereotypes(nodeStatus).get(CAPS).intValue());
314+
315+
// Craft a status that makes it look like the node is busy, and post it on the bus.
316+
NodeStatus status = node.getStatus();
317+
NodeStatus crafted = new NodeStatus(
318+
status.getNodeId(),
319+
status.getExternalUri(),
320+
status.getMaxSessionCount(),
321+
ImmutableSet.of(
322+
new Slot(
323+
new SlotId(status.getNodeId(), UUID.randomUUID()),
324+
CAPS,
325+
Instant.now(),
326+
new Session(
327+
new SessionId(UUID.randomUUID()), sessionUri, CAPS, CAPS, Instant.now()))),
328+
UP,
329+
Duration.ofSeconds(10),
330+
status.getVersion(),
331+
status.getOsInfo());
332+
333+
bus.fire(new NodeStatusEvent(crafted));
334+
335+
// We claimed the only slot is filled. Life is good.
336+
wait.until(obj -> !distributor.getStatus().hasCapacity());
337+
}
326338
}
327339

328340
private Map<Capabilities, Integer> getStereotypes(NodeStatus status) {

0 commit comments

Comments
 (0)