Skip to content

Commit a64e482

Browse files
committed
[grid] Start using better strong typing with events
1 parent 9e80450 commit a64e482

18 files changed

Lines changed: 164 additions & 79 deletions

java/server/src/org/openqa/selenium/events/EventBus.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public interface EventBus extends Closeable, HasReadyState {
2626

2727
void addListener(EventName eventName, Consumer<Event> onType);
2828

29+
default void addListener(EventListener<?> listener) {
30+
addListener(listener.getEventName(), listener);
31+
}
32+
2933
void fire(Event event);
3034

3135
void close();
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.events;
19+
20+
import org.openqa.selenium.internal.Require;
21+
22+
import java.lang.reflect.Type;
23+
import java.util.function.Consumer;
24+
25+
public class EventListener<X> implements Consumer<Event> {
26+
27+
private final EventName name;
28+
private final Consumer<X> handler;
29+
private final Type type;
30+
31+
public EventListener(EventName name, Type typeOfX, Consumer<X> handler) {
32+
this.name = Require.nonNull("Event name", name);
33+
this.type = Require.nonNull("Type", typeOfX);
34+
this.handler = Require.nonNull("Event handler", handler);
35+
}
36+
37+
public EventName getEventName() {
38+
return name;
39+
}
40+
41+
@Override
42+
public void accept(Event event) {
43+
handler.accept(event.getData(type));
44+
}
45+
}

java/server/src/org/openqa/selenium/grid/data/NodeAddedEvent.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@
1818
package org.openqa.selenium.grid.data;
1919

2020
import org.openqa.selenium.events.Event;
21+
import org.openqa.selenium.events.EventListener;
2122
import org.openqa.selenium.events.EventName;
23+
import org.openqa.selenium.internal.Require;
24+
25+
import java.util.function.Consumer;
2226

2327
public class NodeAddedEvent extends Event {
2428

25-
public static final EventName NODE_ADDED = new EventName("node-added");
29+
private static final EventName NODE_ADDED = new EventName("node-added");
2630

2731
public NodeAddedEvent(NodeId nodeId) {
2832
super(NODE_ADDED, nodeId);
2933
}
34+
35+
public static EventListener<NodeId> listener(Consumer<NodeId> handler) {
36+
Require.nonNull("Handler", handler);
37+
38+
return new EventListener<>(NODE_ADDED, NodeId.class, handler);
39+
}
3040
}

java/server/src/org/openqa/selenium/grid/data/NodeDrainComplete.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,22 @@
1818
package org.openqa.selenium.grid.data;
1919

2020
import org.openqa.selenium.events.Event;
21+
import org.openqa.selenium.events.EventListener;
2122
import org.openqa.selenium.events.EventName;
23+
import org.openqa.selenium.internal.Require;
24+
25+
import java.util.function.Consumer;
2226

2327
public class NodeDrainComplete extends Event {
24-
public static final EventName NODE_DRAIN_COMPLETE = new EventName("node-drain-complete");
28+
private static final EventName NODE_DRAIN_COMPLETE = new EventName("node-drain-complete");
2529

2630
public NodeDrainComplete(NodeId id) {
2731
super(NODE_DRAIN_COMPLETE, id);
2832
}
33+
34+
public static EventListener<NodeId> listener(Consumer<NodeId> handler) {
35+
Require.nonNull("Handler", handler);
36+
37+
return new EventListener<>(NODE_DRAIN_COMPLETE, NodeId.class, handler);
38+
}
2939
}

java/server/src/org/openqa/selenium/grid/data/NodeDrainStarted.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@
1818
package org.openqa.selenium.grid.data;
1919

2020
import org.openqa.selenium.events.Event;
21+
import org.openqa.selenium.events.EventListener;
2122
import org.openqa.selenium.events.EventName;
23+
import org.openqa.selenium.internal.Require;
24+
25+
import java.util.function.Consumer;
2226

2327
public class NodeDrainStarted extends Event {
2428

25-
public static final EventName NODE_DRAIN_STARTED = new EventName("node-drain-started");
29+
private static final EventName NODE_DRAIN_STARTED = new EventName("node-drain-started");
2630

2731
public NodeDrainStarted(NodeId id) {
2832
super(NODE_DRAIN_STARTED, id);
2933
}
3034

35+
public static EventListener<NodeId> listener(Consumer<NodeId> handler) {
36+
Require.nonNull("Handler", handler);
37+
38+
return new EventListener<>(NODE_DRAIN_STARTED, NodeId.class, handler);
39+
}
3140
}

java/server/src/org/openqa/selenium/grid/data/NodeRemovedEvent.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@
1818
package org.openqa.selenium.grid.data;
1919

2020
import org.openqa.selenium.events.Event;
21+
import org.openqa.selenium.events.EventListener;
2122
import org.openqa.selenium.events.EventName;
23+
import org.openqa.selenium.internal.Require;
24+
25+
import java.util.function.Consumer;
2226

2327
public class NodeRemovedEvent extends Event {
2428

25-
public static final EventName NODE_REMOVED = new EventName("node-removed");
29+
private static final EventName NODE_REMOVED = new EventName("node-removed");
2630

2731
public NodeRemovedEvent(NodeId nodeId) {
2832
super(NODE_REMOVED, nodeId);
2933
}
34+
35+
public static EventListener<NodeId> listener(Consumer<NodeId> handler) {
36+
Require.nonNull("Handler", handler);
37+
38+
return new EventListener<>(NODE_REMOVED, NodeId.class, handler);
39+
}
3040
}

java/server/src/org/openqa/selenium/grid/data/NodeStatusEvent.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@
1818
package org.openqa.selenium.grid.data;
1919

2020
import org.openqa.selenium.events.Event;
21+
import org.openqa.selenium.events.EventListener;
2122
import org.openqa.selenium.events.EventName;
2223
import org.openqa.selenium.internal.Require;
2324

25+
import java.util.function.Consumer;
26+
2427
public class NodeStatusEvent extends Event {
2528

26-
public static final EventName NODE_STATUS = new EventName("node-status");
29+
private static final EventName NODE_STATUS = new EventName("node-status");
2730

2831
public NodeStatusEvent(NodeStatus status) {
2932
super(NODE_STATUS, Require.nonNull("Node status", status));
3033
}
34+
35+
public static EventListener<NodeStatus> listener(Consumer<NodeStatus> handler) {
36+
Require.nonNull("Handler", handler);
37+
38+
return new EventListener<NodeStatus>(NODE_STATUS, NodeStatus.class, handler);
39+
}
3140
}

java/server/src/org/openqa/selenium/grid/data/SessionClosedEvent.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,24 @@
1818
package org.openqa.selenium.grid.data;
1919

2020
import org.openqa.selenium.events.Event;
21+
import org.openqa.selenium.events.EventListener;
2122
import org.openqa.selenium.events.EventName;
23+
import org.openqa.selenium.internal.Require;
2224
import org.openqa.selenium.remote.SessionId;
2325

26+
import java.util.function.Consumer;
27+
2428
public class SessionClosedEvent extends Event {
2529

26-
public static final EventName SESSION_CLOSED = new EventName("session-closed");
30+
private static final EventName SESSION_CLOSED = new EventName("session-closed");
2731

2832
public SessionClosedEvent(SessionId id) {
2933
super(SESSION_CLOSED, id);
3034
}
35+
36+
public static EventListener<SessionId> listener(Consumer<SessionId> handler) {
37+
Require.nonNull("Handler", handler);
38+
39+
return new EventListener<>(SESSION_CLOSED, SessionId.class, handler);
40+
}
3141
}

java/server/src/org/openqa/selenium/grid/distributor/local/GridModel.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
import com.google.common.collect.ImmutableSet;
2121
import org.openqa.selenium.events.EventBus;
2222
import org.openqa.selenium.grid.data.Availability;
23+
import org.openqa.selenium.grid.data.NodeDrainComplete;
24+
import org.openqa.selenium.grid.data.NodeDrainStarted;
2325
import org.openqa.selenium.grid.data.NodeId;
2426
import org.openqa.selenium.grid.data.NodeRejectedEvent;
27+
import org.openqa.selenium.grid.data.NodeRemovedEvent;
2528
import org.openqa.selenium.grid.data.NodeStatus;
29+
import org.openqa.selenium.grid.data.NodeStatusEvent;
2630
import org.openqa.selenium.grid.data.Session;
31+
import org.openqa.selenium.grid.data.SessionClosedEvent;
2732
import org.openqa.selenium.grid.data.Slot;
2833
import org.openqa.selenium.grid.data.SlotId;
2934
import org.openqa.selenium.grid.security.Secret;
@@ -46,11 +51,6 @@
4651
import static org.openqa.selenium.grid.data.Availability.DOWN;
4752
import static org.openqa.selenium.grid.data.Availability.DRAINING;
4853
import static org.openqa.selenium.grid.data.Availability.UP;
49-
import static org.openqa.selenium.grid.data.NodeDrainComplete.NODE_DRAIN_COMPLETE;
50-
import static org.openqa.selenium.grid.data.NodeDrainStarted.NODE_DRAIN_STARTED;
51-
import static org.openqa.selenium.grid.data.NodeRemovedEvent.NODE_REMOVED;
52-
import static org.openqa.selenium.grid.data.NodeStatusEvent.NODE_STATUS;
53-
import static org.openqa.selenium.grid.data.SessionClosedEvent.SESSION_CLOSED;
5454

5555
public class GridModel {
5656

@@ -63,12 +63,12 @@ public class GridModel {
6363
public GridModel(EventBus events, Secret registrationSecret) {
6464
this.events = Require.nonNull("Event bus", events);
6565

66-
events.addListener(NODE_DRAIN_STARTED, event -> setAvailability(event.getData(NodeId.class), DRAINING));
67-
events.addListener(NODE_DRAIN_COMPLETE, event -> remove(event.getData(NodeId.class)));
68-
events.addListener(NODE_REMOVED, event -> remove(event.getData(NodeId.class)));
69-
events.addListener(NODE_STATUS, event -> refresh(registrationSecret, event.getData(NodeStatus.class)));
66+
events.addListener(NodeDrainStarted.listener(nodeId -> setAvailability(nodeId, DRAINING)));
67+
events.addListener(NodeDrainComplete.listener(this::remove));
68+
events.addListener(NodeRemovedEvent.listener(this::remove));
69+
events.addListener(NodeStatusEvent.listener(status -> refresh(registrationSecret, status)));
7070

71-
events.addListener(SESSION_CLOSED, event -> release(event.getData(SessionId.class)));
71+
events.addListener(SessionClosedEvent.listener(this::release));
7272
}
7373

7474
public GridModel add(NodeStatus node) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
import org.openqa.selenium.grid.data.CreateSessionResponse;
3030
import org.openqa.selenium.grid.data.DistributorStatus;
3131
import org.openqa.selenium.grid.data.NodeAddedEvent;
32+
import org.openqa.selenium.grid.data.NodeDrainComplete;
3233
import org.openqa.selenium.grid.data.NodeId;
3334
import org.openqa.selenium.grid.data.NodeRejectedEvent;
3435
import org.openqa.selenium.grid.data.NodeRemovedEvent;
3536
import org.openqa.selenium.grid.data.NodeStatus;
37+
import org.openqa.selenium.grid.data.NodeStatusEvent;
3638
import org.openqa.selenium.grid.data.Slot;
3739
import org.openqa.selenium.grid.data.SlotId;
3840
import org.openqa.selenium.grid.distributor.Distributor;
@@ -70,8 +72,6 @@
7072
import static com.google.common.collect.ImmutableSet.toImmutableSet;
7173
import static org.openqa.selenium.grid.data.Availability.DOWN;
7274
import static org.openqa.selenium.grid.data.Availability.DRAINING;
73-
import static org.openqa.selenium.grid.data.NodeDrainComplete.NODE_DRAIN_COMPLETE;
74-
import static org.openqa.selenium.grid.data.NodeStatusEvent.NODE_STATUS;
7575

7676
public class LocalDistributor extends Distributor {
7777

@@ -102,9 +102,9 @@ public LocalDistributor(
102102
this.model = new GridModel(bus, registrationSecret);
103103
this.nodes = new HashMap<>();
104104

105-
bus.addListener(NODE_STATUS, event -> register(registrationSecret, event.getData(NodeStatus.class)));
106-
bus.addListener(NODE_STATUS, event -> model.refresh(registrationSecret, event.getData(NodeStatus.class)));
107-
bus.addListener(NODE_DRAIN_COMPLETE, event -> remove(event.getData(NodeId.class)));
105+
bus.addListener(NodeStatusEvent.listener(status -> register(registrationSecret, status)));
106+
bus.addListener(NodeStatusEvent.listener(status -> model.refresh(registrationSecret, status)));
107+
bus.addListener(NodeDrainComplete.listener(this::remove));
108108
}
109109

110110
public static Distributor create(Config config) {

0 commit comments

Comments
 (0)