-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: stuck client when socket client never initializes (#676)
There was an issue with the Client implementation: if a socket started a connection on the internal MJPEG port and was never initialized, the entire server thread would be stuck in the while loop waiting for initialization. This broke other clients connected to the server: no frames would be received anymore. To reproduce on emulator / real device: - start an Appium server with mJPEG server enabled (`appium:mjpegServerPort:9100` for instance) - Start an Appium session that reads the mJPEG frames (using Appium Inspector for instance) - Then from `adb shell`: connect to the internal mJPEG server (default port 7810): `nc localhost 7810` - --> Streaming will be broken for the existing clients This fixes the problem by returning early: if the client is not ready initialization will be retried later if necessary (or never) instead of blocking the thread Another side effect is that we actually receive frames from `nc localhost 7810` instead of it being stuck
- Loading branch information
1 parent
ff0a2d0
commit 34161a3
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/test/java/io/appium/uiautomator2/server/MjpegScreenshotTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.appium.uiautomator2.server.mjpeg; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import static org.junit.Assert.*; | ||
|
||
import java.net.URL; | ||
import java.net.HttpURLConnection; | ||
import java.util.Collections; | ||
import java.nio.charset.StandardCharsets; | ||
import java.net.Socket; | ||
|
||
import static org.mockito.Mockito.spy; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import io.appium.uiautomator2.server.mjpeg.MjpegScreenshotServer; | ||
import io.appium.uiautomator2.server.mjpeg.MjpegScreenshotStream; | ||
import io.appium.uiautomator2.server.ServerConfig; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(MjpegScreenshotStream.class) | ||
public class MjpegScreenshotTest { | ||
private static MjpegScreenshotServer serverThread; | ||
private static int streamingPort; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
// Create a MJPEG server with a mocked getScreenshot method | ||
MjpegScreenshotStream mockScreenshotStreamSpy = | ||
spy(new MjpegScreenshotStream(Collections.emptyList())); | ||
byte[] mockScreenshotData = "screenshot data".getBytes(StandardCharsets.UTF_8); | ||
PowerMockito.stub(PowerMockito.method(MjpegScreenshotStream.class, "getScreenshot")) | ||
.toReturn(mockScreenshotData); | ||
PowerMockito.whenNew(MjpegScreenshotStream.class) | ||
.withAnyArguments() | ||
.thenReturn(mockScreenshotStreamSpy); | ||
|
||
streamingPort = ServerConfig.DEFAULT_MJPEG_SERVER_PORT; | ||
serverThread = new MjpegScreenshotServer(streamingPort); | ||
serverThread.start(); | ||
assertTrue(serverThread.isAlive()); | ||
} | ||
|
||
@Test | ||
public void shouldReceiveDataFromMjpegServer() throws Exception { | ||
URL url = new URL(String.format("http://localhost:%d", streamingPort)); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
int responseCode = connection.getResponseCode(); | ||
assertEquals(HttpURLConnection.HTTP_OK, responseCode); | ||
} | ||
|
||
@Test | ||
public void shouldNotBlockOnUnInitializedClient() throws Exception { | ||
// Create a socket and connect to the streaming server: it will create an uninitialized client | ||
Socket socket = new Socket("localhost", streamingPort); | ||
socket.close(); | ||
|
||
// We should still be able to receive data | ||
URL url = new URL(String.format("http://localhost:%d", streamingPort)); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
int responseCode = connection.getResponseCode(); | ||
assertEquals(HttpURLConnection.HTTP_OK, responseCode); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
serverThread.interrupt(); | ||
} | ||
} |