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

test: fix unit test flakiness on MjpegScreenshotTest #678

Merged
merged 2 commits into from
Jan 7, 2025
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 .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
name: Run js linter

java_test:
env:
CI: true
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.charset.StandardCharsets;
import java.net.Socket;

import org.junit.Assume;
import static org.mockito.Mockito.spy;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand All @@ -29,12 +30,23 @@ public class MjpegScreenshotTest {

@Before
public void setUp() throws Exception {
// Test is flaky in CI because we have to wait for the server to actually start
// Adding a sleep or a loop to wait on the server to be ready could help: skip it for now
Assume.assumeTrue("true".equalsIgnoreCase(System.getenv("CI")));

// Create a MJPEG server with a mocked getScreenshot method
MjpegScreenshotStream mockScreenshotStreamSpy =
spy(new MjpegScreenshotStream(Collections.emptyList()));
byte[] mockScreenshotData = "screenshot data".getBytes(StandardCharsets.UTF_8);
String mockScreenshotData = "screenshot data";
byte[] mockHTTPResponse =
("HTTP/1.1 200 OK\n"
+ "Content-Length: "
+ mockScreenshotData.length()
+ "\n\n"
+ mockScreenshotData)
.getBytes(StandardCharsets.UTF_8);
PowerMockito.stub(PowerMockito.method(MjpegScreenshotStream.class, "getScreenshot"))
.toReturn(mockScreenshotData);
.toReturn(mockHTTPResponse);
PowerMockito.whenNew(MjpegScreenshotStream.class)
.withAnyArguments()
.thenReturn(mockScreenshotStreamSpy);
Expand Down Expand Up @@ -70,6 +82,8 @@ public void shouldNotBlockOnUnInitializedClient() throws Exception {

@After
public void tearDown() {
if (serverThread != null) {
serverThread.interrupt();
}
}
}
Loading