blob: b087badd94ab5cbd64803db680b4026fcdbec444 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
deadbeeff5f03e82016-06-06 18:16:0614#include "webrtc/base/fakeclock.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2615#include "webrtc/base/logging.h"
16#include "webrtc/base/thread.h"
phoglundc6c00b32016-05-04 08:54:3517#if defined(GTEST_RELATIVE_PATH)
kjellander@webrtc.org3c0aae12014-09-04 09:55:4018#include "testing/gtest/include/gtest/gtest.h"
phoglundc6c00b32016-05-04 08:54:3519#else
20#include "testing/base/public/gunit.h"
21#endif
henrike@webrtc.orgf0488722014-05-13 18:00:2622
henrike@webrtc.orgf0488722014-05-13 18:00:2623// Wait until "ex" is true, or "timeout" expires.
Taylor Brandstetter716d07a2016-06-27 21:07:4124#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); \
deadbeeff5f03e82016-06-06 18:16:0629 }
henrike@webrtc.orgf0488722014-05-13 18:00:2630
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 Brandstetter716d07a2016-06-27 21:07:4134#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 } \
torbjornga0892572015-12-17 02:38:3143 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:2644
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); \
torbjornga0892572015-12-17 02:38:3151 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:2652
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); \
torbjornga0892572015-12-17 02:38:3158 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:2659
60#define ASSERT_TRUE_WAIT(ex, timeout) \
61 do { \
62 bool res; \
63 WAIT_(ex, timeout, res); \
64 if (!res) ASSERT_TRUE(ex); \
torbjornga0892572015-12-17 02:38:3165 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:2666
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); \
torbjornga0892572015-12-17 02:38:3172 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:2673
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 Brandstetter716d07a2016-06-27 21:07:4177#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 } \
torbjornga0892572015-12-17 02:38:3191 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:2692
deadbeeff5f03e82016-06-06 18:16:0693// 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 Brandstetter716d07a2016-06-27 21:07:4197 !(ex) && rtc::TimeMillis() < start + (timeout);) { \
deadbeeff5f03e82016-06-06 18:16:0698 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 Brandstetter716d07a2016-06-27 21:07:41108 while (!res && rtc::TimeMillis() < start + (timeout)) { \
deadbeeff5f03e82016-06-06 18:16:06109 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 Brandstetter716d07a2016-06-27 21:07:41133#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.orgf0488722014-05-13 18:00:26149#endif // WEBRTC_BASE_GUNIT_H_