blob: 3260019e0abbb01c7d61d13a9e38aec8d840c9ee [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Henrik Kjellanderc0362762017-06-29 08:03:04 +020011#ifndef WEBRTC_RTC_BASE_GUNIT_H_
12#define WEBRTC_RTC_BASE_GUNIT_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
kjellandere96c45b2017-06-30 10:45:21 -070014#include "webrtc/rtc_base/fakeclock.h"
15#include "webrtc/rtc_base/logging.h"
16#include "webrtc/rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#if defined(GTEST_RELATIVE_PATH)
18#include "webrtc/test/gtest.h"
19#else
20#include "testing/base/public/gunit.h"
21#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023// Wait until "ex" is true, or "timeout" expires.
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); \
29 }
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.
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 } \
43 } while (0)
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); \
51 } while (0)
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); \
58 } while (0)
59
60#define ASSERT_TRUE_WAIT(ex, timeout) \
61 do { \
62 bool res; \
63 WAIT_(ex, timeout, res); \
64 if (!res) ASSERT_TRUE(ex); \
65 } while (0)
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); \
72 } while (0)
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.
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 } \
91 } while (0)
92
93// Wait until "ex" is true, or "timeout" expires, using fake clock where
94// messages are processed every millisecond.
95// TODO(pthatcher): Allow tests to control how many milliseconds to advance.
96#define SIMULATED_WAIT(ex, timeout, clock) \
97 for (int64_t start = rtc::TimeMillis(); \
98 !(ex) && rtc::TimeMillis() < start + (timeout);) { \
99 (clock).AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); \
100 }
101
102// This returns the result of the test in res, so that we don't re-evaluate
103// the expression in the XXXX_WAIT macros below, since that causes problems
104// when the expression is only true the first time you check it.
105#define SIMULATED_WAIT_(ex, timeout, res, clock) \
106 do { \
107 int64_t start = rtc::TimeMillis(); \
108 res = (ex); \
109 while (!res && rtc::TimeMillis() < start + (timeout)) { \
110 (clock).AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); \
111 res = (ex); \
112 } \
113 } while (0)
114
115// The typical EXPECT_XXXX, but done until true or a timeout with a fake clock.
116#define EXPECT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \
117 do { \
118 bool res; \
119 SIMULATED_WAIT_(ex, timeout, res, clock); \
120 if (!res) { \
121 EXPECT_TRUE(ex); \
122 } \
123 } while (0)
124
125#define EXPECT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \
126 do { \
127 bool res; \
128 SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \
129 if (!res) { \
130 EXPECT_EQ(v1, v2); \
131 } \
132 } while (0)
133
134#define ASSERT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \
135 do { \
136 bool res; \
137 SIMULATED_WAIT_(ex, timeout, res, clock); \
138 if (!res) \
139 ASSERT_TRUE(ex); \
140 } while (0)
141
142#define ASSERT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \
143 do { \
144 bool res; \
145 SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \
146 if (!res) \
147 ASSERT_EQ(v1, v2); \
148 } while (0)
Taylor Brandstetter716d07a2016-06-27 14:07:41 -0700149
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200150#endif // WEBRTC_RTC_BASE_GUNIT_H_