Export of internal Abseil changes.
-- 898e99cae89ca4cc27f86f719148f020d521dd58 by Abseil Team <absl-team@google.com>: Update comment. PiperOrigin-RevId: 204323401 -- b9d14db8b8a9dfb0e1cfb5967aaa0de1c4e94c42 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 204178059 -- f3b5a667611a588aa06fea9168e997ef5cffa7ac by Abseil Team <absl-team@google.com>: Fix a potential reinterpret_cast compile error in absl::InlinedVector The current code will trigger a reinterpret_cast error enhanced by llvm r336738. PiperOrigin-RevId: 204131536 -- cc87d7a8302ad4471c1a25781d6ec19c2ce1524e by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 203979040 -- bc5cae3cfc72af1d3e786d5a3b59a47e205afec9 by Gennadiy Rozental <rogeeff@google.com>: Internal: add internal logging hooks PiperOrigin-RevId: 203850605 -- 503655c248f557f677c920078613522b010e73c8 by Derek Mauro <dmauro@google.com>: Cleanup stacktrace_config.h Instead of listing the platforms that aren't supported, list the ones we do support, and fallback to stacktrace_unimplemented-inl.inc at the end. Previously any platform that wasn't listed gets "#error Not supported yet". GitHub issue #135 PiperOrigin-RevId: 203823079 -- cb69925c4cacc14558cf103a09f218c37f466a16 by Abseil Team <absl-team@google.com>: Fix a minor typo in absl::variant documentation. PiperOrigin-RevId: 203679877 -- 23a0e4db10039011fa5fd879fb73d2f2bbd17301 by Abseil Team <absl-team@google.com>: Format .bzl files with buildifier PiperOrigin-RevId: 203461813 -- 1ad02616bdb715dfdc7f1a73313e383f4ededa03 by Abseil Team <absl-team@google.com>: Make the absl::SleepFor() tests treat any successful retry within a 48x deadline as a total success, thereby reducing flakiness. PiperOrigin-RevId: 203401603 -- b3dccbbc6563ea0173527076a3fff21433d48f47 by Abseil Team <absl-team@google.com>: Replace config_setting.values{"compiler"} with config_setting.flag_values{"@bazel_tools//tools/cpp:compiler"} Due to changes in Bazel we need to change the way "compiler" is specified in config_setting. This will not change the behavior of the config_setting itself. PiperOrigin-RevId: 203345693 -- 170f1692537460a4ba1756d34852275899c2339b by Matt Armstrong <marmstrong@google.com>: Address test flakiness in the TimeoutTest. The basic idea is to loop forever until the expected scheduling delays are observed (within reasonable bounds), and only then assert the other invariants. PiperOrigin-RevId: 203188162 GitOrigin-RevId: 898e99cae89ca4cc27f86f719148f020d521dd58 Change-Id: Ie853ec050afa3a04c519393afe666bc03e11dc87
This commit is contained in:
parent
8f612ebb15
commit
02687955b7
15 changed files with 561 additions and 268 deletions
|
@ -18,11 +18,10 @@ package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
licenses(["notice"]) # Apache 2.0
|
licenses(["notice"]) # Apache 2.0
|
||||||
|
|
||||||
config_setting(
|
load(":compiler_config_setting.bzl", "create_llvm_config")
|
||||||
|
|
||||||
|
create_llvm_config(
|
||||||
name = "llvm_compiler",
|
name = "llvm_compiler",
|
||||||
values = {
|
|
||||||
"compiler": "llvm",
|
|
||||||
},
|
|
||||||
visibility = [":__subpackages__"],
|
visibility = [":__subpackages__"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -362,6 +362,7 @@ cc_test(
|
||||||
copts = ABSL_TEST_COPTS,
|
copts = ABSL_TEST_COPTS,
|
||||||
deps = [
|
deps = [
|
||||||
":base",
|
":base",
|
||||||
|
"//absl/strings",
|
||||||
"@com_google_googletest//:gtest_main",
|
"@com_google_googletest//:gtest_main",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
@ -310,7 +310,7 @@ absl_test(
|
||||||
|
|
||||||
# test raw_logging_test
|
# test raw_logging_test
|
||||||
set(RAW_LOGGING_TEST_SRC "raw_logging_test.cc")
|
set(RAW_LOGGING_TEST_SRC "raw_logging_test.cc")
|
||||||
set(RAW_LOGGING_TEST_PUBLIC_LIBRARIES absl::base)
|
set(RAW_LOGGING_TEST_PUBLIC_LIBRARIES absl::base absl::strings)
|
||||||
|
|
||||||
absl_test(
|
absl_test(
|
||||||
TARGET
|
TARGET
|
||||||
|
|
|
@ -206,6 +206,15 @@ void RawLog(absl::LogSeverity severity, const char* file, int line,
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Non-formatting version of RawLog().
|
||||||
|
//
|
||||||
|
// TODO(gfalcon): When string_view no longer depends on base, change this
|
||||||
|
// interface to take its message as a string_view instead.
|
||||||
|
static void DefaultInternalLog(absl::LogSeverity severity, const char* file,
|
||||||
|
int line, const std::string& message) {
|
||||||
|
RawLog(severity, file, line, "%s", message.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
bool RawLoggingFullySupported() {
|
bool RawLoggingFullySupported() {
|
||||||
#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
#ifdef ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||||
return true;
|
return true;
|
||||||
|
@ -214,5 +223,12 @@ bool RawLoggingFullySupported() {
|
||||||
#endif // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
#endif // !ABSL_LOW_LEVEL_WRITE_SUPPORTED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ABSL_CONST_INIT absl::base_internal::AtomicHook<InternalLogFunction>
|
||||||
|
internal_log_function(DefaultInternalLog);
|
||||||
|
|
||||||
|
void RegisterInternalLogFunction(InternalLogFunction func) {
|
||||||
|
internal_log_function.Store(func);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace raw_logging_internal
|
} // namespace raw_logging_internal
|
||||||
} // namespace absl
|
} // namespace absl
|
||||||
|
|
|
@ -19,7 +19,10 @@
|
||||||
#ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
#ifndef ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
||||||
#define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
#define ABSL_BASE_INTERNAL_RAW_LOGGING_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "absl/base/attributes.h"
|
#include "absl/base/attributes.h"
|
||||||
|
#include "absl/base/internal/atomic_hook.h"
|
||||||
#include "absl/base/log_severity.h"
|
#include "absl/base/log_severity.h"
|
||||||
#include "absl/base/macros.h"
|
#include "absl/base/macros.h"
|
||||||
#include "absl/base/port.h"
|
#include "absl/base/port.h"
|
||||||
|
@ -57,6 +60,34 @@
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
// ABSL_INTERNAL_LOG and ABSL_INTERNAL_CHECK work like the RAW variants above,
|
||||||
|
// except that if the richer log library is linked into the binary, we dispatch
|
||||||
|
// to that instead. This is potentially useful for internal logging and
|
||||||
|
// assertions, where we are using RAW_LOG neither for its async-signal-safety
|
||||||
|
// nor for its non-allocating nature, but rather because raw logging has very
|
||||||
|
// few other dependencies.
|
||||||
|
//
|
||||||
|
// The API is a subset of the above: each macro only takes two arguments. Use
|
||||||
|
// StrCat if you need to build a richer message.
|
||||||
|
#define ABSL_INTERNAL_LOG(severity, message) \
|
||||||
|
do { \
|
||||||
|
constexpr const char* absl_raw_logging_internal_basename = \
|
||||||
|
::absl::raw_logging_internal::Basename(__FILE__, \
|
||||||
|
sizeof(__FILE__) - 1); \
|
||||||
|
::absl::raw_logging_internal::internal_log_function( \
|
||||||
|
ABSL_RAW_LOGGING_INTERNAL_##severity, \
|
||||||
|
absl_raw_logging_internal_basename, __LINE__, message); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define ABSL_INTERNAL_CHECK(condition, message) \
|
||||||
|
do { \
|
||||||
|
if (ABSL_PREDICT_FALSE(!(condition))) { \
|
||||||
|
std::string death_message = "Check " #condition " failed: "; \
|
||||||
|
death_message += std::string(message); \
|
||||||
|
ABSL_INTERNAL_LOG(FATAL, death_message); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
|
#define ABSL_RAW_LOGGING_INTERNAL_INFO ::absl::LogSeverity::kInfo
|
||||||
#define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
|
#define ABSL_RAW_LOGGING_INTERNAL_WARNING ::absl::LogSeverity::kWarning
|
||||||
#define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
|
#define ABSL_RAW_LOGGING_INTERNAL_ERROR ::absl::LogSeverity::kError
|
||||||
|
@ -131,6 +162,18 @@ using LogPrefixHook = bool (*)(absl::LogSeverity severity, const char* file,
|
||||||
using AbortHook = void (*)(const char* file, int line, const char* buf_start,
|
using AbortHook = void (*)(const char* file, int line, const char* buf_start,
|
||||||
const char* prefix_end, const char* buf_end);
|
const char* prefix_end, const char* buf_end);
|
||||||
|
|
||||||
|
// Internal logging function for ABSL_INTERNAL_LOG to dispatch to.
|
||||||
|
//
|
||||||
|
// TODO(gfalcon): When string_view no longer depends on base, change this
|
||||||
|
// interface to take its message as a string_view instead.
|
||||||
|
using InternalLogFunction = void (*)(absl::LogSeverity severity,
|
||||||
|
const char* file, int line,
|
||||||
|
const std::string& message);
|
||||||
|
|
||||||
|
extern base_internal::AtomicHook<InternalLogFunction> internal_log_function;
|
||||||
|
|
||||||
|
void RegisterInternalLogFunction(InternalLogFunction func);
|
||||||
|
|
||||||
} // namespace raw_logging_internal
|
} // namespace raw_logging_internal
|
||||||
} // namespace absl
|
} // namespace absl
|
||||||
|
|
||||||
|
|
|
@ -18,12 +18,20 @@
|
||||||
|
|
||||||
#include "absl/base/internal/raw_logging.h"
|
#include "absl/base/internal/raw_logging.h"
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
#include "absl/strings/str_cat.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
TEST(RawLoggingCompilationTest, Log) {
|
TEST(RawLoggingCompilationTest, Log) {
|
||||||
ABSL_RAW_LOG(INFO, "RAW INFO: %d", 1);
|
ABSL_RAW_LOG(INFO, "RAW INFO: %d", 1);
|
||||||
|
ABSL_RAW_LOG(INFO, "RAW INFO: %d %d", 1, 2);
|
||||||
|
ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d", 1, 2, 3);
|
||||||
|
ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d %d", 1, 2, 3, 4);
|
||||||
|
ABSL_RAW_LOG(INFO, "RAW INFO: %d %d %d %d %d", 1, 2, 3, 4, 5);
|
||||||
|
ABSL_RAW_LOG(WARNING, "RAW WARNING: %d", 1);
|
||||||
ABSL_RAW_LOG(ERROR, "RAW ERROR: %d", 1);
|
ABSL_RAW_LOG(ERROR, "RAW ERROR: %d", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,4 +55,25 @@ TEST(RawLoggingDeathTest, LogFatal) {
|
||||||
kExpectedDeathOutput);
|
kExpectedDeathOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(InternalLog, CompilationTest) {
|
||||||
|
ABSL_INTERNAL_LOG(INFO, "Internal Log");
|
||||||
|
std::string log_msg = "Internal Log";
|
||||||
|
ABSL_INTERNAL_LOG(INFO, log_msg);
|
||||||
|
|
||||||
|
ABSL_INTERNAL_LOG(INFO, log_msg + " 2");
|
||||||
|
|
||||||
|
float d = 1.1f;
|
||||||
|
ABSL_INTERNAL_LOG(INFO, absl::StrCat("Internal log ", 3, " + ", d));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(InternalLogDeathTest, FailingCheck) {
|
||||||
|
EXPECT_DEATH_IF_SUPPORTED(ABSL_INTERNAL_CHECK(1 == 0, "explanation"),
|
||||||
|
kExpectedDeathOutput);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(InternalLogDeathTest, LogFatal) {
|
||||||
|
EXPECT_DEATH_IF_SUPPORTED(ABSL_INTERNAL_LOG(FATAL, "my dog has fleas"),
|
||||||
|
kExpectedDeathOutput);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
39
absl/compiler_config_setting.bzl
Normal file
39
absl/compiler_config_setting.bzl
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#
|
||||||
|
# Copyright 2018 The Abseil Authors.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
|
||||||
|
"""Creates config_setting that allows selecting based on 'compiler' value."""
|
||||||
|
|
||||||
|
def create_llvm_config(name, visibility):
|
||||||
|
# The "do_not_use_tools_cpp_compiler_present" attribute exists to
|
||||||
|
# distinguish between older versions of Bazel that do not support
|
||||||
|
# "@bazel_tools//tools/cpp:compiler" flag_value, and newer ones that do.
|
||||||
|
# In the future, the only way to select on the compiler will be through
|
||||||
|
# flag_values{"@bazel_tools//tools/cpp:compiler"} and the else branch can
|
||||||
|
# be removed.
|
||||||
|
if hasattr(cc_common, "do_not_use_tools_cpp_compiler_present"):
|
||||||
|
native.config_setting(
|
||||||
|
name = name,
|
||||||
|
flag_values = {
|
||||||
|
"@bazel_tools//tools/cpp:compiler": "llvm",
|
||||||
|
},
|
||||||
|
visibility = visibility,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
native.config_setting(
|
||||||
|
name = name,
|
||||||
|
values = {"compiler": "llvm"},
|
||||||
|
visibility = visibility,
|
||||||
|
)
|
|
@ -689,11 +689,14 @@ class InlinedVector {
|
||||||
new (&rep_.allocation_storage.allocation) Allocation(allocation);
|
new (&rep_.allocation_storage.allocation) Allocation(allocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(absl-team): investigate whether the reinterpret_cast is appropriate.
|
||||||
value_type* inlined_space() {
|
value_type* inlined_space() {
|
||||||
return reinterpret_cast<value_type*>(&rep_.inlined_storage.inlined);
|
return reinterpret_cast<value_type*>(
|
||||||
|
std::addressof(rep_.inlined_storage.inlined[0]));
|
||||||
}
|
}
|
||||||
const value_type* inlined_space() const {
|
const value_type* inlined_space() const {
|
||||||
return reinterpret_cast<const value_type*>(&rep_.inlined_storage.inlined);
|
return reinterpret_cast<const value_type*>(
|
||||||
|
std::addressof(rep_.inlined_storage.inlined[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
value_type* allocated_space() { return allocation().buffer(); }
|
value_type* allocated_space() { return allocation().buffer(); }
|
||||||
|
|
|
@ -31,7 +31,6 @@ GCC_TEST_FLAGS = [
|
||||||
"-Wno-unused-private-field",
|
"-Wno-unused-private-field",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# Docs on single flags is preceded by a comment.
|
# Docs on single flags is preceded by a comment.
|
||||||
# Docs on groups of flags is preceded by ###.
|
# Docs on groups of flags is preceded by ###.
|
||||||
|
|
||||||
|
|
|
@ -21,26 +21,16 @@
|
||||||
#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
|
#ifndef ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
|
||||||
#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
|
#define ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
|
||||||
|
|
||||||
// First, test platforms which only support a stub.
|
#if defined(ABSL_STACKTRACE_INL_HEADER)
|
||||||
#if ABSL_STACKTRACE_INL_HEADER
|
|
||||||
#error ABSL_STACKTRACE_INL_HEADER cannot be directly set
|
#error ABSL_STACKTRACE_INL_HEADER cannot be directly set
|
||||||
#elif defined(__native_client__) || defined(__APPLE__) || \
|
|
||||||
defined(__FreeBSD__) || defined(__ANDROID__) || defined(__myriad2__) || \
|
|
||||||
defined(__asmjs__) || defined(__wasm__) || defined(__Fuchsia__)
|
|
||||||
#define ABSL_STACKTRACE_INL_HEADER \
|
|
||||||
"absl/debugging/internal/stacktrace_unimplemented-inl.inc"
|
|
||||||
|
|
||||||
// Next, test for Mips and Windows.
|
#elif defined(_WIN32)
|
||||||
// TODO(marmstrong): Mips case, remove the check for ABSL_STACKTRACE_INL_HEADER
|
|
||||||
#elif defined(__mips__) && !defined(ABSL_STACKTRACE_INL_HEADER)
|
|
||||||
#define ABSL_STACKTRACE_INL_HEADER \
|
|
||||||
"absl/debugging/internal/stacktrace_unimplemented-inl.inc"
|
|
||||||
#elif defined(_WIN32) // windows
|
|
||||||
#define ABSL_STACKTRACE_INL_HEADER \
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
"absl/debugging/internal/stacktrace_win32-inl.inc"
|
"absl/debugging/internal/stacktrace_win32-inl.inc"
|
||||||
|
|
||||||
// Finally, test NO_FRAME_POINTER.
|
#elif defined(__linux__) && !defined(__ANDROID__)
|
||||||
#elif !defined(NO_FRAME_POINTER)
|
|
||||||
|
#if !defined(NO_FRAME_POINTER)
|
||||||
# if defined(__i386__) || defined(__x86_64__)
|
# if defined(__i386__) || defined(__x86_64__)
|
||||||
#define ABSL_STACKTRACE_INL_HEADER \
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
"absl/debugging/internal/stacktrace_x86-inl.inc"
|
"absl/debugging/internal/stacktrace_x86-inl.inc"
|
||||||
|
@ -53,22 +43,27 @@
|
||||||
# elif defined(__arm__)
|
# elif defined(__arm__)
|
||||||
#define ABSL_STACKTRACE_INL_HEADER \
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
"absl/debugging/internal/stacktrace_arm-inl.inc"
|
"absl/debugging/internal/stacktrace_arm-inl.inc"
|
||||||
|
# else
|
||||||
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
|
"absl/debugging/internal/stacktrace_unimplemented-inl.inc"
|
||||||
# endif
|
# endif
|
||||||
#else // defined(NO_FRAME_POINTER)
|
#else // defined(NO_FRAME_POINTER)
|
||||||
# if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
|
# if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
|
||||||
#define ABSL_STACKTRACE_INL_HEADER \
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
"absl/debugging/internal/stacktrace_unimplemented-inl.inc"
|
"absl/debugging/internal/stacktrace_generic-inl.inc"
|
||||||
# elif defined(__ppc__) || defined(__PPC__)
|
# elif defined(__ppc__) || defined(__PPC__)
|
||||||
// Use glibc's backtrace.
|
|
||||||
#define ABSL_STACKTRACE_INL_HEADER \
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
"absl/debugging/internal/stacktrace_generic-inl.inc"
|
"absl/debugging/internal/stacktrace_generic-inl.inc"
|
||||||
# elif defined(__arm__)
|
# else
|
||||||
# error stacktrace without frame pointer is not supported on ARM
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
|
"absl/debugging/internal/stacktrace_unimplemented-inl.inc"
|
||||||
# endif
|
# endif
|
||||||
#endif // NO_FRAME_POINTER
|
#endif // NO_FRAME_POINTER
|
||||||
|
|
||||||
#if !defined(ABSL_STACKTRACE_INL_HEADER)
|
#else
|
||||||
#error Not supported yet
|
#define ABSL_STACKTRACE_INL_HEADER \
|
||||||
|
"absl/debugging/internal/stacktrace_unimplemented-inl.inc"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
|
#endif // ABSL_DEBUGGING_INTERNAL_STACKTRACE_CONFIG_H_
|
||||||
|
|
|
@ -384,8 +384,8 @@ TEST(StrFormat, BehavesAsDocumented) {
|
||||||
EXPECT_EQ(StrFormat("%G", 1e10), "1E+10");
|
EXPECT_EQ(StrFormat("%G", 1e10), "1E+10");
|
||||||
// a/A - lower,upper case hex Eg: -3.0 -> "-0x1.8p+1"/"-0X1.8P+1"
|
// a/A - lower,upper case hex Eg: -3.0 -> "-0x1.8p+1"/"-0X1.8P+1"
|
||||||
|
|
||||||
// On NDK r16, there is a regression in hexfloat formatting.
|
// On Android platform <=21, there is a regression in hexfloat formatting.
|
||||||
#if !defined(__NDK_MAJOR__) || __NDK_MAJOR__ != 16
|
#if !defined(__ANDROID_API__) || __ANDROID_API__ > 21
|
||||||
EXPECT_EQ(StrFormat("%.1a", -3.0), "-0x1.8p+1"); // .1 to fix MSVC output
|
EXPECT_EQ(StrFormat("%.1a", -3.0), "-0x1.8p+1"); // .1 to fix MSVC output
|
||||||
EXPECT_EQ(StrFormat("%.1A", -3.0), "-0X1.8P+1"); // .1 to fix MSVC output
|
EXPECT_EQ(StrFormat("%.1A", -3.0), "-0X1.8P+1"); // .1 to fix MSVC output
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
// Unlike a `std::mutex`, the Abseil `Mutex` provides the following additional
|
// Unlike a `std::mutex`, the Abseil `Mutex` provides the following additional
|
||||||
// features:
|
// features:
|
||||||
// * Conditional predicates intrinsic to the `Mutex` object
|
// * Conditional predicates intrinsic to the `Mutex` object
|
||||||
// * Reader/writer locks, in addition to standard exclusive/writer locks
|
// * Shared/reader locks, in addition to standard exclusive/writer locks
|
||||||
// * Deadlock detection and debug support.
|
// * Deadlock detection and debug support.
|
||||||
//
|
//
|
||||||
// The following helper classes are also defined within this file:
|
// The following helper classes are also defined within this file:
|
||||||
|
@ -290,7 +290,7 @@ class LOCKABLE Mutex {
|
||||||
// Mutex::ReaderLockWhen()
|
// Mutex::ReaderLockWhen()
|
||||||
// Mutex::WriterLockWhen()
|
// Mutex::WriterLockWhen()
|
||||||
//
|
//
|
||||||
// Blocks until simultaneously both `cond` is `true` and this` Mutex` can
|
// Blocks until simultaneously both `cond` is `true` and this `Mutex` can
|
||||||
// be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
|
// be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
|
||||||
// logically equivalent to `*Lock(); Await();` though they may have different
|
// logically equivalent to `*Lock(); Await();` though they may have different
|
||||||
// performance characteristics.
|
// performance characteristics.
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
#include "absl/base/attributes.h"
|
||||||
#include "absl/base/internal/raw_logging.h"
|
#include "absl/base/internal/raw_logging.h"
|
||||||
#include "absl/base/internal/sysinfo.h"
|
#include "absl/base/internal/sysinfo.h"
|
||||||
#include "absl/memory/memory.h"
|
#include "absl/memory/memory.h"
|
||||||
|
@ -54,8 +55,8 @@ CreateDefaultPool() {
|
||||||
// Hack to schedule a function to run on a thread pool thread after a
|
// Hack to schedule a function to run on a thread pool thread after a
|
||||||
// duration has elapsed.
|
// duration has elapsed.
|
||||||
static void ScheduleAfter(absl::synchronization_internal::ThreadPool *tp,
|
static void ScheduleAfter(absl::synchronization_internal::ThreadPool *tp,
|
||||||
const std::function<void()> &func,
|
absl::Duration after,
|
||||||
absl::Duration after) {
|
const std::function<void()> &func) {
|
||||||
tp->Schedule([func, after] {
|
tp->Schedule([func, after] {
|
||||||
absl::SleepFor(after);
|
absl::SleepFor(after);
|
||||||
func();
|
func();
|
||||||
|
@ -1150,249 +1151,369 @@ TEST(Mutex, DeadlockIdBug) NO_THREAD_SAFETY_ANALYSIS {
|
||||||
// and so never expires/passes, and one that will expire/pass in the near
|
// and so never expires/passes, and one that will expire/pass in the near
|
||||||
// future.
|
// future.
|
||||||
|
|
||||||
// Encapsulate a Mutex-protected bool with its associated Condition/CondVar.
|
static absl::Duration TimeoutTestAllowedSchedulingDelay() {
|
||||||
class Cond {
|
// Note: we use a function here because Microsoft Visual Studio fails to
|
||||||
public:
|
// properly initialize constexpr static absl::Duration variables.
|
||||||
explicit Cond(bool use_deadline) : use_deadline_(use_deadline), c_(&b_) {}
|
return absl::Milliseconds(150);
|
||||||
|
}
|
||||||
|
|
||||||
void Set(bool v) {
|
// Returns true if `actual_delay` is close enough to `expected_delay` to pass
|
||||||
absl::MutexLock lock(&mu_);
|
// the timeouts/deadlines test. Otherwise, logs warnings and returns false.
|
||||||
b_ = v;
|
ABSL_MUST_USE_RESULT
|
||||||
|
static bool DelayIsWithinBounds(absl::Duration expected_delay,
|
||||||
|
absl::Duration actual_delay) {
|
||||||
|
bool pass = true;
|
||||||
|
// Do not allow the observed delay to be less than expected. This may occur
|
||||||
|
// in practice due to clock skew or when the synchronization primitives use a
|
||||||
|
// different clock than absl::Now(), but these cases should be handled by the
|
||||||
|
// the retry mechanism in each TimeoutTest.
|
||||||
|
if (actual_delay < expected_delay) {
|
||||||
|
ABSL_RAW_LOG(WARNING,
|
||||||
|
"Actual delay %s was too short, expected %s (difference %s)",
|
||||||
|
absl::FormatDuration(actual_delay).c_str(),
|
||||||
|
absl::FormatDuration(expected_delay).c_str(),
|
||||||
|
absl::FormatDuration(actual_delay - expected_delay).c_str());
|
||||||
|
pass = false;
|
||||||
}
|
}
|
||||||
|
// If the expected delay is <= zero then allow a small error tolerance, since
|
||||||
|
// we do not expect context switches to occur during test execution.
|
||||||
|
// Otherwise, thread scheduling delays may be substantial in rare cases, so
|
||||||
|
// tolerate up to kTimeoutTestAllowedSchedulingDelay of error.
|
||||||
|
absl::Duration tolerance = expected_delay <= absl::ZeroDuration()
|
||||||
|
? absl::Milliseconds(10)
|
||||||
|
: TimeoutTestAllowedSchedulingDelay();
|
||||||
|
if (actual_delay > expected_delay + tolerance) {
|
||||||
|
ABSL_RAW_LOG(WARNING,
|
||||||
|
"Actual delay %s was too long, expected %s (difference %s)",
|
||||||
|
absl::FormatDuration(actual_delay).c_str(),
|
||||||
|
absl::FormatDuration(expected_delay).c_str(),
|
||||||
|
absl::FormatDuration(actual_delay - expected_delay).c_str());
|
||||||
|
pass = false;
|
||||||
|
}
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
bool AwaitWithTimeout(absl::Duration timeout) {
|
// Parameters for TimeoutTest, below.
|
||||||
absl::MutexLock lock(&mu_);
|
struct TimeoutTestParam {
|
||||||
return use_deadline_ ? mu_.AwaitWithDeadline(c_, absl::Now() + timeout)
|
// The file and line number (used for logging purposes only).
|
||||||
: mu_.AwaitWithTimeout(c_, timeout);
|
const char *from_file;
|
||||||
}
|
int from_line;
|
||||||
|
|
||||||
bool LockWhenWithTimeout(absl::Duration timeout) {
|
// Should the absolute deadline API based on absl::Time be tested? If false,
|
||||||
bool b = use_deadline_ ? mu_.LockWhenWithDeadline(c_, absl::Now() + timeout)
|
// the relative deadline API based on absl::Duration is tested.
|
||||||
: mu_.LockWhenWithTimeout(c_, timeout);
|
bool use_absolute_deadline;
|
||||||
mu_.Unlock();
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ReaderLockWhenWithTimeout(absl::Duration timeout) {
|
// The deadline/timeout used when calling the API being tested
|
||||||
bool b = use_deadline_
|
// (e.g. Mutex::LockWhenWithDeadline).
|
||||||
? mu_.ReaderLockWhenWithDeadline(c_, absl::Now() + timeout)
|
absl::Duration wait_timeout;
|
||||||
: mu_.ReaderLockWhenWithTimeout(c_, timeout);
|
|
||||||
mu_.ReaderUnlock();
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Await() {
|
// The delay before the condition will be set true by the test code. If zero
|
||||||
absl::MutexLock lock(&mu_);
|
// or negative, the condition is set true immediately (before calling the API
|
||||||
mu_.Await(c_);
|
// being tested). Otherwise, if infinite, the condition is never set true.
|
||||||
}
|
// Otherwise a closure is scheduled for the future that sets the condition
|
||||||
|
// true.
|
||||||
|
absl::Duration satisfy_condition_delay;
|
||||||
|
|
||||||
void Signal(bool v) {
|
// The expected result of the condition after the call to the API being
|
||||||
absl::MutexLock lock(&mu_);
|
// tested. Generally `true` means the condition was true when the API returns,
|
||||||
b_ = v;
|
// `false` indicates an expected timeout.
|
||||||
cv_.Signal();
|
bool expected_result;
|
||||||
}
|
|
||||||
|
|
||||||
bool WaitWithTimeout(absl::Duration timeout) {
|
// The expected delay before the API under test returns. This is inherently
|
||||||
absl::MutexLock lock(&mu_);
|
// flaky, so some slop is allowed (see `DelayIsWithinBounds` above), and the
|
||||||
absl::Time deadline = absl::Now() + timeout;
|
// test keeps trying indefinitely until this constraint passes.
|
||||||
if (use_deadline_) {
|
absl::Duration expected_delay;
|
||||||
while (!b_ && !cv_.WaitWithDeadline(&mu_, deadline)) {
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
while (!b_ && !cv_.WaitWithTimeout(&mu_, timeout)) {
|
|
||||||
timeout = deadline - absl::Now(); // recompute timeout
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return b_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Wait() {
|
|
||||||
absl::MutexLock lock(&mu_);
|
|
||||||
while (!b_) cv_.Wait(&mu_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
const bool use_deadline_;
|
|
||||||
|
|
||||||
bool b_;
|
|
||||||
absl::Condition c_;
|
|
||||||
absl::CondVar cv_;
|
|
||||||
absl::Mutex mu_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class OperationTimer {
|
// Print a `TimeoutTestParam` to a debug log.
|
||||||
public:
|
std::ostream &operator<<(std::ostream &os, const TimeoutTestParam ¶m) {
|
||||||
OperationTimer() : start_(absl::Now()) {}
|
return os << "from: " << param.from_file << ":" << param.from_line
|
||||||
absl::Duration Get() const { return absl::Now() - start_; }
|
<< " use_absolute_deadline: "
|
||||||
|
<< (param.use_absolute_deadline ? "true" : "false")
|
||||||
|
<< " wait_timeout: " << param.wait_timeout
|
||||||
|
<< " satisfy_condition_delay: " << param.satisfy_condition_delay
|
||||||
|
<< " expected_result: "
|
||||||
|
<< (param.expected_result ? "true" : "false")
|
||||||
|
<< " expected_delay: " << param.expected_delay;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
std::string FormatString(const TimeoutTestParam ¶m) {
|
||||||
const absl::Time start_;
|
std::ostringstream os;
|
||||||
};
|
os << param;
|
||||||
|
return os.str();
|
||||||
|
}
|
||||||
|
|
||||||
static void CheckResults(bool exp_result, bool act_result,
|
// Like `thread::Executor::ScheduleAt` except:
|
||||||
absl::Duration exp_duration,
|
// a) Delays zero or negative are executed immediately in the current thread.
|
||||||
absl::Duration act_duration) {
|
// b) Infinite delays are never scheduled.
|
||||||
ABSL_RAW_CHECK(exp_result == act_result, "CheckResults failed");
|
// c) Calls this test's `ScheduleAt` helper instead of using `pool` directly.
|
||||||
// Allow for some worse-case scheduling delay and clock skew.
|
static void RunAfterDelay(absl::Duration delay,
|
||||||
if ((exp_duration - absl::Milliseconds(40) > act_duration) ||
|
absl::synchronization_internal::ThreadPool *pool,
|
||||||
(exp_duration + absl::Milliseconds(150) < act_duration)) {
|
const std::function<void()> &callback) {
|
||||||
ABSL_RAW_LOG(FATAL, "CheckResults failed: operation took %s, expected %s",
|
if (delay <= absl::ZeroDuration()) {
|
||||||
absl::FormatDuration(act_duration).c_str(),
|
callback(); // immediate
|
||||||
absl::FormatDuration(exp_duration).c_str());
|
} else if (delay != absl::InfiniteDuration()) {
|
||||||
|
ScheduleAfter(pool, delay, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TestAwaitTimeout(Cond *cp, absl::Duration timeout, bool exp_result,
|
class TimeoutTest : public ::testing::Test,
|
||||||
absl::Duration exp_duration) {
|
public ::testing::WithParamInterface<TimeoutTestParam> {};
|
||||||
OperationTimer t;
|
|
||||||
bool act_result = cp->AwaitWithTimeout(timeout);
|
|
||||||
CheckResults(exp_result, act_result, exp_duration, t.Get());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void TestLockWhenTimeout(Cond *cp, absl::Duration timeout,
|
std::vector<TimeoutTestParam> MakeTimeoutTestParamValues() {
|
||||||
bool exp_result, absl::Duration exp_duration) {
|
// The `finite` delay is a finite, relatively short, delay. We make it larger
|
||||||
OperationTimer t;
|
// than our allowed scheduling delay (slop factor) to avoid confusion when
|
||||||
bool act_result = cp->LockWhenWithTimeout(timeout);
|
// diagnosing test failures. The other constants here have clear meanings.
|
||||||
CheckResults(exp_result, act_result, exp_duration, t.Get());
|
const absl::Duration finite = 3 * TimeoutTestAllowedSchedulingDelay();
|
||||||
}
|
const absl::Duration never = absl::InfiniteDuration();
|
||||||
|
|
||||||
static void TestReaderLockWhenTimeout(Cond *cp, absl::Duration timeout,
|
|
||||||
bool exp_result,
|
|
||||||
absl::Duration exp_duration) {
|
|
||||||
OperationTimer t;
|
|
||||||
bool act_result = cp->ReaderLockWhenWithTimeout(timeout);
|
|
||||||
CheckResults(exp_result, act_result, exp_duration, t.Get());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void TestWaitTimeout(Cond *cp, absl::Duration timeout, bool exp_result,
|
|
||||||
absl::Duration exp_duration) {
|
|
||||||
OperationTimer t;
|
|
||||||
bool act_result = cp->WaitWithTimeout(timeout);
|
|
||||||
CheckResults(exp_result, act_result, exp_duration, t.Get());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests with a negative timeout (deadline in the past), which should
|
|
||||||
// immediately return the current state of the condition.
|
|
||||||
static void TestNegativeTimeouts(absl::synchronization_internal::ThreadPool *tp,
|
|
||||||
Cond *cp) {
|
|
||||||
const absl::Duration negative = -absl::InfiniteDuration();
|
const absl::Duration negative = -absl::InfiniteDuration();
|
||||||
const absl::Duration immediate = absl::ZeroDuration();
|
const absl::Duration immediate = absl::ZeroDuration();
|
||||||
|
|
||||||
|
// Every test case is run twice; once using the absolute deadline API and once
|
||||||
|
// using the relative timeout API.
|
||||||
|
std::vector<TimeoutTestParam> values;
|
||||||
|
for (bool use_absolute_deadline : {false, true}) {
|
||||||
|
// Tests with a negative timeout (deadline in the past), which should
|
||||||
|
// immediately return current state of the condition.
|
||||||
|
|
||||||
// The condition is already true:
|
// The condition is already true:
|
||||||
cp->Set(true);
|
values.push_back(TimeoutTestParam{
|
||||||
TestAwaitTimeout(cp, negative, true, immediate);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
TestLockWhenTimeout(cp, negative, true, immediate);
|
negative, // wait_timeout
|
||||||
TestReaderLockWhenTimeout(cp, negative, true, immediate);
|
immediate, // satisfy_condition_delay
|
||||||
TestWaitTimeout(cp, negative, true, immediate);
|
true, // expected_result
|
||||||
|
immediate, // expected_delay
|
||||||
|
});
|
||||||
|
|
||||||
// The condition becomes true, but the timeout has already expired:
|
// The condition becomes true, but the timeout has already expired:
|
||||||
const absl::Duration delay = absl::Milliseconds(200);
|
values.push_back(TimeoutTestParam{
|
||||||
cp->Set(false);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), 3 * delay);
|
negative, // wait_timeout
|
||||||
TestAwaitTimeout(cp, negative, false, immediate);
|
finite, // satisfy_condition_delay
|
||||||
TestLockWhenTimeout(cp, negative, false, immediate);
|
false, // expected_result
|
||||||
TestReaderLockWhenTimeout(cp, negative, false, immediate);
|
immediate // expected_delay
|
||||||
cp->Await(); // wait for the scheduled Set() to complete
|
});
|
||||||
cp->Set(false);
|
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay);
|
|
||||||
TestWaitTimeout(cp, negative, false, immediate);
|
|
||||||
cp->Wait(); // wait for the scheduled Signal() to complete
|
|
||||||
|
|
||||||
// The condition never becomes true:
|
// The condition never becomes true:
|
||||||
cp->Set(false);
|
values.push_back(TimeoutTestParam{
|
||||||
TestAwaitTimeout(cp, negative, false, immediate);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
TestLockWhenTimeout(cp, negative, false, immediate);
|
negative, // wait_timeout
|
||||||
TestReaderLockWhenTimeout(cp, negative, false, immediate);
|
never, // satisfy_condition_delay
|
||||||
TestWaitTimeout(cp, negative, false, immediate);
|
false, // expected_result
|
||||||
}
|
immediate // expected_delay
|
||||||
|
});
|
||||||
|
|
||||||
// Tests with an infinite timeout (deadline in the infinite future), which
|
// Tests with an infinite timeout (deadline in the infinite future), which
|
||||||
// should only return when the condition becomes true.
|
// should only return when the condition becomes true.
|
||||||
static void TestInfiniteTimeouts(absl::synchronization_internal::ThreadPool *tp,
|
|
||||||
Cond *cp) {
|
|
||||||
const absl::Duration infinite = absl::InfiniteDuration();
|
|
||||||
const absl::Duration immediate = absl::ZeroDuration();
|
|
||||||
|
|
||||||
// The condition is already true:
|
// The condition is already true:
|
||||||
cp->Set(true);
|
values.push_back(TimeoutTestParam{
|
||||||
TestAwaitTimeout(cp, infinite, true, immediate);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
TestLockWhenTimeout(cp, infinite, true, immediate);
|
never, // wait_timeout
|
||||||
TestReaderLockWhenTimeout(cp, infinite, true, immediate);
|
immediate, // satisfy_condition_delay
|
||||||
TestWaitTimeout(cp, infinite, true, immediate);
|
true, // expected_result
|
||||||
|
immediate // expected_delay
|
||||||
|
});
|
||||||
|
|
||||||
// The condition becomes true before the (infinite) expiry:
|
// The condition becomes true before the (infinite) expiry:
|
||||||
const absl::Duration delay = absl::Milliseconds(200);
|
values.push_back(TimeoutTestParam{
|
||||||
cp->Set(false);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay);
|
never, // wait_timeout
|
||||||
TestAwaitTimeout(cp, infinite, true, delay);
|
finite, // satisfy_condition_delay
|
||||||
cp->Set(false);
|
true, // expected_result
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay);
|
finite, // expected_delay
|
||||||
TestLockWhenTimeout(cp, infinite, true, delay);
|
});
|
||||||
cp->Set(false);
|
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay);
|
|
||||||
TestReaderLockWhenTimeout(cp, infinite, true, delay);
|
|
||||||
cp->Set(false);
|
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay);
|
|
||||||
TestWaitTimeout(cp, infinite, true, delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests with a (small) finite timeout (deadline soon), with the condition
|
// Tests with a (small) finite timeout (deadline soon), with the condition
|
||||||
// becoming true both before and after its expiry.
|
// becoming true both before and after its expiry.
|
||||||
static void TestFiniteTimeouts(absl::synchronization_internal::ThreadPool *tp,
|
|
||||||
Cond *cp) {
|
|
||||||
const absl::Duration finite = absl::Milliseconds(400);
|
|
||||||
const absl::Duration immediate = absl::ZeroDuration();
|
|
||||||
|
|
||||||
// The condition is already true:
|
// The condition is already true:
|
||||||
cp->Set(true);
|
values.push_back(TimeoutTestParam{
|
||||||
TestAwaitTimeout(cp, finite, true, immediate);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
TestLockWhenTimeout(cp, finite, true, immediate);
|
never, // wait_timeout
|
||||||
TestReaderLockWhenTimeout(cp, finite, true, immediate);
|
immediate, // satisfy_condition_delay
|
||||||
TestWaitTimeout(cp, finite, true, immediate);
|
true, // expected_result
|
||||||
|
immediate // expected_delay
|
||||||
|
});
|
||||||
|
|
||||||
// The condition becomes true before the expiry:
|
// The condition becomes true before the expiry:
|
||||||
const absl::Duration delay1 = finite / 2;
|
values.push_back(TimeoutTestParam{
|
||||||
cp->Set(false);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay1);
|
finite * 2, // wait_timeout
|
||||||
TestAwaitTimeout(cp, finite, true, delay1);
|
finite, // satisfy_condition_delay
|
||||||
cp->Set(false);
|
true, // expected_result
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay1);
|
finite // expected_delay
|
||||||
TestLockWhenTimeout(cp, finite, true, delay1);
|
});
|
||||||
cp->Set(false);
|
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), delay1);
|
|
||||||
TestReaderLockWhenTimeout(cp, finite, true, delay1);
|
|
||||||
cp->Set(false);
|
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay1);
|
|
||||||
TestWaitTimeout(cp, finite, true, delay1);
|
|
||||||
|
|
||||||
// The condition becomes true, but the timeout has already expired:
|
// The condition becomes true, but the timeout has already expired:
|
||||||
const absl::Duration delay2 = finite * 2;
|
values.push_back(TimeoutTestParam{
|
||||||
cp->Set(false);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Set, cp, true), 3 * delay2);
|
finite, // wait_timeout
|
||||||
TestAwaitTimeout(cp, finite, false, finite);
|
finite * 2, // satisfy_condition_delay
|
||||||
TestLockWhenTimeout(cp, finite, false, finite);
|
false, // expected_result
|
||||||
TestReaderLockWhenTimeout(cp, finite, false, finite);
|
finite // expected_delay
|
||||||
cp->Await(); // wait for the scheduled Set() to complete
|
});
|
||||||
cp->Set(false);
|
|
||||||
ScheduleAfter(tp, std::bind(&Cond::Signal, cp, true), delay2);
|
|
||||||
TestWaitTimeout(cp, finite, false, finite);
|
|
||||||
cp->Wait(); // wait for the scheduled Signal() to complete
|
|
||||||
|
|
||||||
// The condition never becomes true:
|
// The condition never becomes true:
|
||||||
cp->Set(false);
|
values.push_back(TimeoutTestParam{
|
||||||
TestAwaitTimeout(cp, finite, false, finite);
|
__FILE__, __LINE__, use_absolute_deadline,
|
||||||
TestLockWhenTimeout(cp, finite, false, finite);
|
finite, // wait_timeout
|
||||||
TestReaderLockWhenTimeout(cp, finite, false, finite);
|
never, // satisfy_condition_delay
|
||||||
TestWaitTimeout(cp, finite, false, finite);
|
false, // expected_result
|
||||||
|
finite // expected_delay
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Mutex, Timeouts) {
|
// Instantiate `TimeoutTest` with `MakeTimeoutTestParamValues()`.
|
||||||
auto tp = CreateDefaultPool();
|
INSTANTIATE_TEST_CASE_P(All, TimeoutTest,
|
||||||
for (bool use_deadline : {false, true}) {
|
testing::ValuesIn(MakeTimeoutTestParamValues()));
|
||||||
Cond cond(use_deadline);
|
|
||||||
TestNegativeTimeouts(tp.get(), &cond);
|
TEST_P(TimeoutTest, Await) {
|
||||||
TestInfiniteTimeouts(tp.get(), &cond);
|
const TimeoutTestParam params = GetParam();
|
||||||
TestFiniteTimeouts(tp.get(), &cond);
|
ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str());
|
||||||
|
|
||||||
|
// Because this test asserts bounds on scheduling delays it is flaky. To
|
||||||
|
// compensate it loops forever until it passes. Failures express as test
|
||||||
|
// timeouts, in which case the test log can be used to diagnose the issue.
|
||||||
|
for (int attempt = 1;; ++attempt) {
|
||||||
|
ABSL_RAW_LOG(INFO, "Attempt %d", attempt);
|
||||||
|
|
||||||
|
absl::Mutex mu;
|
||||||
|
bool value = false; // condition value (under mu)
|
||||||
|
|
||||||
|
std::unique_ptr<absl::synchronization_internal::ThreadPool> pool =
|
||||||
|
CreateDefaultPool();
|
||||||
|
RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] {
|
||||||
|
absl::MutexLock l(&mu);
|
||||||
|
value = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
absl::MutexLock lock(&mu);
|
||||||
|
absl::Time start_time = absl::Now();
|
||||||
|
absl::Condition cond(&value);
|
||||||
|
bool result =
|
||||||
|
params.use_absolute_deadline
|
||||||
|
? mu.AwaitWithDeadline(cond, start_time + params.wait_timeout)
|
||||||
|
: mu.AwaitWithTimeout(cond, params.wait_timeout);
|
||||||
|
if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) {
|
||||||
|
EXPECT_EQ(params.expected_result, result);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(TimeoutTest, LockWhen) {
|
||||||
|
const TimeoutTestParam params = GetParam();
|
||||||
|
ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str());
|
||||||
|
|
||||||
|
// Because this test asserts bounds on scheduling delays it is flaky. To
|
||||||
|
// compensate it loops forever until it passes. Failures express as test
|
||||||
|
// timeouts, in which case the test log can be used to diagnose the issue.
|
||||||
|
for (int attempt = 1;; ++attempt) {
|
||||||
|
ABSL_RAW_LOG(INFO, "Attempt %d", attempt);
|
||||||
|
|
||||||
|
absl::Mutex mu;
|
||||||
|
bool value = false; // condition value (under mu)
|
||||||
|
|
||||||
|
std::unique_ptr<absl::synchronization_internal::ThreadPool> pool =
|
||||||
|
CreateDefaultPool();
|
||||||
|
RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] {
|
||||||
|
absl::MutexLock l(&mu);
|
||||||
|
value = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
absl::Time start_time = absl::Now();
|
||||||
|
absl::Condition cond(&value);
|
||||||
|
bool result =
|
||||||
|
params.use_absolute_deadline
|
||||||
|
? mu.LockWhenWithDeadline(cond, start_time + params.wait_timeout)
|
||||||
|
: mu.LockWhenWithTimeout(cond, params.wait_timeout);
|
||||||
|
mu.Unlock();
|
||||||
|
|
||||||
|
if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) {
|
||||||
|
EXPECT_EQ(params.expected_result, result);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(TimeoutTest, ReaderLockWhen) {
|
||||||
|
const TimeoutTestParam params = GetParam();
|
||||||
|
ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str());
|
||||||
|
|
||||||
|
// Because this test asserts bounds on scheduling delays it is flaky. To
|
||||||
|
// compensate it loops forever until it passes. Failures express as test
|
||||||
|
// timeouts, in which case the test log can be used to diagnose the issue.
|
||||||
|
for (int attempt = 0;; ++attempt) {
|
||||||
|
ABSL_RAW_LOG(INFO, "Attempt %d", attempt);
|
||||||
|
|
||||||
|
absl::Mutex mu;
|
||||||
|
bool value = false; // condition value (under mu)
|
||||||
|
|
||||||
|
std::unique_ptr<absl::synchronization_internal::ThreadPool> pool =
|
||||||
|
CreateDefaultPool();
|
||||||
|
RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] {
|
||||||
|
absl::MutexLock l(&mu);
|
||||||
|
value = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
absl::Time start_time = absl::Now();
|
||||||
|
bool result =
|
||||||
|
params.use_absolute_deadline
|
||||||
|
? mu.ReaderLockWhenWithDeadline(absl::Condition(&value),
|
||||||
|
start_time + params.wait_timeout)
|
||||||
|
: mu.ReaderLockWhenWithTimeout(absl::Condition(&value),
|
||||||
|
params.wait_timeout);
|
||||||
|
mu.ReaderUnlock();
|
||||||
|
|
||||||
|
if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) {
|
||||||
|
EXPECT_EQ(params.expected_result, result);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_P(TimeoutTest, Wait) {
|
||||||
|
const TimeoutTestParam params = GetParam();
|
||||||
|
ABSL_RAW_LOG(INFO, "Params: %s", FormatString(params).c_str());
|
||||||
|
|
||||||
|
// Because this test asserts bounds on scheduling delays it is flaky. To
|
||||||
|
// compensate it loops forever until it passes. Failures express as test
|
||||||
|
// timeouts, in which case the test log can be used to diagnose the issue.
|
||||||
|
for (int attempt = 0;; ++attempt) {
|
||||||
|
ABSL_RAW_LOG(INFO, "Attempt %d", attempt);
|
||||||
|
|
||||||
|
absl::Mutex mu;
|
||||||
|
bool value = false; // condition value (under mu)
|
||||||
|
absl::CondVar cv; // signals a change of `value`
|
||||||
|
|
||||||
|
std::unique_ptr<absl::synchronization_internal::ThreadPool> pool =
|
||||||
|
CreateDefaultPool();
|
||||||
|
RunAfterDelay(params.satisfy_condition_delay, pool.get(), [&] {
|
||||||
|
absl::MutexLock l(&mu);
|
||||||
|
value = true;
|
||||||
|
cv.Signal();
|
||||||
|
});
|
||||||
|
|
||||||
|
absl::MutexLock lock(&mu);
|
||||||
|
absl::Time start_time = absl::Now();
|
||||||
|
absl::Duration timeout = params.wait_timeout;
|
||||||
|
absl::Time deadline = start_time + timeout;
|
||||||
|
while (!value) {
|
||||||
|
if (params.use_absolute_deadline ? cv.WaitWithDeadline(&mu, deadline)
|
||||||
|
: cv.WaitWithTimeout(&mu, timeout)) {
|
||||||
|
break; // deadline/timeout exceeded
|
||||||
|
}
|
||||||
|
timeout = deadline - absl::Now(); // recompute
|
||||||
|
}
|
||||||
|
bool result = value; // note: `mu` is still held
|
||||||
|
|
||||||
|
if (DelayIsWithinBounds(params.expected_delay, absl::Now() - start_time)) {
|
||||||
|
EXPECT_EQ(params.expected_result, result);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,36 +35,84 @@ TEST(Time, Now) {
|
||||||
EXPECT_GE(after, now);
|
EXPECT_GE(after, now);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SleepForTest, BasicSanity) {
|
enum class AlarmPolicy { kWithoutAlarm, kWithAlarm };
|
||||||
absl::Duration sleep_time = absl::Milliseconds(2500);
|
|
||||||
absl::Time start = absl::Now();
|
|
||||||
absl::SleepFor(sleep_time);
|
|
||||||
absl::Time end = absl::Now();
|
|
||||||
EXPECT_LE(sleep_time - absl::Milliseconds(100), end - start);
|
|
||||||
EXPECT_GE(sleep_time + absl::Milliseconds(200), end - start);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef ABSL_HAVE_ALARM
|
#if defined(ABSL_HAVE_ALARM)
|
||||||
// Helper for test SleepFor.
|
|
||||||
bool alarm_handler_invoked = false;
|
bool alarm_handler_invoked = false;
|
||||||
|
|
||||||
void AlarmHandler(int signo) {
|
void AlarmHandler(int signo) {
|
||||||
ASSERT_EQ(signo, SIGALRM);
|
ASSERT_EQ(signo, SIGALRM);
|
||||||
alarm_handler_invoked = true;
|
alarm_handler_invoked = true;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST(SleepForTest, AlarmSupport) {
|
// Does SleepFor(d) take between lower_bound and upper_bound at least
|
||||||
|
// once between now and (now + timeout)? If requested (and supported),
|
||||||
|
// add an alarm for the middle of the sleep period and expect it to fire.
|
||||||
|
bool SleepForBounded(absl::Duration d, absl::Duration lower_bound,
|
||||||
|
absl::Duration upper_bound, absl::Duration timeout,
|
||||||
|
AlarmPolicy alarm_policy, int* attempts) {
|
||||||
|
const absl::Time deadline = absl::Now() + timeout;
|
||||||
|
while (absl::Now() < deadline) {
|
||||||
|
#if defined(ABSL_HAVE_ALARM)
|
||||||
|
sig_t old_alarm = SIG_DFL;
|
||||||
|
if (alarm_policy == AlarmPolicy::kWithAlarm) {
|
||||||
alarm_handler_invoked = false;
|
alarm_handler_invoked = false;
|
||||||
sig_t old_alarm = signal(SIGALRM, AlarmHandler);
|
old_alarm = signal(SIGALRM, AlarmHandler);
|
||||||
alarm(2);
|
alarm(absl::ToInt64Seconds(d / 2));
|
||||||
absl::Duration sleep_time = absl::Milliseconds(3500);
|
}
|
||||||
|
#else
|
||||||
|
EXPECT_EQ(alarm_policy, AlarmPolicy::kWithoutAlarm);
|
||||||
|
#endif
|
||||||
|
++*attempts;
|
||||||
absl::Time start = absl::Now();
|
absl::Time start = absl::Now();
|
||||||
absl::SleepFor(sleep_time);
|
absl::SleepFor(d);
|
||||||
absl::Time end = absl::Now();
|
absl::Duration actual = absl::Now() - start;
|
||||||
EXPECT_TRUE(alarm_handler_invoked);
|
#if defined(ABSL_HAVE_ALARM)
|
||||||
EXPECT_LE(sleep_time - absl::Milliseconds(100), end - start);
|
if (alarm_policy == AlarmPolicy::kWithAlarm) {
|
||||||
EXPECT_GE(sleep_time + absl::Milliseconds(200), end - start);
|
|
||||||
signal(SIGALRM, old_alarm);
|
signal(SIGALRM, old_alarm);
|
||||||
|
if (!alarm_handler_invoked) continue;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (lower_bound <= actual && actual <= upper_bound) {
|
||||||
|
return true; // yes, the SleepFor() was correctly bounded
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
testing::AssertionResult AssertSleepForBounded(absl::Duration d,
|
||||||
|
absl::Duration early,
|
||||||
|
absl::Duration late,
|
||||||
|
absl::Duration timeout,
|
||||||
|
AlarmPolicy alarm_policy) {
|
||||||
|
const absl::Duration lower_bound = d - early;
|
||||||
|
const absl::Duration upper_bound = d + late;
|
||||||
|
int attempts = 0;
|
||||||
|
if (SleepForBounded(d, lower_bound, upper_bound, timeout, alarm_policy,
|
||||||
|
&attempts)) {
|
||||||
|
return testing::AssertionSuccess();
|
||||||
|
}
|
||||||
|
return testing::AssertionFailure()
|
||||||
|
<< "SleepFor(" << d << ") did not return within [" << lower_bound
|
||||||
|
<< ":" << upper_bound << "] in " << attempts << " attempt"
|
||||||
|
<< (attempts == 1 ? "" : "s") << " over " << timeout
|
||||||
|
<< (alarm_policy == AlarmPolicy::kWithAlarm ? " with" : " without")
|
||||||
|
<< " an alarm";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that SleepFor() returns neither too early nor too late.
|
||||||
|
TEST(SleepFor, Bounded) {
|
||||||
|
const absl::Duration d = absl::Milliseconds(2500);
|
||||||
|
const absl::Duration early = absl::Milliseconds(100);
|
||||||
|
const absl::Duration late = absl::Milliseconds(300);
|
||||||
|
const absl::Duration timeout = 48 * d;
|
||||||
|
EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout,
|
||||||
|
AlarmPolicy::kWithoutAlarm));
|
||||||
|
#if defined(ABSL_HAVE_ALARM)
|
||||||
|
EXPECT_TRUE(AssertSleepForBounded(d, early, late, timeout,
|
||||||
|
AlarmPolicy::kWithAlarm));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif // ABSL_HAVE_ALARM
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -248,7 +248,7 @@ using variant_alternative_t = typename variant_alternative<I, T>::type;
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// absl::variant<int, std::string> bar = 42;
|
// absl::variant<int, std::string> foo = 42;
|
||||||
// if (absl::holds_alternative<int>(foo)) {
|
// if (absl::holds_alternative<int>(foo)) {
|
||||||
// std::cout << "The variant holds an integer";
|
// std::cout << "The variant holds an integer";
|
||||||
// }
|
// }
|
||||||
|
|
Loading…
Reference in a new issue