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 39 // forward declarations 40 namespace talk_base { 41 class Pathname; 42 } 43 44 // Wait until "ex" is true, or "timeout" expires. 45 #define WAIT(ex, timeout) \ 46 for (uint32 start = talk_base::Time(); \ 47 !(ex) && talk_base::Time() < start + timeout;) \ 48 talk_base::Thread::Current()->ProcessMessages(1); 49 50 // This returns the result of the test in res, so that we don't re-evaluate 51 // the expression in the XXXX_WAIT macros below, since that causes problems 52 // when the expression is only true the first time you check it. 53 #define WAIT_(ex, timeout, res) \ 54 do { \ 55 uint32 start = talk_base::Time(); \ 56 res = (ex); \ 57 while (!res && talk_base::Time() < start + timeout) { \ 58 talk_base::Thread::Current()->ProcessMessages(1); \ 59 res = (ex); \ 60 } \ 61 } while (0); 62 63 // The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout. 64 #define EXPECT_TRUE_WAIT(ex, timeout) \ 65 do { \ 66 bool res; \ 67 WAIT_(ex, timeout, res); \ 68 if (!res) EXPECT_TRUE(ex); \ 69 } while (0); 70 71 #define EXPECT_EQ_WAIT(v1, v2, timeout) \ 72 do { \ 73 bool res; \ 74 WAIT_(v1 == v2, timeout, res); \ 75 if (!res) EXPECT_EQ(v1, v2); \ 76 } while (0); 77 78 #define ASSERT_TRUE_WAIT(ex, timeout) \ 79 do { \ 80 bool res; \ 81 WAIT_(ex, timeout, res); \ 82 if (!res) ASSERT_TRUE(ex); \ 83 } while (0); 84 85 #define ASSERT_EQ_WAIT(v1, v2, timeout) \ 86 do { \ 87 bool res; \ 88 WAIT_(v1 == v2, timeout, res); \ 89 if (!res) ASSERT_EQ(v1, v2); \ 90 } while (0); 91 92 // Version with a "soft" timeout and a margin. This logs if the timeout is 93 // exceeded, but it only fails if the expression still isn't true after the 94 // margin time passes. 95 #define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \ 96 do { \ 97 bool res; \ 98 WAIT_(ex, timeout, res); \ 99 if (res) { \ 100 break; \ 101 } \ 102 LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \ 103 timeout << "ms; waiting an additional " << margin << "ms"; \ 104 WAIT_(ex, margin, res); \ 105 if (!res) { \ 106 EXPECT_TRUE(ex); \ 107 } \ 108 } while (0); 109 110 talk_base::Pathname GetTalkDirectory(); 111 112 #endif // TALK_BASE_GUNIT_H_ 113