You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef TALK_BASE_GUNIT_H_
  28. #define TALK_BASE_GUNIT_H_
  29. #include "talk/base/logging.h"
  30. #include "talk/base/thread.h"
  31. #if defined(ANDROID) || defined(GTEST_RELATIVE_PATH)
  32. #include "gtest/gtest.h"
  33. #else
  34. #include "testing/base/public/gunit.h"
  35. #endif
  36. // forward declarations
  37. namespace talk_base {
  38. class Pathname;
  39. }
  40. // Wait until "ex" is true, or "timeout" expires.
  41. #define WAIT(ex, timeout) \
  42. for (uint32 start = talk_base::Time(); \
  43. !(ex) && talk_base::Time() < start + timeout;) \
  44. talk_base::Thread::Current()->ProcessMessages(1);
  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. // The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout.
  58. #define EXPECT_TRUE_WAIT(ex, timeout) \
  59. do { \
  60. bool res; \
  61. WAIT_(ex, timeout, res); \
  62. if (!res) EXPECT_TRUE(ex); \
  63. } while (0);
  64. #define EXPECT_EQ_WAIT(v1, v2, timeout) \
  65. do { \
  66. bool res; \
  67. WAIT_(v1 == v2, timeout, res); \
  68. if (!res) EXPECT_EQ(v1, v2); \
  69. } while (0);
  70. #define ASSERT_TRUE_WAIT(ex, timeout) \
  71. do { \
  72. bool res; \
  73. WAIT_(ex, timeout, res); \
  74. if (!res) ASSERT_TRUE(ex); \
  75. } while (0);
  76. #define ASSERT_EQ_WAIT(v1, v2, timeout) \
  77. do { \
  78. bool res; \
  79. WAIT_(v1 == v2, timeout, res); \
  80. if (!res) ASSERT_EQ(v1, v2); \
  81. } while (0);
  82. // Version with a "soft" timeout and a margin. This logs if the timeout is
  83. // exceeded, but it only fails if the expression still isn't true after the
  84. // margin time passes.
  85. #define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \
  86. do { \
  87. bool res; \
  88. WAIT_(ex, timeout, res); \
  89. if (res) { \
  90. break; \
  91. } \
  92. LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \
  93. timeout << "ms; waiting an additional " << margin << "ms"; \
  94. WAIT_(ex, margin, res); \
  95. if (!res) { \
  96. EXPECT_TRUE(ex); \
  97. } \
  98. } while (0);
  99. talk_base::Pathname GetTalkDirectory();
  100. #endif // TALK_BASE_GUNIT_H_