Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

import static com.microsoft.playwright.impl.Serialization.gson;
import static java.lang.System.currentTimeMillis;
Expand All @@ -39,7 +40,7 @@
String method;
JsonObject params;
JsonElement result;
SerializedError error;

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / dev (ubuntu-latest, chromium)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / dev (macos-15-xlarge, webkit)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / stable (ubuntu-latest, chrome)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / dev (ubuntu-latest, webkit)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / dev (ubuntu-latest, firefox)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / dev (macos-latest, chromium)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / stable (macos-latest, msedge)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / stable (macos-latest, chrome)

cannot find symbol

Check failure on line 43 in playwright/src/main/java/com/microsoft/playwright/impl/Connection.java

View workflow job for this annotation

GitHub Actions / dev (macos-latest, firefox)

cannot find symbol
JsonObject errorDetails;
JsonArray log;

Expand Down Expand Up @@ -69,6 +70,7 @@
private final Map<String, WaitableResult<JsonElement>> callbacks = new HashMap<>();
private String title;
private boolean titleReported = false;
private static final long DRIVER_RESPONSE_GRACE_MS = 10_000;
private static final boolean isLogging;
static {
String debug = System.getenv("DEBUG");
Expand Down Expand Up @@ -145,7 +147,19 @@
}

public JsonElement sendMessage(String guid, String method, JsonObject params, Double timeout) {
return root.runUntil(() -> {}, internalSendMessage(guid, method, params, timeout, true, true));
WaitableResult<JsonElement> result = internalSendMessage(guid, method, params, timeout, true, true);
if (timeout == null || timeout == 0) {
return root.runUntil(() -> {}, result);
}
// The driver enforces the timeout, but the driver itself may hang.
long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeout.longValue() + DRIVER_RESPONSE_GRACE_MS);
while (!result.isDone()) {
if (!processOneMessage() && System.nanoTime() - deadline > 0) {
result.completeExceptionally(new TimeoutError("Timeout " + timeout.longValue() + "ms exceeded while waiting for the driver to respond to " +
method + ". The driver is not responding."));
}
}
return result.get();
}

public WaitableResult<JsonElement> sendMessageAsync(String guid, String method, JsonObject params) {
Expand Down Expand Up @@ -232,14 +246,15 @@
objects.remove(guid);
}

void processOneMessage() {
boolean processOneMessage() {
JsonObject message = transport.poll(Duration.ofMillis(10));
if (message == null) {
return;
return false;
}
Gson gson = gson();
Message messageObj = gson.fromJson(message, Message.class);
dispatch(messageObj);
return true;
}

private static String formatCallLog(JsonArray log) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.microsoft.playwright;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import org.junit.jupiter.api.condition.OS;

import java.lang.reflect.Field;

import static org.junit.jupiter.api.Assertions.*;

public class TestUnresponsiveDriver {
@Test
@DisabledOnOs(OS.WINDOWS)
@EnabledForJreRange(min = JRE.JAVA_9)
void shouldThrowTimeoutErrorWhenDriverStopsResponding() throws Exception {
try (Playwright playwright = Playwright.create()) {
Browser browser = Utils.getBrowserTypeFromEnv(playwright).launch(TestBase.createLaunchOptions());
Page page = browser.newPage();
long pid = driverPid(playwright);
signal("STOP", pid);
try {
long start = System.nanoTime();
TimeoutError e = assertThrows(TimeoutError.class,
() -> page.waitForFunction("() => false", null, new Page.WaitForFunctionOptions().setTimeout(100)));
long elapsedMs = (System.nanoTime() - start) / 1_000_000;
assertTrue(e.getMessage().contains("Timeout 100ms exceeded while waiting for the driver to respond to waitForFunction"), e.getMessage());
assertTrue(elapsedMs < 20_000, "elapsed: " + elapsedMs);
} finally {
signal("CONT", pid);
}
// The driver's late reply to the abandoned call must not break the connection.
assertEquals(2, page.evaluate("1 + 1"));
}
}

private static long driverPid(Playwright playwright) throws Exception {
Field field = playwright.getClass().getDeclaredField("driverProcess");
field.setAccessible(true);
// Called via reflection as the tests are compiled with JDK 8 on CI.
return (long) Process.class.getMethod("pid").invoke(field.get(playwright));
}

private static void signal(String signal, long pid) throws Exception {
Process kill = new ProcessBuilder("kill", "-" + signal, Long.toString(pid)).inheritIO().start();
assertEquals(0, kill.waitFor());
}
}
Loading