henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
| 11 | #ifndef WEBRTC_BASE_GUNIT_H_ |
| 12 | #define WEBRTC_BASE_GUNIT_H_ |
| 13 | |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 14 | #include "webrtc/base/fakeclock.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | #include "webrtc/base/logging.h" |
| 16 | #include "webrtc/base/thread.h" |
phoglund | c6c00b3 | 2016-05-04 08:54:35 | [diff] [blame] | 17 | #if defined(GTEST_RELATIVE_PATH) |
kjellander@webrtc.org | 3c0aae1 | 2014-09-04 09:55:40 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
phoglund | c6c00b3 | 2016-05-04 08:54:35 | [diff] [blame] | 19 | #else |
| 20 | #include "testing/base/public/gunit.h" |
| 21 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 22 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 23 | // Wait until "ex" is true, or "timeout" expires. |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 24 | #define WAIT(ex, timeout) \ |
| 25 | for (int64_t start = rtc::SystemTimeMillis(); \ |
| 26 | !(ex) && rtc::SystemTimeMillis() < start + (timeout);) { \ |
| 27 | rtc::Thread::Current()->ProcessMessages(0); \ |
| 28 | rtc::Thread::Current()->SleepMs(1); \ |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 29 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 30 | |
| 31 | // This returns the result of the test in res, so that we don't re-evaluate |
| 32 | // the expression in the XXXX_WAIT macros below, since that causes problems |
| 33 | // when the expression is only true the first time you check it. |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 34 | #define WAIT_(ex, timeout, res) \ |
| 35 | do { \ |
| 36 | int64_t start = rtc::SystemTimeMillis(); \ |
| 37 | res = (ex); \ |
| 38 | while (!res && rtc::SystemTimeMillis() < start + (timeout)) { \ |
| 39 | rtc::Thread::Current()->ProcessMessages(0); \ |
| 40 | rtc::Thread::Current()->SleepMs(1); \ |
| 41 | res = (ex); \ |
| 42 | } \ |
torbjorng | a089257 | 2015-12-17 02:38:31 | [diff] [blame] | 43 | } while (0) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 44 | |
| 45 | // The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout. |
| 46 | #define EXPECT_TRUE_WAIT(ex, timeout) \ |
| 47 | do { \ |
| 48 | bool res; \ |
| 49 | WAIT_(ex, timeout, res); \ |
| 50 | if (!res) EXPECT_TRUE(ex); \ |
torbjorng | a089257 | 2015-12-17 02:38:31 | [diff] [blame] | 51 | } while (0) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 52 | |
| 53 | #define EXPECT_EQ_WAIT(v1, v2, timeout) \ |
| 54 | do { \ |
| 55 | bool res; \ |
| 56 | WAIT_(v1 == v2, timeout, res); \ |
| 57 | if (!res) EXPECT_EQ(v1, v2); \ |
torbjorng | a089257 | 2015-12-17 02:38:31 | [diff] [blame] | 58 | } while (0) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 59 | |
| 60 | #define ASSERT_TRUE_WAIT(ex, timeout) \ |
| 61 | do { \ |
| 62 | bool res; \ |
| 63 | WAIT_(ex, timeout, res); \ |
| 64 | if (!res) ASSERT_TRUE(ex); \ |
torbjorng | a089257 | 2015-12-17 02:38:31 | [diff] [blame] | 65 | } while (0) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 66 | |
| 67 | #define ASSERT_EQ_WAIT(v1, v2, timeout) \ |
| 68 | do { \ |
| 69 | bool res; \ |
| 70 | WAIT_(v1 == v2, timeout, res); \ |
| 71 | if (!res) ASSERT_EQ(v1, v2); \ |
torbjorng | a089257 | 2015-12-17 02:38:31 | [diff] [blame] | 72 | } while (0) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 73 | |
| 74 | // Version with a "soft" timeout and a margin. This logs if the timeout is |
| 75 | // exceeded, but it only fails if the expression still isn't true after the |
| 76 | // margin time passes. |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 77 | #define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \ |
| 78 | do { \ |
| 79 | bool res; \ |
| 80 | WAIT_(ex, timeout, res); \ |
| 81 | if (res) { \ |
| 82 | break; \ |
| 83 | } \ |
| 84 | LOG(LS_WARNING) << "Expression " << #ex << " still not true after " \ |
| 85 | << (timeout) << "ms; waiting an additional " << margin \ |
| 86 | << "ms"; \ |
| 87 | WAIT_(ex, margin, res); \ |
| 88 | if (!res) { \ |
| 89 | EXPECT_TRUE(ex); \ |
| 90 | } \ |
torbjorng | a089257 | 2015-12-17 02:38:31 | [diff] [blame] | 91 | } while (0) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 92 | |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 93 | // Wait until "ex" is true, or "timeout" expires, using fake clock where |
| 94 | // messages are processed every millisecond. |
| 95 | #define SIMULATED_WAIT(ex, timeout, clock) \ |
| 96 | for (int64_t start = rtc::TimeMillis(); \ |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 97 | !(ex) && rtc::TimeMillis() < start + (timeout);) { \ |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 98 | clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); \ |
| 99 | } |
| 100 | |
| 101 | // This returns the result of the test in res, so that we don't re-evaluate |
| 102 | // the expression in the XXXX_WAIT macros below, since that causes problems |
| 103 | // when the expression is only true the first time you check it. |
| 104 | #define SIMULATED_WAIT_(ex, timeout, res, clock) \ |
| 105 | do { \ |
| 106 | int64_t start = rtc::TimeMillis(); \ |
| 107 | res = (ex); \ |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 108 | while (!res && rtc::TimeMillis() < start + (timeout)) { \ |
deadbeef | f5f03e8 | 2016-06-06 18:16:06 | [diff] [blame] | 109 | clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); \ |
| 110 | res = (ex); \ |
| 111 | } \ |
| 112 | } while (0) |
| 113 | |
| 114 | // The typical EXPECT_XXXX, but done until true or a timeout with a fake clock. |
| 115 | #define EXPECT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \ |
| 116 | do { \ |
| 117 | bool res; \ |
| 118 | SIMULATED_WAIT_(ex, timeout, res, clock); \ |
| 119 | if (!res) { \ |
| 120 | EXPECT_TRUE(ex); \ |
| 121 | } \ |
| 122 | } while (0) |
| 123 | |
| 124 | #define EXPECT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \ |
| 125 | do { \ |
| 126 | bool res; \ |
| 127 | SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \ |
| 128 | if (!res) { \ |
| 129 | EXPECT_EQ(v1, v2); \ |
| 130 | } \ |
| 131 | } while (0) |
| 132 | |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 133 | #define ASSERT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \ |
| 134 | do { \ |
| 135 | bool res; \ |
| 136 | SIMULATED_WAIT_(ex, timeout, res, clock); \ |
| 137 | if (!res) \ |
| 138 | ASSERT_TRUE(ex); \ |
| 139 | } while (0) |
| 140 | |
| 141 | #define ASSERT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \ |
| 142 | do { \ |
| 143 | bool res; \ |
| 144 | SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \ |
| 145 | if (!res) \ |
| 146 | ASSERT_EQ(v1, v2); \ |
| 147 | } while (0) |
| 148 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 149 | #endif // WEBRTC_BASE_GUNIT_H_ |