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