ceftests: Add support for scaling default test timeout values

This commit is contained in:
Marshall Greenblatt 2023-01-13 13:49:33 -05:00
parent 2b906c31b5
commit b065ca8cf4
10 changed files with 84 additions and 51 deletions

View File

@ -84,7 +84,7 @@ class ResourceManagerTestHandler : public RoutingTestHandler {
// Create the browser. // Create the browser.
CreateBrowser(GetNextURL()); CreateBrowser(GetNextURL());
SetTestTimeout(IsChromeRuntimeEnabled() ? 10000 : 5000); SetTestTimeout(5000);
} }
cef_return_value_t OnBeforeResourceLoad( cef_return_value_t OnBeforeResourceLoad(

View File

@ -651,8 +651,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe<HttpTestRunner> {
void SetTestTimeout(int timeout_ms) { void SetTestTimeout(int timeout_ms) {
EXPECT_UI_THREAD(); EXPECT_UI_THREAD();
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( const auto timeout = GetConfiguredTestTimeout(timeout_ms);
"disable-test-timeout")) { if (!timeout) {
return; return;
} }
@ -660,8 +660,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe<HttpTestRunner> {
// test runner can be destroyed before the timeout expires. // test runner can be destroyed before the timeout expires.
GetUIThreadHelper()->PostDelayedTask( GetUIThreadHelper()->PostDelayedTask(
base::BindOnce(&HttpTestRunner::OnTestTimeout, base::Unretained(this), base::BindOnce(&HttpTestRunner::OnTestTimeout, base::Unretained(this),
timeout_ms), *timeout),
timeout_ms); *timeout);
} }
void OnTestTimeout(int timeout_ms) { void OnTestTimeout(int timeout_ms) {

View File

@ -7,16 +7,17 @@
#include "include/cef_task.h" #include "include/cef_task.h"
#include "include/wrapper/cef_closure_task.h" #include "include/wrapper/cef_closure_task.h"
#include "tests/ceftests/test_handler.h" #include "tests/ceftests/test_handler.h"
#include "tests/ceftests/test_util.h"
#include "tests/gtest/include/gtest/gtest.h" #include "tests/gtest/include/gtest/gtest.h"
namespace { namespace {
void WaitForEvent(CefRefPtr<CefWaitableEvent> event) { void WaitForEvent(CefRefPtr<CefWaitableEvent> event) {
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( const auto timeout = GetConfiguredTestTimeout(/*timeout_ms=*/1000);
"disable-test-timeout")) { if (!timeout) {
event->Wait(); event->Wait();
} else { } else {
EXPECT_TRUE(event->TimedWait(1000)); EXPECT_TRUE(event->TimedWait(*timeout));
} }
} }

View File

@ -13,6 +13,7 @@
#include "include/wrapper/cef_closure_task.h" #include "include/wrapper/cef_closure_task.h"
#include "include/wrapper/cef_stream_resource_handler.h" #include "include/wrapper/cef_stream_resource_handler.h"
#include "tests/ceftests/test_request.h" #include "tests/ceftests/test_request.h"
#include "tests/ceftests/test_util.h"
#include "tests/shared/common/client_switches.h" #include "tests/shared/common/client_switches.h"
namespace { namespace {
@ -442,8 +443,8 @@ void TestHandler::SetTestTimeout(int timeout_ms, bool treat_as_error) {
return; return;
} }
if (treat_as_error && CefCommandLine::GetGlobalCommandLine()->HasSwitch( const auto timeout = GetConfiguredTestTimeout(timeout_ms);
"disable-test-timeout")) { if (treat_as_error && !timeout) {
return; return;
} }
@ -451,8 +452,8 @@ void TestHandler::SetTestTimeout(int timeout_ms, bool treat_as_error) {
// can be destroyed before the timeout expires. // can be destroyed before the timeout expires.
GetUIThreadHelper()->PostDelayedTask( GetUIThreadHelper()->PostDelayedTask(
base::BindOnce(&TestHandler::OnTestTimeout, base::Unretained(this), base::BindOnce(&TestHandler::OnTestTimeout, base::Unretained(this),
timeout_ms, treat_as_error), *timeout, treat_as_error),
timeout_ms); *timeout);
} }
void TestHandler::TestComplete() { void TestHandler::TestComplete() {

View File

@ -11,6 +11,7 @@
#include "include/wrapper/cef_helpers.h" #include "include/wrapper/cef_helpers.h"
#include "tests/ceftests/test_request.h" #include "tests/ceftests/test_request.h"
#include "tests/ceftests/test_server_observer.h" #include "tests/ceftests/test_server_observer.h"
#include "tests/ceftests/test_util.h"
#include "tests/ceftests/track_callback.h" #include "tests/ceftests/track_callback.h"
#include "tests/gtest/include/gtest/gtest.h" #include "tests/gtest/include/gtest/gtest.h"
@ -165,11 +166,11 @@ void SignalIfDone(CefRefPtr<CefWaitableEvent> event,
} }
void Wait(CefRefPtr<CefWaitableEvent> event) { void Wait(CefRefPtr<CefWaitableEvent> event) {
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( const auto timeout = GetConfiguredTestTimeout(/*timeout_ms=*/2000);
"disable-test-timeout")) { if (!timeout) {
event->Wait(); event->Wait();
} else { } else {
event->TimedWait(2000); event->TimedWait(*timeout);
} }
} }

View File

@ -373,8 +373,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe<HttpTestRunner> {
void SetTestTimeout(int timeout_ms) { void SetTestTimeout(int timeout_ms) {
EXPECT_UI_THREAD(); EXPECT_UI_THREAD();
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( const auto timeout = GetConfiguredTestTimeout(timeout_ms);
"disable-test-timeout")) { if (!timeout) {
return; return;
} }
@ -382,8 +382,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe<HttpTestRunner> {
// test runner can be destroyed before the timeout expires. // test runner can be destroyed before the timeout expires.
GetUIThreadHelper()->PostDelayedTask( GetUIThreadHelper()->PostDelayedTask(
base::BindOnce(&HttpTestRunner::OnTestTimeout, base::Unretained(this), base::BindOnce(&HttpTestRunner::OnTestTimeout, base::Unretained(this),
timeout_ms), *timeout),
timeout_ms); *timeout);
} }
void OnTestTimeout(int timeout_ms) { void OnTestTimeout(int timeout_ms) {

View File

@ -5,6 +5,8 @@
#include "tests/ceftests/test_util.h" #include "tests/ceftests/test_util.h"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <cstdlib>
#include "include/cef_base.h" #include "include/cef_base.h"
#include "include/cef_command_line.h" #include "include/cef_command_line.h"
@ -278,23 +280,19 @@ void TestStringVectorEqual(const std::vector<CefString>& val1,
} }
bool TestOldResourceAPI() { bool TestOldResourceAPI() {
static int state = -1; static bool state = []() {
if (state == -1) { return CefCommandLine::GetGlobalCommandLine()->HasSwitch(
CefRefPtr<CefCommandLine> command_line = "test-old-resource-api");
CefCommandLine::GetGlobalCommandLine(); }();
state = command_line->HasSwitch("test-old-resource-api") ? 1 : 0; return state;
}
return state ? true : false;
} }
bool IsChromeRuntimeEnabled() { bool IsChromeRuntimeEnabled() {
static int state = -1; static bool state = []() {
if (state == -1) { return CefCommandLine::GetGlobalCommandLine()->HasSwitch(
CefRefPtr<CefCommandLine> command_line = "enable-chrome-runtime");
CefCommandLine::GetGlobalCommandLine(); }();
state = command_line->HasSwitch("enable-chrome-runtime") ? 1 : 0; return state;
}
return state ? true : false;
} }
bool IsBFCacheEnabled() { bool IsBFCacheEnabled() {
@ -303,15 +301,13 @@ bool IsBFCacheEnabled() {
return false; return false;
} }
// Enabled by default starting in M96. static bool state = []() {
static int state = -1; const std::string& value =
if (state == -1) { CefCommandLine::GetGlobalCommandLine()->GetSwitchValue(
CefRefPtr<CefCommandLine> command_line = "disable-features");
CefCommandLine::GetGlobalCommandLine(); return value.find("BackForwardCache") == std::string::npos;
const std::string& value = command_line->GetSwitchValue("disable-features"); }();
state = value.find("BackForwardCache") == std::string::npos ? 1 : 0; return state;
}
return state ? true : false;
} }
bool IsSameSiteBFCacheEnabled() { bool IsSameSiteBFCacheEnabled() {
@ -325,6 +321,31 @@ bool IgnoreURL(const std::string& url) {
url.find("/favicon.ico") != std::string::npos; url.find("/favicon.ico") != std::string::npos;
} }
std::optional<int> GetConfiguredTestTimeout(int timeout_ms) {
static std::optional<double> multiplier = []() -> std::optional<double> {
auto command_line = CefCommandLine::GetGlobalCommandLine();
if (command_line->HasSwitch("disable-test-timeout")) {
return std::nullopt;
}
const std::string& sval =
command_line->GetSwitchValue("test-timeout-multiplier");
if (!sval.empty()) {
if (double dval = std::atof(sval.c_str())) {
return dval;
}
}
return IsChromeRuntimeEnabled() ? 2.0 : 1.0;
}();
if (!multiplier) {
// Test timeout is disabled.
return std::nullopt;
}
return static_cast<int>(
std::round(static_cast<double>(timeout_ms) * (*multiplier)));
}
CefRefPtr<CefRequestContext> CreateTestRequestContext( CefRefPtr<CefRequestContext> CreateTestRequestContext(
TestRequestContextMode mode, TestRequestContextMode mode,
const std::string& cache_path) { const std::string& cache_path) {

View File

@ -6,6 +6,8 @@
#define CEF_TESTS_CEFTESTS_TEST_UTIL_H_ #define CEF_TESTS_CEFTESTS_TEST_UTIL_H_
#pragma once #pragma once
#include <optional>
#include "include/cef_process_message.h" #include "include/cef_process_message.h"
#include "include/cef_request.h" #include "include/cef_request.h"
#include "include/cef_request_context.h" #include "include/cef_request_context.h"
@ -96,6 +98,10 @@ bool IsSameSiteBFCacheEnabled();
// Returns true if requests for |url| should be ignored by tests. // Returns true if requests for |url| should be ignored by tests.
bool IgnoreURL(const std::string& url); bool IgnoreURL(const std::string& url);
// Returns |timeout_ms| as scaled by the current configuration, or std::nullopt
// if timeouts are disabled.
std::optional<int> GetConfiguredTestTimeout(int timeout_ms);
// Return a RequestContext object matching the specified |mode|. // Return a RequestContext object matching the specified |mode|.
// |cache_path| may be specified for CUSTOM modes. // |cache_path| may be specified for CUSTOM modes.
// Use the RC_TEST_GROUP_BASE macro to test all valid combinations. // Use the RC_TEST_GROUP_BASE macro to test all valid combinations.

View File

@ -2760,7 +2760,7 @@ class RequestTestHandler : public TestHandler {
void RunTest() override { void RunTest() override {
// Time out the test after a reasonable period of time. // Time out the test after a reasonable period of time.
SetTestTimeout(IsChromeRuntimeEnabled() ? 10000 : 5000); SetTestTimeout(5000);
// Start pre-setup actions. // Start pre-setup actions.
PreSetupStart(); PreSetupStart();

View File

@ -8,6 +8,7 @@
#include "include/views/cef_window.h" #include "include/views/cef_window.h"
#include "include/views/cef_window_delegate.h" #include "include/views/cef_window_delegate.h"
#include "include/wrapper/cef_closure_task.h" #include "include/wrapper/cef_closure_task.h"
#include "tests/ceftests/test_util.h"
#include "tests/ceftests/thread_helper.h" #include "tests/ceftests/thread_helper.h"
#include "tests/gtest/include/gtest/gtest.h" #include "tests/gtest/include/gtest/gtest.h"
@ -125,14 +126,16 @@ void TestWindowDelegate::OnWindowCreated(CefRefPtr<CefWindow> window) {
// Close the window asynchronously. // Close the window asynchronously.
CefPostTask(TID_UI, CefPostTask(TID_UI,
base::BindOnce(&TestWindowDelegate::OnCloseWindow, this)); base::BindOnce(&TestWindowDelegate::OnCloseWindow, this));
} else if (!CefCommandLine::GetGlobalCommandLine()->HasSwitch( } else {
"disable-test-timeout")) { const auto timeout = GetConfiguredTestTimeout(kTestTimeout);
// Timeout the test after a reasonable delay. Use a WeakPtr so that the if (timeout) {
// delayed task doesn't keep this object alive. // Timeout the test after a reasonable delay. Use a WeakPtr so that the
CefPostDelayedTask(TID_UI, // delayed task doesn't keep this object alive.
base::BindOnce(&TestWindowDelegate::OnTimeoutWindow, CefPostDelayedTask(TID_UI,
weak_ptr_factory_.GetWeakPtr()), base::BindOnce(&TestWindowDelegate::OnTimeoutWindow,
kTestTimeout); weak_ptr_factory_.GetWeakPtr()),
*timeout);
}
} }
} }