Skip to content

Commit 7c69619

Browse files
committed
[grid] Unifying the slot matching logic in one single place
For now, we will use the basic browser, platform, and browser version values for matching a session caps with a slot stereotype. We will probably make the code a bit more elegant and efficient in a future iteration.
1 parent 2bc0700 commit 7c69619

File tree

5 files changed

+240
-13
lines changed

5 files changed

+240
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.grid.data;
19+
20+
import org.openqa.selenium.Capabilities;
21+
22+
import java.util.Objects;
23+
24+
public class DefaultSlotMatcher implements SlotMatcher {
25+
26+
@Override
27+
public boolean matches(Capabilities capabilities, Capabilities stereotype) {
28+
// Simple browser, browserVersion and platformName match
29+
boolean browserNameMatch =
30+
Objects.equals(stereotype.getBrowserName(), capabilities.getBrowserName());
31+
boolean browserVersionMatch =
32+
capabilities.getBrowserVersion().isEmpty() ||
33+
Objects.equals(stereotype.getBrowserVersion(), capabilities.getBrowserVersion());
34+
boolean platformNameMatch =
35+
capabilities.getPlatformName() == null ||
36+
Objects.equals(stereotype.getPlatformName(), capabilities.getPlatformName());
37+
return browserNameMatch && browserVersionMatch && platformNameMatch;
38+
}
39+
}

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ public class Slot {
3232
private final Capabilities stereotype;
3333
private final Optional<Session> session;
3434
private final Instant lastStarted;
35+
private final SlotMatcher slotMatcher;
3536

3637
public Slot(SlotId id, Capabilities stereotype, Instant lastStarted, Optional<Session> session) {
3738
this.id = Require.nonNull("Slot ID", id);
3839
this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));
3940
this.lastStarted = Require.nonNull("Last started", lastStarted);
4041
this.session = Require.nonNull("Session", session);
42+
this.slotMatcher = new DefaultSlotMatcher();
4143
}
4244

