Skip to content

Commit

Permalink
Optimize check for active tunnels (#257)
Browse files Browse the repository at this point in the history
Use single REST API call instead of several consequent ones
  • Loading branch information
valfirst authored May 29, 2024
1 parent 9742e35 commit a36ea41
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>saucerest</artifactId>
<version>2.4.0</version>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,24 +563,17 @@ private TunnelInformation getTunnelInformation(String name) {
*/
private String activeTunnelID(String username, String tunnelName) {
try {
List<String> tunnels = scEndpoint.getTunnelsForAUser();
if (tunnels.isEmpty()) {
// no active tunnels
return null;
}
// iterate over elements
for (String tunnelId : tunnels) {

com.saucelabs.saucerest.model.sauceconnect.TunnelInformation tunnelInformation =
scEndpoint.getTunnelInformation(tunnelId);
List<com.saucelabs.saucerest.model.sauceconnect.TunnelInformation> tunnelsInformation =
scEndpoint.getTunnelsInformationForAUser();

for (com.saucelabs.saucerest.model.sauceconnect.TunnelInformation tunnelInformation : tunnelsInformation) {
String configName = tunnelInformation.tunnelIdentifier;
String status = tunnelInformation.status;
if ("running".equalsIgnoreCase(status)
&& ("null".equalsIgnoreCase(configName) && tunnelName.equals(username))
|| !"null".equalsIgnoreCase(configName) && configName.equals(tunnelName)) {
// we have an active tunnel
return tunnelId;
return tunnelInformation.id;
}
}
} catch (JSONException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ private InputStream getResourceAsStream(String resourceName) {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void testOpenConnectionSuccessfully(boolean cleanUpOnExit) throws IOException {
when(mockSCEndpoint.getTunnelsForAUser()).thenReturn(List.of());
when(mockSCEndpoint.getTunnelsInformationForAUser()).thenReturn(List.of());
tunnelManager.setCleanUpOnExit(cleanUpOnExit);
Process process = testOpenConnection("/started_sc.log");
assertEquals(mockProcess, process);
}

@Test
void openConnectionTest_closes() throws IOException, InterruptedException {
when(mockSCEndpoint.getTunnelsForAUser()).thenReturn(List.of());
when(mockSCEndpoint.getTunnelsInformationForAUser()).thenReturn(List.of());
when(mockProcess.waitFor(30, TimeUnit.SECONDS)).thenReturn(true);
assertThrows(AbstractSauceTunnelManager.SauceConnectDidNotStartException.class, () -> testOpenConnection(
"/started_sc_closes.log"));
Expand All @@ -74,7 +74,7 @@ void openConnectionTest_closes() throws IOException, InterruptedException {

@Test
void testOpenConnectionWithExtraSpacesInArgs() throws IOException {
when(mockSCEndpoint.getTunnelsForAUser()).thenReturn(List.of());
when(mockSCEndpoint.getTunnelsInformationForAUser()).thenReturn(List.of());
testOpenConnection("/started_sc.log", " username-with-spaces-around ");
}

Expand All @@ -95,7 +95,7 @@ private Process testOpenConnection(String logFile, String username) throws IOExc
return tunnelManager.openConnection(
username, apiKey, dataCenter, port, null, " ", ps, false, "");
} finally {
verify(mockSCEndpoint).getTunnelsForAUser();
verify(mockSCEndpoint).getTunnelsInformationForAUser();
ArgumentCaptor<String[]> argsCaptor = ArgumentCaptor.forClass(String[].class);
verify(tunnelManager).createProcess(argsCaptor.capture(), any(File.class));
String[] actualArgs = argsCaptor.getValue();
Expand All @@ -122,14 +122,12 @@ void openConnectionTest_existing_tunnel() throws IOException {
started.tunnelIdentifier = "8949e55fb5e14fd6bf6230b7a609b494";
started.status = "running";

when(mockSCEndpoint.getTunnelsForAUser()).thenReturn(List.of(started.tunnelIdentifier));
when(mockSCEndpoint.getTunnelInformation(started.tunnelIdentifier)).thenReturn(started);
when(mockSCEndpoint.getTunnelsInformationForAUser()).thenReturn(List.of(started));

Process process = testOpenConnection("/started_sc.log");
assertEquals(mockProcess, process);

verify(mockSCEndpoint).getTunnelInformation(started.tunnelIdentifier);
verify(mockSCEndpoint).getTunnelsForAUser();
verify(mockSCEndpoint).getTunnelsInformationForAUser();
}

@ParameterizedTest
Expand Down

0 comments on commit a36ea41

Please sign in to comment.