webrtc_m130/api/sequence_checker_unittest.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

175 lines
5.0 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "api/sequence_checker.h"
#include <memory>
#include <utility>
#include "api/function_view.h"
#include "rtc_base/event.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/task_queue_for_test.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
// This class is dead code, but its purpose is to make sure that
// SequenceChecker is compatible with the RTC_GUARDED_BY and RTC_RUN_ON
// attributes that are checked at compile-time.
class CompileTimeTestForGuardedBy {
public:
int CalledOnSequence() RTC_RUN_ON(sequence_checker_) { return guarded_; }
void CallMeFromSequence() {
RTC_DCHECK_RUN_ON(&sequence_checker_);
guarded_ = 41;
}
private:
int guarded_ RTC_GUARDED_BY(sequence_checker_);
::webrtc::SequenceChecker sequence_checker_;
};
void RunOnDifferentThread(rtc::FunctionView<void()> run) {
Revert "Refactor the PlatformThread API." This reverts commit c89fdd716c4c8af608017c76f75bf27e4c3d602e. Reason for revert: Causes rare compilation error on win-libfuzzer-asan trybot. See https://ci.chromium.org/p/chromium/builders/try/win-libfuzzer-asan-rel/713745? Original change's description: > Refactor the PlatformThread API. > > PlatformThread's API is using old style function pointers, causes > casting, is unintuitive and forces artificial call sequences, and > is additionally possible to misuse in release mode. > > Fix this by an API face lift: > 1. The class is turned into a handle, which can be empty. > 2. The only way of getting a non-empty PlatformThread is by calling > SpawnJoinable or SpawnDetached, clearly conveying the semantics to the > code reader. > 3. Handles can be Finalized, which works differently for joinable and > detached threads: > a) Handles for detached threads are simply closed where applicable. > b) Joinable threads are joined before handles are closed. > 4. The destructor finalizes handles. No explicit call is needed. > > Fixed: webrtc:12727 > Change-Id: Id00a0464edf4fc9e552b6a1fbb5d2e1280e88811 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/215075 > Commit-Queue: Markus Handell <handellm@webrtc.org> > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> > Reviewed-by: Tommi <tommi@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#33923} # Not skipping CQ checks because original CL landed > 1 day ago. TBR=handellm@webrtc.org Bug: webrtc:12727 Change-Id: Ic0146be8866f6dd3ad9c364fb8646650b8e07419 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/217583 Reviewed-by: Guido Urdaneta <guidou@webrtc.org> Reviewed-by: Markus Handell <handellm@webrtc.org> Commit-Queue: Guido Urdaneta <guidou@webrtc.org> Cr-Commit-Position: refs/heads/master@{#33936}
2021-05-06 13:12:47 +00:00
struct Object {
static void Run(void* obj) {
auto* me = static_cast<Object*>(obj);
me->run();
me->thread_has_run_event.Set();
}
rtc::FunctionView<void()> run;
rtc::Event thread_has_run_event;
} object{run};
rtc::PlatformThread thread(&Object::Run, &object, "thread");
thread.Start();
EXPECT_TRUE(object.thread_has_run_event.Wait(1000));
thread.Stop();
}
} // namespace
TEST(SequenceCheckerTest, CallsAllowedOnSameThread) {
SequenceChecker sequence_checker;
EXPECT_TRUE(sequence_checker.IsCurrent());
}
TEST(SequenceCheckerTest, DestructorAllowedOnDifferentThread) {
Use std::make_unique instead of absl::make_unique. WebRTC is now using C++14 so there is no need to use the Abseil version of std::make_unique. This CL has been created with the following steps: git grep -l absl::make_unique | sort | uniq > /tmp/make_unique.txt git grep -l absl::WrapUnique | sort | uniq > /tmp/wrap_unique.txt git grep -l "#include <memory>" | sort | uniq > /tmp/memory.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/make_unique.txt /tmp/wrap_unique.txt | sort | \ uniq > /tmp/only_make_unique.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/only_make_unique.txt /tmp/memory.txt | \ xargs grep -l "absl/memory" > /tmp/add-memory.txt git grep -l "\babsl::make_unique\b" | \ xargs sed -i "s/\babsl::make_unique\b/std::make_unique/g" git checkout PRESUBMIT.py abseil-in-webrtc.md cat /tmp/add-memory.txt | \ xargs sed -i \ 's/#include "absl\/memory\/memory.h"/#include <memory>/g' git cl format # Manual fix order of the new inserted #include <memory> cat /tmp/only_make_unique | xargs grep -l "#include <memory>" | \ xargs sed -i '/#include "absl\/memory\/memory.h"/d' git ls-files | grep BUILD.gn | \ xargs sed -i '/\/\/third_party\/abseil-cpp\/absl\/memory/d' python tools_webrtc/gn_check_autofix.py \ -m tryserver.webrtc -b linux_rel # Repead the gn_check_autofix step for other platforms git ls-files | grep BUILD.gn | \ xargs sed -i 's/absl\/memory:memory/absl\/memory/g' git cl format Bug: webrtc:10945 Change-Id: I3fe28ea80f4dd3ba3cf28effd151d5e1f19aff89 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153221 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29209}
2019-09-17 17:06:18 +02:00
auto sequence_checker = std::make_unique<SequenceChecker>();
RunOnDifferentThread([&] {
// Verify that the destructor doesn't assert when called on a different
// thread.
sequence_checker.reset();
});
}
TEST(SequenceCheckerTest, Detach) {
SequenceChecker sequence_checker;
sequence_checker.Detach();
RunOnDifferentThread([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); });
}
TEST(SequenceCheckerTest, DetachFromThreadAndUseOnTaskQueue) {
SequenceChecker sequence_checker;
sequence_checker.Detach();
TaskQueueForTest queue;
queue.SendTask([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); },
RTC_FROM_HERE);
}
TEST(SequenceCheckerTest, DetachFromTaskQueueAndUseOnThread) {
TaskQueueForTest queue;
queue.SendTask(
[] {
SequenceChecker sequence_checker;
sequence_checker.Detach();
RunOnDifferentThread(
[&] { EXPECT_TRUE(sequence_checker.IsCurrent()); });
},
RTC_FROM_HERE);
}
TEST(SequenceCheckerTest, MethodNotAllowedOnDifferentThreadInDebug) {
SequenceChecker sequence_checker;
RunOnDifferentThread(
[&] { EXPECT_EQ(sequence_checker.IsCurrent(), !RTC_DCHECK_IS_ON); });
}
TEST(SequenceCheckerTest, MethodNotAllowedOnDifferentTaskQueueInDebug) {
SequenceChecker sequence_checker;
TaskQueueForTest queue;
queue.SendTask(
[&] { EXPECT_EQ(sequence_checker.IsCurrent(), !RTC_DCHECK_IS_ON); },
RTC_FROM_HERE);
}
TEST(SequenceCheckerTest, DetachFromTaskQueueInDebug) {
SequenceChecker sequence_checker;
sequence_checker.Detach();
TaskQueueForTest queue1;
queue1.SendTask([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); },
RTC_FROM_HERE);
// IsCurrent should return false in debug builds after moving to
// another task queue.
TaskQueueForTest queue2;
queue2.SendTask(
[&] { EXPECT_EQ(sequence_checker.IsCurrent(), !RTC_DCHECK_IS_ON); },
RTC_FROM_HERE);
}
class TestAnnotations {
public:
TestAnnotations() : test_var_(false) {}
void ModifyTestVar() {
RTC_DCHECK_RUN_ON(&checker_);
test_var_ = true;
}
private:
bool test_var_ RTC_GUARDED_BY(&checker_);
SequenceChecker checker_;
};
TEST(SequenceCheckerTest, TestAnnotations) {
TestAnnotations annotations;
annotations.ModifyTestVar();
}
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
void TestAnnotationsOnWrongQueue() {
TestAnnotations annotations;
TaskQueueForTest queue;
queue.SendTask([&] { annotations.ModifyTestVar(); }, RTC_FROM_HERE);
}
#if RTC_DCHECK_IS_ON
// Note: Ending the test suite name with 'DeathTest' is important as it causes
// gtest to order this test before any other non-death-tests, to avoid potential
// global process state pollution such as shared worker threads being started
// (e.g. a side effect of calling InitCocoaMultiThreading() on Mac causes one or
// two additional threads to be created).
TEST(SequenceCheckerDeathTest, TestAnnotationsOnWrongQueueDebug) {
ASSERT_DEATH({ TestAnnotationsOnWrongQueue(); }, "");
}
#else
TEST(SequenceCheckerTest, TestAnnotationsOnWrongQueueRelease) {
TestAnnotationsOnWrongQueue();
}
#endif
#endif // GTEST_HAS_DEATH_TEST
} // namespace webrtc