Skip to content

Commit

Permalink
Init web socket clients lazily (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Mokhnach authored May 25, 2018
1 parent 68dccaa commit 95f0964
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/main/java/io/appium/java_client/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.ws.StringWebSocketClient;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.HttpCommandExecutor;
Expand Down Expand Up @@ -66,6 +67,8 @@ public class AndroidDriver<T extends WebElement>

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

private StringWebSocketClient logcatClient;

/**
* Creates a new instance based on command {@code executor} and {@code capabilities}.
*
Expand Down Expand Up @@ -193,4 +196,12 @@ public AndroidBatteryInfo getBatteryInfo() {
return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
"script", "mobile: batteryInfo", "args", Collections.emptyList())));
}

@Override
public synchronized StringWebSocketClient getLogcatClient() {
if (logcatClient == null) {
logcatClient = new StringWebSocketClient();
}
return logcatClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.function.Consumer;

public interface ListensToLogcatMessages extends ExecutesMethod {
StringWebSocketClient logcatClient = new StringWebSocketClient();
StringWebSocketClient getLogcatClient();

/**
* Start logcat messages broadcast via web socket.
Expand Down Expand Up @@ -68,7 +68,7 @@ default void startLogcatBroadcast(String host, int port) {
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
logcatClient.connect(endpointUri);
getLogcatClient().connect(endpointUri);
}

/**
Expand All @@ -80,7 +80,7 @@ default void startLogcatBroadcast(String host, int port) {
* @param handler a function, which accepts a single argument, which is the actual log message
*/
default void addLogcatMessagesListener(Consumer<String> handler) {
logcatClient.addMessageHandler(handler);
getLogcatClient().addMessageHandler(handler);
}

/**
Expand All @@ -92,7 +92,7 @@ default void addLogcatMessagesListener(Consumer<String> handler) {
* @param handler a function, which accepts a single argument, which is the actual exception instance
*/
default void addLogcatErrorsListener(Consumer<Throwable> handler) {
logcatClient.addErrorHandler(handler);
getLogcatClient().addErrorHandler(handler);
}

/**
Expand All @@ -105,7 +105,7 @@ default void addLogcatErrorsListener(Consumer<Throwable> handler) {
* connected to the web socket
*/
default void addLogcatConnectionListener(Runnable handler) {
logcatClient.addConnectionHandler(handler);
getLogcatClient().addConnectionHandler(handler);
}

/**
Expand All @@ -118,14 +118,14 @@ default void addLogcatConnectionListener(Runnable handler) {
* disconnected from the web socket
*/
default void addLogcatDisconnectionListener(Runnable handler) {
logcatClient.addDisconnectionHandler(handler);
getLogcatClient().addDisconnectionHandler(handler);
}

/**
* Removes all existing logcat handlers.
*/
default void removeAllLogcatListeners() {
logcatClient.removeAllHandlers();
getLogcatClient().removeAllHandlers();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.ws.StringWebSocketClient;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class IOSDriver<T extends WebElement>

private static final String IOS_PLATFORM = MobilePlatform.IOS;

private StringWebSocketClient syslogClient;

/**
* Creates a new instance based on command {@code executor} and {@code capabilities}.
*
Expand Down Expand Up @@ -225,4 +228,12 @@ class IOSAlert implements Alert {
}

}

@Override
public synchronized StringWebSocketClient getSyslogClient() {
if (syslogClient == null) {
syslogClient = new StringWebSocketClient();
}
return syslogClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import java.util.function.Consumer;

public interface ListensToSyslogMessages extends ExecutesMethod {
StringWebSocketClient syslogClient = new StringWebSocketClient();

StringWebSocketClient getSyslogClient();

/**
* Start syslog messages broadcast via web socket.
Expand Down Expand Up @@ -68,7 +69,7 @@ default void startSyslogBroadcast(String host, int port) {
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
syslogClient.connect(endpointUri);
getSyslogClient().connect(endpointUri);
}

/**
Expand All @@ -80,7 +81,7 @@ default void startSyslogBroadcast(String host, int port) {
* @param handler a function, which accepts a single argument, which is the actual log message
*/
default void addSyslogMessagesListener(Consumer<String> handler) {
syslogClient.addMessageHandler(handler);
getSyslogClient().addMessageHandler(handler);
}

/**
Expand All @@ -92,7 +93,7 @@ default void addSyslogMessagesListener(Consumer<String> handler) {
* @param handler a function, which accepts a single argument, which is the actual exception instance
*/
default void addSyslogErrorsListener(Consumer<Throwable> handler) {
syslogClient.addErrorHandler(handler);
getSyslogClient().addErrorHandler(handler);
}

/**
Expand All @@ -105,7 +106,7 @@ default void addSyslogErrorsListener(Consumer<Throwable> handler) {
* connected to the web socket
*/
default void addSyslogConnectionListener(Runnable handler) {
syslogClient.addConnectionHandler(handler);
getSyslogClient().addConnectionHandler(handler);
}

/**
Expand All @@ -118,14 +119,14 @@ default void addSyslogConnectionListener(Runnable handler) {
* disconnected from the web socket
*/
default void addSyslogDisconnectionListener(Runnable handler) {
syslogClient.addDisconnectionHandler(handler);
getSyslogClient().addDisconnectionHandler(handler);
}

/**
* Removes all existing syslog handlers.
*/
default void removeAllSyslogListeners() {
syslogClient.removeAllHandlers();
getSyslogClient().removeAllHandlers();
}

/**
Expand Down

0 comments on commit 95f0964

Please sign in to comment.