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