Skip to content

Commit 46fc208

Browse files
authored
[grid] Consider max-session value while selecting the slot and identifying Node capacity (#9838)
1 parent 58c925d commit 46fc208

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

java/src/org/openqa/selenium/grid/data/NodeStatus.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ public boolean hasCapacity() {
139139
return slots.stream().anyMatch(slot -> slot.getSession() == null);
140140
}
141141

142+
// Check if the Node's max session limit is not exceeded and has a free slot that supports the capability.
142143
public boolean hasCapacity(Capabilities caps) {
143-
return slots.stream()
144-
.anyMatch(slot -> slot.getSession() == null && slot.isSupporting(caps));
144+
return slots.stream().filter(slot -> slot.getSession() != null).count() < maxSessionCount &&
145+
slots.stream().anyMatch(slot -> slot.getSession() == null && slot.isSupporting(caps));
145146
}
146147

147148
public int getMaxSessionCount() {

java/test/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelectorTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ public void theMostLightlyLoadedNodeIsSelectedFirst() {
131131
assertThat(lightest.getSlots().stream()).anyMatch(slot -> expected.equals(slot.getId()));
132132
}
133133

134+
@Test
135+
public void theNodeWhichHasExceededMaxSessionsIsNotSelected() {
136+
Capabilities chrome = new ImmutableCapabilities("browserName", "chrome");
137+
138+
NodeStatus lightLoad =
139+
createNode(ImmutableList.of(chrome), 12, 2);
140+
NodeStatus mediumLoad =
141+
createNode(ImmutableList.of(chrome), 12, 5);
142+
NodeStatus maximumLoad =
143+
createNode(ImmutableList.of(chrome), 12, 12);
144+
145+
Set<SlotId> ids = selector.selectSlot(chrome, ImmutableSet.of(maximumLoad, mediumLoad, lightLoad));
146+
SlotId expected = ids.iterator().next();
147+
148+
// The slot should belong to the Node with light load
149+
assertThat(lightLoad.getSlots().stream())
150+
.anyMatch(slot -> expected.equals(slot.getId()));
151+
152+
// The node whose current number of sessions is greater than or equal to the max sessions is not included
153+
// Hence, the node with the maximum load is skipped
154+
ImmutableSet<NodeId> nodeIds = ids.stream()
155+
.map(SlotId::getOwningNodeId)
156+
.distinct()
157+
.collect(toImmutableSet());
158+
assertThat(nodeIds).doesNotContain(maximumLoad.getNodeId());
159+
assertThat(nodeIds)
160+
.containsSequence(
161+
lightLoad.getNodeId(),
162+
mediumLoad.getNodeId());
163+
}
164+
134165
@Test
135166
public void nodesAreOrderedByNumberOfSupportedBrowsersAndLoad() {
136167
Capabilities chrome = new ImmutableCapabilities("browserName", "chrome");
@@ -161,6 +192,7 @@ public void nodesAreOrderedByNumberOfSupportedBrowsersAndLoad() {
161192
.anyMatch(slot -> expected.equals(slot.getId()));
162193

163194
// Nodes are ordered by the diversity of supported browsers, then by load
195+
// The node whose current number of sessions is greater than or equal to the max sessions is not included
164196
ImmutableSet<NodeId> nodeIds = ids.stream()
165197
.map(SlotId::getOwningNodeId)
166198
.distinct()
@@ -169,7 +201,6 @@ public void nodesAreOrderedByNumberOfSupportedBrowsersAndLoad() {
169201
.containsSequence(
170202
highLoadAndOneBrowser.getNodeId(),
171203
mediumLoadAndTwoBrowsers.getNodeId(),
172-
mediumLoadAndOtherTwoBrowsers.getNodeId(),
173204
lightLoadAndThreeBrowsers.getNodeId());
174205
}
175206

0 commit comments

Comments
 (0)