Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stuck MJPEG clients when one client fails to initialize #676

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ android {
'--add-exports', 'java.base/sun.nio.ch=ALL-UNNAMED',
'--add-opens', 'java.base/java.lang.reflect=ALL-UNNAMED',
'--add-opens', 'java.base/java.io=ALL-UNNAMED',
'--add-opens', 'java.base/java.net=ALL-UNNAMED',
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
'--add-opens', 'java.base/sun.net.www.protocol.http=ALL-UNNAMED',
'--add-exports', 'jdk.unsupported/sun.misc=ALL-UNNAMED',
])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ void initialize() {
}

try {
while (!in.ready()) {
if (!in.ready()) {
SystemClock.sleep(INPUT_NOT_READY_SLEEP_TIME_MS);
// Client initialization will be retried on the next frame
return;
}

Logger.info(String.format(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.appium.uiautomator2.server.mjpeg;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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();
}
}
Loading