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