John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/condition.h" |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 2 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 3 | #include <sys/types.h> |
| 4 | #include <sys/wait.h> |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 5 | #include <unistd.h> |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 6 | |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 7 | #include <atomic> |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 8 | #include <chrono> |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 9 | #include <thread> |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 10 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 11 | #include "aos/die.h" |
| 12 | #include "aos/ipc_lib/aos_sync.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 13 | #include "aos/ipc_lib/core_lib.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 14 | #include "aos/logging/logging.h" |
| 15 | #include "aos/macros.h" |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 16 | #include "aos/mutex/mutex.h" |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 17 | #include "aos/testing/prevent_exit.h" |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 18 | #include "aos/testing/test_shm.h" |
| 19 | #include "aos/time/time.h" |
| 20 | #include "aos/type_traits/type_traits.h" |
| 21 | #include "gtest/gtest.h" |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 22 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 23 | namespace aos { |
| 24 | namespace testing { |
| 25 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 26 | namespace chrono = ::std::chrono; |
| 27 | |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 28 | class ConditionTestCommon : public ::testing::Test { |
| 29 | public: |
| 30 | ConditionTestCommon() {} |
| 31 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 32 | void Settle() { ::std::this_thread::sleep_for(chrono::milliseconds(8)); } |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 33 | |
| 34 | private: |
| 35 | DISALLOW_COPY_AND_ASSIGN(ConditionTestCommon); |
| 36 | }; |
| 37 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 38 | // Some simple tests that don't rely on a TestSharedMemory to help with |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 39 | // debugging problems that cause tests using that to just completely lock up. |
| 40 | class SimpleConditionTest : public ConditionTestCommon { |
| 41 | public: |
| 42 | SimpleConditionTest() : condition_(&mutex_) {} |
| 43 | |
| 44 | Mutex mutex_; |
| 45 | Condition condition_; |
| 46 | |
| 47 | private: |
| 48 | DISALLOW_COPY_AND_ASSIGN(SimpleConditionTest); |
| 49 | }; |
| 50 | |
| 51 | // Makes sure that nothing crashes or anything with a basic Wait() and then |
| 52 | // Signal(). |
| 53 | // This test is written to hopefully fail instead of deadlocking on failure, but |
| 54 | // it's tricky because there's no way to kill the child in the middle. |
| 55 | TEST_F(SimpleConditionTest, Basic) { |
| 56 | ::std::atomic_bool child_finished(false); |
| 57 | Condition child_ready(&mutex_); |
| 58 | ASSERT_FALSE(mutex_.Lock()); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame] | 59 | std::thread child([this, &child_finished, &child_ready] { |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 60 | ASSERT_FALSE(mutex_.Lock()); |
| 61 | child_ready.Broadcast(); |
| 62 | ASSERT_FALSE(condition_.Wait()); |
| 63 | child_finished.store(true); |
| 64 | mutex_.Unlock(); |
| 65 | }); |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 66 | ASSERT_FALSE(child_ready.Wait()); |
| 67 | EXPECT_FALSE(child_finished.load()); |
| 68 | condition_.Signal(); |
| 69 | mutex_.Unlock(); |
Kai Tinkess | bf1385d | 2020-01-18 14:18:49 -0800 | [diff] [blame] | 70 | child.join(); |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 71 | EXPECT_TRUE(child_finished.load()); |
| 72 | } |
| 73 | |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 74 | // Tests that contention on the associated mutex doesn't break anything. |
| 75 | // This seems likely to cause issues with AddressSanitizer in particular. |
| 76 | TEST_F(SimpleConditionTest, MutexContention) { |
| 77 | for (int i = 0; i < 1000; ++i) { |
| 78 | ASSERT_FALSE(mutex_.Lock()); |
| 79 | ::std::thread thread([this]() { |
| 80 | ASSERT_FALSE(mutex_.Lock()); |
| 81 | condition_.Signal(); |
| 82 | mutex_.Unlock(); |
| 83 | }); |
| 84 | ASSERT_FALSE(condition_.Wait()); |
| 85 | mutex_.Unlock(); |
| 86 | thread.join(); |
| 87 | } |
| 88 | } |
| 89 | |
Brian Silverman | f119464 | 2014-09-04 13:01:17 -0400 | [diff] [blame] | 90 | class ConditionTest : public ConditionTestCommon { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 91 | public: |
| 92 | struct Shared { |
| 93 | Shared() : condition(&mutex) {} |
| 94 | |
| 95 | Mutex mutex; |
| 96 | Condition condition; |
| 97 | }; |
| 98 | static_assert(shm_ok<Shared>::value, |
| 99 | "it's going to get shared between forked processes"); |
| 100 | |
| 101 | ConditionTest() : shared_(static_cast<Shared *>(shm_malloc(sizeof(Shared)))) { |
| 102 | new (shared_) Shared(); |
| 103 | } |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 104 | ~ConditionTest() { shared_->~Shared(); } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 105 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 106 | ::aos::testing::TestSharedMemory my_shm_; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 107 | |
| 108 | Shared *const shared_; |
| 109 | |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 110 | protected: |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 111 | void SetUp() override { SetDieTestMode(true); } |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 112 | |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 113 | private: |
| 114 | DISALLOW_COPY_AND_ASSIGN(ConditionTest); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | class ConditionTestProcess { |
| 118 | public: |
| 119 | enum class Action { |
| 120 | kWaitLockStart, // lock, delay, wait, unlock |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 121 | kWait, // delay, lock, wait, unlock |
| 122 | kWaitNoUnlock, // delay, lock, wait |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | // This amount gets added to any passed in delay to make the test repeatable. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 126 | static constexpr chrono::milliseconds kMinimumDelay = |
| 127 | chrono::milliseconds(150); |
| 128 | static constexpr chrono::milliseconds kDefaultTimeout = |
| 129 | chrono::milliseconds(150); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 130 | |
| 131 | // delay is how long to wait before doing action to condition. |
| 132 | // timeout is how long to wait after delay before deciding that it's hung. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 133 | ConditionTestProcess(chrono::milliseconds delay, Action action, |
| 134 | Condition *condition, |
| 135 | chrono::milliseconds timeout = kDefaultTimeout) |
| 136 | : delay_(kMinimumDelay + delay), |
| 137 | action_(action), |
| 138 | condition_(condition), |
| 139 | timeout_(delay_ + timeout), |
| 140 | child_(-1), |
| 141 | shared_(static_cast<Shared *>(shm_malloc(sizeof(Shared)))) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 142 | new (shared_) Shared(); |
| 143 | } |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 144 | ~ConditionTestProcess() { AOS_CHECK_EQ(child_, -1); } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 145 | |
| 146 | void Start() { |
| 147 | ASSERT_FALSE(shared_->started); |
| 148 | |
| 149 | child_ = fork(); |
| 150 | if (child_ == 0) { // in child |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 151 | ::aos::testing::PreventExit(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 152 | Run(); |
| 153 | exit(EXIT_SUCCESS); |
| 154 | } else { // in parent |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 155 | AOS_CHECK_NE(child_, -1); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 156 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 157 | ASSERT_EQ(0, futex_wait(&shared_->ready)); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 158 | |
| 159 | shared_->started = true; |
| 160 | } |
| 161 | } |
| 162 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 163 | bool IsFinished() { return shared_->finished; } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 164 | |
| 165 | ::testing::AssertionResult Hung() { |
| 166 | if (!shared_->started) { |
| 167 | ADD_FAILURE(); |
| 168 | return ::testing::AssertionFailure() << "not started yet"; |
| 169 | } |
| 170 | if (shared_->finished) { |
| 171 | Join(); |
| 172 | return ::testing::AssertionFailure() << "already returned"; |
| 173 | } |
| 174 | if (shared_->delayed) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 175 | if (shared_->start_time > monotonic_clock::now() + timeout_) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 176 | Kill(); |
| 177 | return ::testing::AssertionSuccess() << "already been too long"; |
| 178 | } |
| 179 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 180 | AOS_CHECK_EQ(0, futex_wait(&shared_->done_delaying)); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 181 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 182 | ::std::this_thread::sleep_for(chrono::milliseconds(10)); |
| 183 | if (!shared_->finished) { |
| 184 | ::std::this_thread::sleep_until(shared_->start_time + timeout_); |
| 185 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 186 | if (shared_->finished) { |
| 187 | Join(); |
| 188 | return ::testing::AssertionFailure() << "completed within timeout"; |
| 189 | } else { |
| 190 | Kill(); |
| 191 | return ::testing::AssertionSuccess() << "took too long"; |
| 192 | } |
| 193 | } |
| 194 | ::testing::AssertionResult Test() { |
| 195 | Start(); |
| 196 | return Hung(); |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | struct Shared { |
| 201 | Shared() |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 202 | : started(false), |
| 203 | delayed(false), |
| 204 | done_delaying(0), |
| 205 | start_time(monotonic_clock::epoch()), |
| 206 | finished(false), |
| 207 | ready(0) {} |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 208 | |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 209 | volatile bool started; |
| 210 | volatile bool delayed; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 211 | aos_futex done_delaying; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 212 | monotonic_clock::time_point start_time; |
Brian Silverman | eeb62ca | 2013-09-11 15:08:03 -0700 | [diff] [blame] | 213 | volatile bool finished; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 214 | aos_futex ready; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 215 | }; |
| 216 | static_assert(shm_ok<Shared>::value, |
| 217 | "it's going to get shared between forked processes"); |
| 218 | |
| 219 | void Run() { |
| 220 | if (action_ == Action::kWaitLockStart) { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 221 | ASSERT_EQ(1, futex_set(&shared_->ready)); |
| 222 | ASSERT_FALSE(condition_->m()->Lock()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 223 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 224 | ::std::this_thread::sleep_for(delay_); |
| 225 | shared_->start_time = monotonic_clock::now(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 226 | shared_->delayed = true; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 227 | ASSERT_NE(-1, futex_set(&shared_->done_delaying)); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 228 | if (action_ != Action::kWaitLockStart) { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 229 | ASSERT_EQ(1, futex_set(&shared_->ready)); |
| 230 | ASSERT_FALSE(condition_->m()->Lock()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 231 | } |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 232 | // TODO(brians): Test this returning true (aka the owner dying). |
| 233 | ASSERT_FALSE(condition_->Wait()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 234 | shared_->finished = true; |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 235 | if (action_ != Action::kWaitNoUnlock) { |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 236 | condition_->m()->Unlock(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void Join() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 241 | AOS_CHECK_NE(child_, -1); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 242 | int status; |
| 243 | do { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 244 | AOS_CHECK_EQ(waitpid(child_, &status, 0), child_); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 245 | } while (!(WIFEXITED(status) || WIFSIGNALED(status))); |
| 246 | child_ = -1; |
| 247 | } |
| 248 | void Kill() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 249 | AOS_CHECK_NE(child_, -1); |
| 250 | AOS_PCHECK(kill(child_, SIGTERM)); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 251 | Join(); |
| 252 | } |
| 253 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 254 | const chrono::milliseconds delay_; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 255 | const Action action_; |
| 256 | Condition *const condition_; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 257 | const chrono::milliseconds timeout_; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 258 | |
| 259 | pid_t child_; |
| 260 | |
| 261 | Shared *const shared_; |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 262 | |
| 263 | DISALLOW_COPY_AND_ASSIGN(ConditionTestProcess); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 264 | }; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 265 | constexpr chrono::milliseconds ConditionTestProcess::kMinimumDelay; |
| 266 | constexpr chrono::milliseconds ConditionTestProcess::kDefaultTimeout; |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 267 | |
| 268 | // Makes sure that the testing framework and everything work for a really simple |
| 269 | // Wait() and then Signal(). |
| 270 | TEST_F(ConditionTest, Basic) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 271 | ConditionTestProcess child(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 272 | ConditionTestProcess::Action::kWait, |
| 273 | &shared_->condition); |
| 274 | child.Start(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 275 | Settle(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 276 | EXPECT_FALSE(child.IsFinished()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 277 | shared_->condition.Signal(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 278 | EXPECT_FALSE(child.Hung()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 281 | // Makes sure that the worker child locks before it tries to Wait() etc. |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 282 | TEST_F(ConditionTest, Locking) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 283 | ConditionTestProcess child(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 284 | ConditionTestProcess::Action::kWait, |
| 285 | &shared_->condition); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 286 | ASSERT_FALSE(shared_->mutex.Lock()); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 287 | child.Start(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 288 | Settle(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 289 | // This Signal() shouldn't do anything because the child should still be |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 290 | // waiting to lock the mutex. |
| 291 | shared_->condition.Signal(); |
| 292 | Settle(); |
| 293 | shared_->mutex.Unlock(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 294 | EXPECT_TRUE(child.Hung()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 297 | // Tests that the work child only catches a Signal() after the mutex gets |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 298 | // unlocked. |
| 299 | TEST_F(ConditionTest, LockFirst) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 300 | ConditionTestProcess child(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 301 | ConditionTestProcess::Action::kWait, |
| 302 | &shared_->condition); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 303 | ASSERT_FALSE(shared_->mutex.Lock()); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 304 | child.Start(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 305 | Settle(); |
| 306 | shared_->condition.Signal(); |
| 307 | Settle(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 308 | EXPECT_FALSE(child.IsFinished()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 309 | shared_->mutex.Unlock(); |
| 310 | Settle(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 311 | EXPECT_FALSE(child.IsFinished()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 312 | shared_->condition.Signal(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 313 | EXPECT_FALSE(child.Hung()); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Tests that the mutex gets relocked after Wait() returns. |
| 317 | TEST_F(ConditionTest, Relocking) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 318 | ConditionTestProcess child(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 319 | ConditionTestProcess::Action::kWaitNoUnlock, |
| 320 | &shared_->condition); |
| 321 | child.Start(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 322 | Settle(); |
| 323 | shared_->condition.Signal(); |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 324 | EXPECT_FALSE(child.Hung()); |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 325 | EXPECT_EQ(Mutex::State::kOwnerDied, shared_->mutex.TryLock()); |
| 326 | shared_->mutex.Unlock(); |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 329 | // Tests that Signal() stops exactly 1 Wait()er. |
| 330 | TEST_F(ConditionTest, SignalOne) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 331 | ConditionTestProcess child1(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 332 | ConditionTestProcess::Action::kWait, |
| 333 | &shared_->condition); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 334 | ConditionTestProcess child2(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 335 | ConditionTestProcess::Action::kWait, |
| 336 | &shared_->condition); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 337 | ConditionTestProcess child3(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 338 | ConditionTestProcess::Action::kWait, |
| 339 | &shared_->condition); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 340 | auto number_finished = [&]() { |
| 341 | return (child1.IsFinished() ? 1 : 0) + (child2.IsFinished() ? 1 : 0) + |
| 342 | (child3.IsFinished() ? 1 : 0); |
| 343 | }; |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 344 | child1.Start(); |
| 345 | child2.Start(); |
| 346 | child3.Start(); |
| 347 | Settle(); |
| 348 | EXPECT_EQ(0, number_finished()); |
| 349 | shared_->condition.Signal(); |
| 350 | Settle(); |
| 351 | EXPECT_EQ(1, number_finished()); |
| 352 | shared_->condition.Signal(); |
| 353 | Settle(); |
| 354 | EXPECT_EQ(2, number_finished()); |
| 355 | shared_->condition.Signal(); |
| 356 | Settle(); |
| 357 | EXPECT_EQ(3, number_finished()); |
| 358 | EXPECT_FALSE(child1.Hung()); |
| 359 | EXPECT_FALSE(child2.Hung()); |
| 360 | EXPECT_FALSE(child3.Hung()); |
| 361 | } |
| 362 | |
| 363 | // Tests that Brodcast() wakes multiple Wait()ers. |
| 364 | TEST_F(ConditionTest, Broadcast) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 365 | ConditionTestProcess child1(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 366 | ConditionTestProcess::Action::kWait, |
| 367 | &shared_->condition); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 368 | ConditionTestProcess child2(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 369 | ConditionTestProcess::Action::kWait, |
| 370 | &shared_->condition); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 371 | ConditionTestProcess child3(chrono::milliseconds(0), |
Brian Silverman | 2586a5d | 2013-09-13 13:45:52 -0700 | [diff] [blame] | 372 | ConditionTestProcess::Action::kWait, |
| 373 | &shared_->condition); |
| 374 | child1.Start(); |
| 375 | child2.Start(); |
| 376 | child3.Start(); |
| 377 | Settle(); |
| 378 | shared_->condition.Broadcast(); |
| 379 | EXPECT_FALSE(child1.Hung()); |
| 380 | EXPECT_FALSE(child2.Hung()); |
| 381 | EXPECT_FALSE(child3.Hung()); |
| 382 | } |
| 383 | |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 384 | } // namespace testing |
| 385 | } // namespace aos |