blob: 5ba0bd916e6632a50c8754b4fe07c061a69b12dc [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
11#ifndef WEBRTC_BASE_GUNIT_H_
12#define WEBRTC_BASE_GUNIT_H_
13
deadbeeff5f03e82016-06-06 11:16:06 -070014#include "webrtc/base/fakeclock.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include "webrtc/base/logging.h"
16#include "webrtc/base/thread.h"
phoglundc6c00b32016-05-04 01:54:35 -070017#if defined(GTEST_RELATIVE_PATH)
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000018#include "testing/gtest/include/gtest/gtest.h"
phoglundc6c00b32016-05-04 01:54:35 -070019#else
20#include "testing/base/public/gunit.h"
21#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023// Wait until "ex" is true, or "timeout" expires.
deadbeeff5f03e82016-06-06 11:16:06 -070024#define WAIT(ex, timeout) \
25 for (int64_t start = rtc::TimeMillis(); \
26 !(ex) && rtc::TimeMillis() < start + timeout;) { \
27 rtc::Thread::Current()->ProcessMessages(0); \
28 rtc::Thread::Current()->SleepMs(1); \
29 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030
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.
Honghai Zhang82d78622016-05-06 11:29:15 -070034#define WAIT_(ex, timeout, res) \
35 do { \
36 int64_t start = rtc::TimeMillis(); \
37 res = (ex); \
38 while (!res && rtc::TimeMillis() < start + timeout) { \
deadbeeff5f03e82016-06-06 11:16:06 -070039 rtc::Thread::Current()->ProcessMessages(0); \
40 rtc::Thread::Current()->SleepMs(1); \
Honghai Zhang82d78622016-05-06 11:29:15 -070041 res = (ex); \
42 } \
torbjornga0892572015-12-16 18:38:31 -080043 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
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-16 18:38:31 -080051 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052
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-16 18:38:31 -080058 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
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-16 18:38:31 -080065 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066
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-16 18:38:31 -080072 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073
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 << "ms"; \
86 WAIT_(ex, margin, res); \
87 if (!res) { \
88 EXPECT_TRUE(ex); \
89 } \
torbjornga0892572015-12-16 18:38:31 -080090 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091
deadbeeff5f03e82016-06-06 11:16:06 -070092// Wait until "ex" is true, or "timeout" expires, using fake clock where
93// messages are processed every millisecond.
94#define SIMULATED_WAIT(ex, timeout, clock) \
95 for (int64_t start = rtc::TimeMillis(); \
96 !(ex) && rtc::TimeMillis() < start + timeout;) { \
97 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); \
98 }
99
100// This returns the result of the test in res, so that we don't re-evaluate
101// the expression in the XXXX_WAIT macros below, since that causes problems
102// when the expression is only true the first time you check it.
103#define SIMULATED_WAIT_(ex, timeout, res, clock) \
104 do { \
105 int64_t start = rtc::TimeMillis(); \
106 res = (ex); \
107 while (!res && rtc::TimeMillis() < start + timeout) { \
108 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); \
109 res = (ex); \
110 } \
111 } while (0)
112
113// The typical EXPECT_XXXX, but done until true or a timeout with a fake clock.
114#define EXPECT_TRUE_SIMULATED_WAIT(ex, timeout, clock) \
115 do { \
116 bool res; \
117 SIMULATED_WAIT_(ex, timeout, res, clock); \
118 if (!res) { \
119 EXPECT_TRUE(ex); \
120 } \
121 } while (0)
122
123#define EXPECT_EQ_SIMULATED_WAIT(v1, v2, timeout, clock) \
124 do { \
125 bool res; \
126 SIMULATED_WAIT_(v1 == v2, timeout, res, clock); \
127 if (!res) { \
128 EXPECT_EQ(v1, v2); \
129 } \
130 } while (0)
131
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132#endif // WEBRTC_BASE_GUNIT_H_