Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Artem Titov | b586d82 | 2021-02-04 14:06:50 | [diff] [blame] | 11 | #include "api/sequence_checker.h" |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 16 | #include "api/function_view.h" |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 | [diff] [blame] | 17 | #include "api/units/time_delta.h" |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 18 | #include "rtc_base/event.h" |
| 19 | #include "rtc_base/platform_thread.h" |
| 20 | #include "rtc_base/task_queue_for_test.h" |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 21 | #include "test/gtest.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | namespace { |
| 25 | |
| 26 | // This class is dead code, but its purpose is to make sure that |
| 27 | // SequenceChecker is compatible with the RTC_GUARDED_BY and RTC_RUN_ON |
| 28 | // attributes that are checked at compile-time. |
| 29 | class CompileTimeTestForGuardedBy { |
| 30 | public: |
| 31 | int CalledOnSequence() RTC_RUN_ON(sequence_checker_) { return guarded_; } |
| 32 | |
| 33 | void CallMeFromSequence() { |
Tomas Gunnarsson | 4d177eb | 2020-06-08 21:08:46 | [diff] [blame] | 34 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 35 | guarded_ = 41; |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | int guarded_ RTC_GUARDED_BY(sequence_checker_); |
| 40 | ::webrtc::SequenceChecker sequence_checker_; |
| 41 | }; |
| 42 | |
| 43 | void RunOnDifferentThread(rtc::FunctionView<void()> run) { |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 44 | rtc::Event thread_has_run_event; |
| 45 | rtc::PlatformThread::SpawnJoinable( |
| 46 | [&] { |
| 47 | run(); |
| 48 | thread_has_run_event.Set(); |
| 49 | }, |
| 50 | "thread"); |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 | [diff] [blame] | 51 | EXPECT_TRUE(thread_has_run_event.Wait(TimeDelta::Seconds(1))); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
| 56 | TEST(SequenceCheckerTest, CallsAllowedOnSameThread) { |
| 57 | SequenceChecker sequence_checker; |
| 58 | EXPECT_TRUE(sequence_checker.IsCurrent()); |
| 59 | } |
| 60 | |
| 61 | TEST(SequenceCheckerTest, DestructorAllowedOnDifferentThread) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 62 | auto sequence_checker = std::make_unique<SequenceChecker>(); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 63 | RunOnDifferentThread([&] { |
| 64 | // Verify that the destructor doesn't assert when called on a different |
| 65 | // thread. |
| 66 | sequence_checker.reset(); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | TEST(SequenceCheckerTest, Detach) { |
| 71 | SequenceChecker sequence_checker; |
| 72 | sequence_checker.Detach(); |
| 73 | RunOnDifferentThread([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); }); |
| 74 | } |
| 75 | |
| 76 | TEST(SequenceCheckerTest, DetachFromThreadAndUseOnTaskQueue) { |
| 77 | SequenceChecker sequence_checker; |
| 78 | sequence_checker.Detach(); |
| 79 | TaskQueueForTest queue; |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 80 | queue.SendTask([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); }); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | TEST(SequenceCheckerTest, DetachFromTaskQueueAndUseOnThread) { |
| 84 | TaskQueueForTest queue; |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 85 | queue.SendTask([] { |
| 86 | SequenceChecker sequence_checker; |
| 87 | sequence_checker.Detach(); |
| 88 | RunOnDifferentThread([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); }); |
| 89 | }); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST(SequenceCheckerTest, MethodNotAllowedOnDifferentThreadInDebug) { |
| 93 | SequenceChecker sequence_checker; |
| 94 | RunOnDifferentThread( |
| 95 | [&] { EXPECT_EQ(sequence_checker.IsCurrent(), !RTC_DCHECK_IS_ON); }); |
| 96 | } |
| 97 | |
| 98 | TEST(SequenceCheckerTest, MethodNotAllowedOnDifferentTaskQueueInDebug) { |
| 99 | SequenceChecker sequence_checker; |
| 100 | TaskQueueForTest queue; |
| 101 | queue.SendTask( |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 102 | [&] { EXPECT_EQ(sequence_checker.IsCurrent(), !RTC_DCHECK_IS_ON); }); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST(SequenceCheckerTest, DetachFromTaskQueueInDebug) { |
| 106 | SequenceChecker sequence_checker; |
| 107 | sequence_checker.Detach(); |
| 108 | |
| 109 | TaskQueueForTest queue1; |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 110 | queue1.SendTask([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); }); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 111 | |
| 112 | // IsCurrent should return false in debug builds after moving to |
| 113 | // another task queue. |
| 114 | TaskQueueForTest queue2; |
| 115 | queue2.SendTask( |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 116 | [&] { EXPECT_EQ(sequence_checker.IsCurrent(), !RTC_DCHECK_IS_ON); }); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | class TestAnnotations { |
| 120 | public: |
| 121 | TestAnnotations() : test_var_(false) {} |
| 122 | |
| 123 | void ModifyTestVar() { |
| 124 | RTC_DCHECK_RUN_ON(&checker_); |
| 125 | test_var_ = true; |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | bool test_var_ RTC_GUARDED_BY(&checker_); |
| 130 | SequenceChecker checker_; |
| 131 | }; |
| 132 | |
| 133 | TEST(SequenceCheckerTest, TestAnnotations) { |
| 134 | TestAnnotations annotations; |
| 135 | annotations.ModifyTestVar(); |
| 136 | } |
| 137 | |
| 138 | #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 139 | |
| 140 | void TestAnnotationsOnWrongQueue() { |
| 141 | TestAnnotations annotations; |
| 142 | TaskQueueForTest queue; |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 143 | queue.SendTask([&] { annotations.ModifyTestVar(); }); |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | #if RTC_DCHECK_IS_ON |
Tommi | ec3ba73 | 2020-05-17 12:33:40 | [diff] [blame] | 147 | // Note: Ending the test suite name with 'DeathTest' is important as it causes |
| 148 | // gtest to order this test before any other non-death-tests, to avoid potential |
| 149 | // global process state pollution such as shared worker threads being started |
| 150 | // (e.g. a side effect of calling InitCocoaMultiThreading() on Mac causes one or |
| 151 | // two additional threads to be created). |
| 152 | TEST(SequenceCheckerDeathTest, TestAnnotationsOnWrongQueueDebug) { |
Sebastian Jansson | b55015e | 2019-04-09 11:44:04 | [diff] [blame] | 153 | ASSERT_DEATH({ TestAnnotationsOnWrongQueue(); }, ""); |
| 154 | } |
| 155 | #else |
| 156 | TEST(SequenceCheckerTest, TestAnnotationsOnWrongQueueRelease) { |
| 157 | TestAnnotationsOnWrongQueue(); |
| 158 | } |
| 159 | #endif |
| 160 | #endif // GTEST_HAS_DEATH_TEST |
| 161 | } // namespace webrtc |