From c12701592e3886c692fa77494b6013c5fc7ead71 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 13 Jan 2023 13:49:33 -0500 Subject: [PATCH] ceftests: Add support for scaling default test timeout values --- tests/ceftests/resource_manager_unittest.cc | 2 +- tests/ceftests/server_unittest.cc | 8 +-- tests/ceftests/task_unittest.cc | 7 +- tests/ceftests/test_handler.cc | 9 +-- .../ceftests/test_server_observer_unittest.cc | 7 +- tests/ceftests/test_server_unittest.cc | 8 +-- tests/ceftests/test_util.cc | 67 ++++++++++++------- tests/ceftests/test_util.h | 6 ++ tests/ceftests/urlrequest_unittest.cc | 2 +- tests/ceftests/views/test_window_delegate.cc | 19 +++--- 10 files changed, 84 insertions(+), 51 deletions(-) diff --git a/tests/ceftests/resource_manager_unittest.cc b/tests/ceftests/resource_manager_unittest.cc index f4904c329..df5325027 100644 --- a/tests/ceftests/resource_manager_unittest.cc +++ b/tests/ceftests/resource_manager_unittest.cc @@ -84,7 +84,7 @@ class ResourceManagerTestHandler : public RoutingTestHandler { // Create the browser. CreateBrowser(GetNextURL()); - SetTestTimeout(IsChromeRuntimeEnabled() ? 10000 : 5000); + SetTestTimeout(5000); } cef_return_value_t OnBeforeResourceLoad( diff --git a/tests/ceftests/server_unittest.cc b/tests/ceftests/server_unittest.cc index e1a3a06ec..c558e564b 100644 --- a/tests/ceftests/server_unittest.cc +++ b/tests/ceftests/server_unittest.cc @@ -651,8 +651,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe { void SetTestTimeout(int timeout_ms) { EXPECT_UI_THREAD(); - if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( - "disable-test-timeout")) { + const auto timeout = GetConfiguredTestTimeout(timeout_ms); + if (!timeout) { return; } @@ -660,8 +660,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe { // test runner can be destroyed before the timeout expires. GetUIThreadHelper()->PostDelayedTask( base::BindOnce(&HttpTestRunner::OnTestTimeout, base::Unretained(this), - timeout_ms), - timeout_ms); + *timeout), + *timeout); } void OnTestTimeout(int timeout_ms) { diff --git a/tests/ceftests/task_unittest.cc b/tests/ceftests/task_unittest.cc index 24034742b..b413fa990 100644 --- a/tests/ceftests/task_unittest.cc +++ b/tests/ceftests/task_unittest.cc @@ -7,16 +7,17 @@ #include "include/cef_task.h" #include "include/wrapper/cef_closure_task.h" #include "tests/ceftests/test_handler.h" +#include "tests/ceftests/test_util.h" #include "tests/gtest/include/gtest/gtest.h" namespace { void WaitForEvent(CefRefPtr event) { - if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( - "disable-test-timeout")) { + const auto timeout = GetConfiguredTestTimeout(/*timeout_ms=*/1000); + if (!timeout) { event->Wait(); } else { - EXPECT_TRUE(event->TimedWait(1000)); + EXPECT_TRUE(event->TimedWait(*timeout)); } } diff --git a/tests/ceftests/test_handler.cc b/tests/ceftests/test_handler.cc index 4bf9c0147..3ebf4ae16 100644 --- a/tests/ceftests/test_handler.cc +++ b/tests/ceftests/test_handler.cc @@ -13,6 +13,7 @@ #include "include/wrapper/cef_closure_task.h" #include "include/wrapper/cef_stream_resource_handler.h" #include "tests/ceftests/test_request.h" +#include "tests/ceftests/test_util.h" #include "tests/shared/common/client_switches.h" namespace { @@ -442,8 +443,8 @@ void TestHandler::SetTestTimeout(int timeout_ms, bool treat_as_error) { return; } - if (treat_as_error && CefCommandLine::GetGlobalCommandLine()->HasSwitch( - "disable-test-timeout")) { + const auto timeout = GetConfiguredTestTimeout(timeout_ms); + if (treat_as_error && !timeout) { return; } @@ -451,8 +452,8 @@ void TestHandler::SetTestTimeout(int timeout_ms, bool treat_as_error) { // can be destroyed before the timeout expires. GetUIThreadHelper()->PostDelayedTask( base::BindOnce(&TestHandler::OnTestTimeout, base::Unretained(this), - timeout_ms, treat_as_error), - timeout_ms); + *timeout, treat_as_error), + *timeout); } void TestHandler::TestComplete() { diff --git a/tests/ceftests/test_server_observer_unittest.cc b/tests/ceftests/test_server_observer_unittest.cc index b1fd4996e..dca955600 100644 --- a/tests/ceftests/test_server_observer_unittest.cc +++ b/tests/ceftests/test_server_observer_unittest.cc @@ -11,6 +11,7 @@ #include "include/wrapper/cef_helpers.h" #include "tests/ceftests/test_request.h" #include "tests/ceftests/test_server_observer.h" +#include "tests/ceftests/test_util.h" #include "tests/ceftests/track_callback.h" #include "tests/gtest/include/gtest/gtest.h" @@ -165,11 +166,11 @@ void SignalIfDone(CefRefPtr event, } void Wait(CefRefPtr event) { - if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( - "disable-test-timeout")) { + const auto timeout = GetConfiguredTestTimeout(/*timeout_ms=*/2000); + if (!timeout) { event->Wait(); } else { - event->TimedWait(2000); + event->TimedWait(*timeout); } } diff --git a/tests/ceftests/test_server_unittest.cc b/tests/ceftests/test_server_unittest.cc index 9e9da9d28..d81a51517 100644 --- a/tests/ceftests/test_server_unittest.cc +++ b/tests/ceftests/test_server_unittest.cc @@ -373,8 +373,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe { void SetTestTimeout(int timeout_ms) { EXPECT_UI_THREAD(); - if (CefCommandLine::GetGlobalCommandLine()->HasSwitch( - "disable-test-timeout")) { + const auto timeout = GetConfiguredTestTimeout(timeout_ms); + if (!timeout) { return; } @@ -382,8 +382,8 @@ class HttpTestRunner : public base::RefCountedThreadSafe { // test runner can be destroyed before the timeout expires. GetUIThreadHelper()->PostDelayedTask( base::BindOnce(&HttpTestRunner::OnTestTimeout, base::Unretained(this), - timeout_ms), - timeout_ms); + *timeout), + *timeout); } void OnTestTimeout(int timeout_ms) { diff --git a/tests/ceftests/test_util.cc b/tests/ceftests/test_util.cc index 35c0b5d58..a865fbacd 100644 --- a/tests/ceftests/test_util.cc +++ b/tests/ceftests/test_util.cc @@ -5,6 +5,8 @@ #include "tests/ceftests/test_util.h" #include +#include +#include #include "include/cef_base.h" #include "include/cef_command_line.h" @@ -278,23 +280,19 @@ void TestStringVectorEqual(const std::vector& val1, } bool TestOldResourceAPI() { - static int state = -1; - if (state == -1) { - CefRefPtr command_line = - CefCommandLine::GetGlobalCommandLine(); - state = command_line->HasSwitch("test-old-resource-api") ? 1 : 0; - } - return state ? true : false; + static bool state = []() { + return CefCommandLine::GetGlobalCommandLine()->HasSwitch( + "test-old-resource-api"); + }(); + return state; } bool IsChromeRuntimeEnabled() { - static int state = -1; - if (state == -1) { - CefRefPtr command_line = - CefCommandLine::GetGlobalCommandLine(); - state = command_line->HasSwitch("enable-chrome-runtime") ? 1 : 0; - } - return state ? true : false; + static bool state = []() { + return CefCommandLine::GetGlobalCommandLine()->HasSwitch( + "enable-chrome-runtime"); + }(); + return state; } bool IsBFCacheEnabled() { @@ -303,15 +301,13 @@ bool IsBFCacheEnabled() { return false; } - // Enabled by default starting in M96. - static int state = -1; - if (state == -1) { - CefRefPtr command_line = - CefCommandLine::GetGlobalCommandLine(); - const std::string& value = command_line->GetSwitchValue("disable-features"); - state = value.find("BackForwardCache") == std::string::npos ? 1 : 0; - } - return state ? true : false; + static bool state = []() { + const std::string& value = + CefCommandLine::GetGlobalCommandLine()->GetSwitchValue( + "disable-features"); + return value.find("BackForwardCache") == std::string::npos; + }(); + return state; } bool IsSameSiteBFCacheEnabled() { @@ -325,6 +321,31 @@ bool IgnoreURL(const std::string& url) { url.find("/favicon.ico") != std::string::npos; } +std::optional GetConfiguredTestTimeout(int timeout_ms) { + static std::optional multiplier = []() -> std::optional { + 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( + std::round(static_cast(timeout_ms) * (*multiplier))); +} + CefRefPtr CreateTestRequestContext( TestRequestContextMode mode, const std::string& cache_path) { diff --git a/tests/ceftests/test_util.h b/tests/ceftests/test_util.h index e066bc4fa..8158c3258 100644 --- a/tests/ceftests/test_util.h +++ b/tests/ceftests/test_util.h @@ -6,6 +6,8 @@ #define CEF_TESTS_CEFTESTS_TEST_UTIL_H_ #pragma once +#include + #include "include/cef_process_message.h" #include "include/cef_request.h" #include "include/cef_request_context.h" @@ -96,6 +98,10 @@ bool IsSameSiteBFCacheEnabled(); // Returns true if requests for |url| should be ignored by tests. bool IgnoreURL(const std::string& url); +// Returns |timeout_ms| as scaled by the current configuration, or std::nullopt +// if timeouts are disabled. +std::optional GetConfiguredTestTimeout(int timeout_ms); + // Return a RequestContext object matching the specified |mode|. // |cache_path| may be specified for CUSTOM modes. // Use the RC_TEST_GROUP_BASE macro to test all valid combinations. diff --git a/tests/ceftests/urlrequest_unittest.cc b/tests/ceftests/urlrequest_unittest.cc index 89bbf90ce..22218d45c 100644 --- a/tests/ceftests/urlrequest_unittest.cc +++ b/tests/ceftests/urlrequest_unittest.cc @@ -2760,7 +2760,7 @@ class RequestTestHandler : public TestHandler { void RunTest() override { // Time out the test after a reasonable period of time. - SetTestTimeout(IsChromeRuntimeEnabled() ? 10000 : 5000); + SetTestTimeout(5000); // Start pre-setup actions. PreSetupStart(); diff --git a/tests/ceftests/views/test_window_delegate.cc b/tests/ceftests/views/test_window_delegate.cc index 94d19ec93..6aa1d1c3d 100644 --- a/tests/ceftests/views/test_window_delegate.cc +++ b/tests/ceftests/views/test_window_delegate.cc @@ -8,6 +8,7 @@ #include "include/views/cef_window.h" #include "include/views/cef_window_delegate.h" #include "include/wrapper/cef_closure_task.h" +#include "tests/ceftests/test_util.h" #include "tests/ceftests/thread_helper.h" #include "tests/gtest/include/gtest/gtest.h" @@ -125,14 +126,16 @@ void TestWindowDelegate::OnWindowCreated(CefRefPtr window) { // Close the window asynchronously. CefPostTask(TID_UI, base::BindOnce(&TestWindowDelegate::OnCloseWindow, this)); - } else if (!CefCommandLine::GetGlobalCommandLine()->HasSwitch( - "disable-test-timeout")) { - // Timeout the test after a reasonable delay. Use a WeakPtr so that the - // delayed task doesn't keep this object alive. - CefPostDelayedTask(TID_UI, - base::BindOnce(&TestWindowDelegate::OnTimeoutWindow, - weak_ptr_factory_.GetWeakPtr()), - kTestTimeout); + } else { + const auto timeout = GetConfiguredTestTimeout(kTestTimeout); + if (timeout) { + // Timeout the test after a reasonable delay. Use a WeakPtr so that the + // delayed task doesn't keep this object alive. + CefPostDelayedTask(TID_UI, + base::BindOnce(&TestWindowDelegate::OnTimeoutWindow, + weak_ptr_factory_.GetWeakPtr()), + *timeout); + } } }