henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2008, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_GUNIT_H_ |
| 29 | #define TALK_BASE_GUNIT_H_ |
| 30 | |
| 31 | #include "talk/base/logging.h" |
| 32 | #include "talk/base/thread.h" |
| 33 | #if defined(ANDROID) || defined(GTEST_RELATIVE_PATH) |
| 34 | #include "gtest/gtest.h" |
| 35 | #else |
| 36 | #include "testing/base/public/gunit.h" |
| 37 | #endif |
| 38 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | // Wait until "ex" is true, or "timeout" expires. |
| 40 | #define WAIT(ex, timeout) \ |
| 41 | for (uint32 start = talk_base::Time(); \ |
| 42 | !(ex) && talk_base::Time() < start + timeout;) \ |
| 43 | talk_base::Thread::Current()->ProcessMessages(1); |
| 44 | |
| 45 | // This returns the result of the test in res, so that we don't re-evaluate |
| 46 | // the expression in the XXXX_WAIT macros below, since that causes problems |
| 47 | // when the expression is only true the first time you check it. |
| 48 | #define WAIT_(ex, timeout, res) \ |
| 49 | do { \ |
| 50 | uint32 start = talk_base::Time(); \ |
| 51 | res = (ex); \ |
| 52 | while (!res && talk_base::Time() < start + timeout) { \ |
| 53 | talk_base::Thread::Current()->ProcessMessages(1); \ |
| 54 | res = (ex); \ |
| 55 | } \ |
| 56 | } while (0); |
| 57 | |
| 58 | // The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout. |
| 59 | #define EXPECT_TRUE_WAIT(ex, timeout) \ |
| 60 | do { \ |
| 61 | bool res; \ |
| 62 | WAIT_(ex, timeout, res); \ |
| 63 | if (!res) EXPECT_TRUE(ex); \ |
| 64 | } while (0); |
| 65 | |
| 66 | #define EXPECT_EQ_WAIT(v1, v2, timeout) \ |
| 67 | do { \ |
| 68 | bool res; \ |
| 69 | WAIT_(v1 == v2, timeout, res); \ |
| 70 | if (!res) EXPECT_EQ(v1, v2); \ |
| 71 | } while (0); |
| 72 | |
| 73 | #define ASSERT_TRUE_WAIT(ex, timeout) \ |
| 74 | do { \ |
| 75 | bool res; \ |
| 76 | WAIT_(ex, timeout, res); \ |
| 77 | if (!res) ASSERT_TRUE(ex); \ |
| 78 | } while (0); |
| 79 | |
| 80 | #define ASSERT_EQ_WAIT(v1, v2, timeout) \ |
| 81 | do { \ |
| 82 | bool res; \ |
| 83 | WAIT_(v1 == v2, timeout, res); \ |
| 84 | if (!res) ASSERT_EQ(v1, v2); \ |
| 85 | } while (0); |
| 86 | |
| 87 | // Version with a "soft" timeout and a margin. This logs if the timeout is |
| 88 | // exceeded, but it only fails if the expression still isn't true after the |
| 89 | // margin time passes. |
| 90 | #define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \ |
| 91 | do { \ |
| 92 | bool res; \ |
| 93 | WAIT_(ex, timeout, res); \ |
| 94 | if (res) { \ |
| 95 | break; \ |
| 96 | } \ |
| 97 | LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \ |
| 98 | timeout << "ms; waiting an additional " << margin << "ms"; \ |
| 99 | WAIT_(ex, margin, res); \ |
| 100 | if (!res) { \ |
| 101 | EXPECT_TRUE(ex); \ |
| 102 | } \ |
| 103 | } while (0); |
| 104 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | #endif // TALK_BASE_GUNIT_H_ |