4345
public SlotId getId() {
@@ -57,15 +59,7 @@ public Optional<Session> getSession() {
5759
}
5860

5961
public boolean isSupporting(Capabilities caps) {
60-
// Simple implementation --- only checks current values
61-
Capabilities stereotype = getStereotype();
62-
63-
return stereotype.getCapabilityNames().stream()
64-
.map(name -> Objects.equals(
65-
stereotype.getCapability(name),
66-
caps.getCapability(name)))
67-
.reduce(Boolean::logicalAnd)
68-
.orElse(false);
62+
return slotMatcher.matches(caps, getStereotype());
6963
}
7064

7165
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.grid.data;
19+
20+
import org.openqa.selenium.Capabilities;
21+
22+
/** Used to determine how a {@link Slot} can match its
23+
* stereotype to the capabilities sent in a particular
24+
* New Session request.
25+
*/
26+
@FunctionalInterface
27+
public interface SlotMatcher {
28+
boolean matches(Capabilities capabilities, Capabilities stereotype);
29+
}

java/server/src/org/openqa/selenium/grid/node/local/LocalNodeFactory.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.common.collect.ImmutableList;
2121
import org.openqa.selenium.Capabilities;
2222
import org.openqa.selenium.grid.config.Config;
23+
import org.openqa.selenium.grid.data.DefaultSlotMatcher;
24+
import org.openqa.selenium.grid.data.SlotMatcher;
2325
import org.openqa.selenium.grid.docker.DockerOptions;
2426
import org.openqa.selenium.grid.log.LoggingOptions;
2527
import org.openqa.selenium.grid.node.Node;
@@ -78,18 +80,19 @@ private static Collection<SessionFactory> createSessionFactory(
7880
Tracer tracer,
7981
HttpClient.Factory clientFactory,
8082
List<DriverService.Builder<?, ?>> builders,
81-
Capabilities capabilities) {
83+
Capabilities stereotype) {
8284
ImmutableList.Builder<SessionFactory> toReturn = ImmutableList.builder();
85+
SlotMatcher slotMatcher = new DefaultSlotMatcher();
8386

8487
builders.stream()
85-
.filter(builder -> builder.score(capabilities) > 0)
88+
.filter(builder -> builder.score(stereotype) > 0)
8689
.forEach(builder -> {
8790
DriverService.Builder<?, ?> driverServiceBuilder = builder.usingAnyFreePort();
8891
toReturn.add(new DriverServiceSessionFactory(
8992
tracer,
9093
clientFactory,
91-
capabilities,
92-
c -> driverServiceBuilder.score(c) > 0,
94+
stereotype,
95+
capabilities -> slotMatcher.matches(capabilities, stereotype),
9396
driverServiceBuilder));
9497
});
9598

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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.grid.data;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
import org.junit.Test;
23+
import org.openqa.selenium.ImmutableCapabilities;
24+
import org.openqa.selenium.Platform;
25+
import org.openqa.selenium.remote.CapabilityType;
26+
27+
public class DefaultSlotMatcherTest {
28+
29+
private final DefaultSlotMatcher slotMatcher = new DefaultSlotMatcher();
30+
31+
@Test
32+
public void fullMatch() {
33+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
34+
CapabilityType.BROWSER_NAME, "chrome",
35+
CapabilityType.BROWSER_VERSION, "80",
36+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
37+
);
38+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
39+
CapabilityType.BROWSER_NAME, "chrome",
40+
CapabilityType.BROWSER_VERSION, "80",
41+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
42+
);
43+
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
44+
}
45+
46+
@Test
47+
public void matchesBrowserAndVersion() {
48+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
49+
CapabilityType.BROWSER_NAME, "chrome",
50+
CapabilityType.BROWSER_VERSION, "80",
51+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
52+
);
53+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
54+
CapabilityType.BROWSER_NAME, "chrome",
55+
CapabilityType.BROWSER_VERSION, "80"
56+
);
57+
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
58+
}
59+
60+
@Test
61+
public void matchesBrowser() {
62+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
63+
CapabilityType.BROWSER_NAME, "chrome",
64+
CapabilityType.BROWSER_VERSION, "80",
65+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
66+
);
67+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
68+
CapabilityType.BROWSER_NAME, "chrome"
69+
);
70+
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
71+
}
72+
73+
@Test
74+
public void platformDoesNotMatch() {
75+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
76+
CapabilityType.BROWSER_NAME, "chrome",
77+
CapabilityType.BROWSER_VERSION, "80",
78+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
79+
);
80+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
81+
CapabilityType.BROWSER_NAME, "chrome",
82+
CapabilityType.BROWSER_VERSION, "80",
83+
CapabilityType.PLATFORM_NAME, Platform.MAC
84+
);
85+
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
86+
}
87+
88+
@Test
89+
public void browserDoesNotMatch() {
90+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
91+
CapabilityType.BROWSER_NAME, "chrome",
92+
CapabilityType.BROWSER_VERSION, "80",
93+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
94+
);
95+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
96+
CapabilityType.BROWSER_NAME, "firefox",
97+
CapabilityType.BROWSER_VERSION, "80",
98+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
99+
);
100+
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
101+
}
102+
103+
@Test
104+
public void browserVersionDoesNotMatch() {
105+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
106+
CapabilityType.BROWSER_NAME, "chrome",
107+
CapabilityType.BROWSER_VERSION, "80",
108+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
109+
);
110+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
111+
CapabilityType.BROWSER_NAME, "chrome",
112+
CapabilityType.BROWSER_VERSION, "84",
113+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
114+
);
115+
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
116+
}
117+
118+
@Test
119+
public void requestedPlatformDoesNotMatch() {
120+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
121+
CapabilityType.BROWSER_NAME, "chrome",
122+
CapabilityType.BROWSER_VERSION, "80"
123+
);
124+
125+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
126+
CapabilityType.BROWSER_NAME, "chrome",
127+
CapabilityType.BROWSER_VERSION, "84",
128+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
129+
);
130+
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
131+
}
132+
133+
@Test
134+
public void requestedBrowserVersionDoesNotMatch() {
135+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
136+
CapabilityType.BROWSER_NAME, "chrome",
137+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
138+
);
139+
140+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
141+
CapabilityType.BROWSER_NAME, "chrome",
142+
CapabilityType.BROWSER_VERSION, "84",
143+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
144+
);
145+
assertThat(slotMatcher.matches(capabilities, stereotype)).isFalse();
146+
}
147+
148+
@Test
149+
public void matchesWithJWPCaps() {
150+
ImmutableCapabilities stereotype = new ImmutableCapabilities(
151+
CapabilityType.BROWSER_NAME, "chrome",
152+
CapabilityType.BROWSER_VERSION, "80",
153+
CapabilityType.PLATFORM_NAME, Platform.WINDOWS
154+
);
155+
ImmutableCapabilities capabilities = new ImmutableCapabilities(
156+
CapabilityType.BROWSER_NAME, "chrome",
157+
CapabilityType.VERSION, "80",
158+
CapabilityType.PLATFORM, Platform.WINDOWS
159+
);
160+
assertThat(slotMatcher.matches(capabilities, stereotype)).isTrue();
161+
}
162+
}

0 commit comments

Comments
 (0)