Skip to content

Commit

Permalink
ceftests: Add support for scaling default test timeout values
Browse files Browse the repository at this point in the history
  • Loading branch information
magreenblatt committed Jan 13, 2023
1 parent 2b906c3 commit b065ca8
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 51 deletions.
2 changes: 1 addition & 1 deletion tests/ceftests/resource_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ResourceManagerTestHandler : public RoutingTestHandler {
// Create the browser.
CreateBrowser(GetNextURL());

SetTestTimeout(IsChromeRuntimeEnabled() ? 10000 : 5000);
SetTestTimeout(5000);
}

cef_return_value_t OnBeforeResourceLoad(
Expand Down
8 changes: 4 additions & 4 deletions tests/ceftests/server_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -651,17 +651,17 @@ class HttpTestRunner : public base::RefCountedThreadSafe<HttpTestRunner> {

void SetTestTimeout(int timeout_ms) {
EXPECT_UI_THREAD();
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch(
"disable-test-timeout")) {
const auto timeout = GetConfiguredTestTimeout(timeout_ms);
if (!timeout) {
return;
}

// Use a weak reference to |this| via UIThreadHelper so that the
// 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) {
Expand Down
7 changes: 4 additions & 3 deletions tests/ceftests/task_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<CefWaitableEvent> 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));
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/ceftests/test_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -442,17 +443,17 @@ 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;
}

// Use a weak reference to |this| via UIThreadHelper so that the TestHandler
// 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() {
Expand Down
7 changes: 4 additions & 3 deletions tests/ceftests/test_server_observer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -165,11 +166,11 @@ void SignalIfDone(CefRefPtr<CefWaitableEvent> event,
}

void Wait(CefRefPtr<CefWaitableEvent> 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);
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/ceftests/test_server_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,17 @@ class HttpTestRunner : public base::RefCountedThreadSafe<HttpTestRunner> {

void SetTestTimeout(int timeout_ms) {
EXPECT_UI_THREAD();
if (CefCommandLine::GetGlobalCommandLine()->HasSwitch(
"disable-test-timeout")) {
const auto timeout = GetConfiguredTestTimeout(timeout_ms);
if (!timeout) {
return;
}

// Use a weak reference to |this| via UIThreadHelper so that the
// 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) {
Expand Down
67 changes: 44 additions & 23 deletions tests/ceftests/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "tests/ceftests/test_util.h"

#include <algorithm>
#include <cmath>
#include <cstdlib>

#include "include/cef_base.h"
#include "include/cef_command_line.h"
Expand Down Expand Up @@ -278,23 +280,19 @@ void TestStringVectorEqual(const std::vector<CefString>& val1,
}

bool TestOldResourceAPI() {
static int state = -1;
if (state == -1) {
CefRefPtr<CefCommandLine> 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<CefCommandLine> 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() {
Expand All @@ -303,15 +301,13 @@ bool IsBFCacheEnabled() {
return false;
}

// Enabled by default starting in M96.
static int state = -1;
if (state == -1) {
CefRefPtr<CefCommandLine> 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() {
Expand All @@ -325,6 +321,31 @@ bool IgnoreURL(const std::string& url) {
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(
TestRequestContextMode mode,
const std::string& cache_path) {
Expand Down
6 changes: 6 additions & 0 deletions tests/ceftests/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define CEF_TESTS_CEFTESTS_TEST_UTIL_H_
#pragma once

#include <optional>

#include "include/cef_process_message.h"
#include "include/cef_request.h"
#include "include/cef_request_context.h"
Expand Down Expand Up @@ -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<int> 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.
Expand Down
2 changes: 1 addition & 1 deletion tests/ceftests/urlrequest_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 11 additions & 8 deletions tests/ceftests/views/test_window_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -125,14 +126,16 @@ void TestWindowDelegate::OnWindowCreated(CefRefPtr<CefWindow> 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);
}
}
}

Expand Down

0 comments on commit b065ca8

Please sign in to comment.