Skip to content

Commit

Permalink
fix test flakiness on MjpegScreenshotTest
Browse files Browse the repository at this point in the history
I realized a bit too late that the tests I added were flaky when  re-running the entire test suite:
` ./gradlew testServerDebugUnitTest   --rerun-tasks`

One of the tests would randomly (roughly 50% of the runs) fail with:
```
MjpegScreenshotTest > shouldNotBlockOnUnInitializedClient FAILED
    java.lang.AssertionError: expected:<200> but was:<-1>
        at org.junit.Assert.fail(Assert.java:89)
        at org.junit.Assert.failNotEquals(Assert.java:835)
        at org.junit.Assert.assertEquals(Assert.java:647)
        at org.junit.Assert.assertEquals(Assert.java:633)
        at io.appium.uiautomator2.server.mjpeg.MjpegScreenshotTest.shouldNotBlockOnUnInitializedClient(MjpegScreenshotTest.java:101)
```

After trying to understand what this meant: I realized I was not sending proper HTTP response, just some raw data on the socket.
It's even worse than that as we send it as streaming: the client doesn't really get an ending response.

There is no need to test that we can read a streaming response: just tweaking the mock data to send the proper header was enough to make the test reliable
  • Loading branch information
vbarbaresi committed Jan 2, 2025
1 parent 8d3930c commit 5d85866
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ 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);
String screenshotMockData = "screenshot data";
byte[] mockHTTPResponse =
("HTTP/1.1 200 OK\n"
+ "Content-Length: "
+ screenshotMockData.length()
+ "\n\n"
+ screenshotMockData)
.getBytes(StandardCharsets.UTF_8);
PowerMockito.stub(PowerMockito.method(MjpegScreenshotStream.class, "getScreenshot"))
.toReturn(mockScreenshotData);
.toReturn(mockHTTPResponse);
PowerMockito.whenNew(MjpegScreenshotStream.class)
.withAnyArguments()
.thenReturn(mockScreenshotStreamSpy);
Expand Down

0 comments on commit 5d85866

Please sign in to comment.