blob: 1a6c36374e935e46a81a1496fe04539fc40f57d9 [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
14#include "webrtc/base/logging.h"
15#include "webrtc/base/thread.h"
phoglundc6c00b32016-05-04 01:54:35 -070016#if defined(GTEST_RELATIVE_PATH)
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000017#include "testing/gtest/include/gtest/gtest.h"
phoglundc6c00b32016-05-04 01:54:35 -070018#else
19#include "testing/base/public/gunit.h"
20#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022// Wait until "ex" is true, or "timeout" expires.
Peter Boström0c4e06b2015-10-07 12:23:21 +020023#define WAIT(ex, timeout) \
24 for (uint32_t start = rtc::Time(); !(ex) && rtc::Time() < start + timeout;) \
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025 rtc::Thread::Current()->ProcessMessages(1);
26
27// This returns the result of the test in res, so that we don't re-evaluate
28// the expression in the XXXX_WAIT macros below, since that causes problems
29// when the expression is only true the first time you check it.
Peter Boström0c4e06b2015-10-07 12:23:21 +020030#define WAIT_(ex, timeout, res) \
31 do { \
32 uint32_t start = rtc::Time(); \
33 res = (ex); \
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034 while (!res && rtc::Time() < start + timeout) { \
Peter Boström0c4e06b2015-10-07 12:23:21 +020035 rtc::Thread::Current()->ProcessMessages(1); \
36 res = (ex); \
37 } \
torbjornga0892572015-12-16 18:38:31 -080038 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039
40// The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout.
41#define EXPECT_TRUE_WAIT(ex, timeout) \
42 do { \
43 bool res; \
44 WAIT_(ex, timeout, res); \
45 if (!res) EXPECT_TRUE(ex); \
torbjornga0892572015-12-16 18:38:31 -080046 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047
48#define EXPECT_EQ_WAIT(v1, v2, timeout) \
49 do { \
50 bool res; \
51 WAIT_(v1 == v2, timeout, res); \
52 if (!res) EXPECT_EQ(v1, v2); \
torbjornga0892572015-12-16 18:38:31 -080053 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054
55#define ASSERT_TRUE_WAIT(ex, timeout) \
56 do { \
57 bool res; \
58 WAIT_(ex, timeout, res); \
59 if (!res) ASSERT_TRUE(ex); \
torbjornga0892572015-12-16 18:38:31 -080060 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061
62#define ASSERT_EQ_WAIT(v1, v2, timeout) \
63 do { \
64 bool res; \
65 WAIT_(v1 == v2, timeout, res); \
66 if (!res) ASSERT_EQ(v1, v2); \
torbjornga0892572015-12-16 18:38:31 -080067 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
69// Version with a "soft" timeout and a margin. This logs if the timeout is
70// exceeded, but it only fails if the expression still isn't true after the
71// margin time passes.
72#define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \
73 do { \
74 bool res; \
75 WAIT_(ex, timeout, res); \
76 if (res) { \
77 break; \
78 } \
79 LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \
80 timeout << "ms; waiting an additional " << margin << "ms"; \
81 WAIT_(ex, margin, res); \
82 if (!res) { \
83 EXPECT_TRUE(ex); \
84 } \
torbjornga0892572015-12-16 18:38:31 -080085 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087#endif // WEBRTC_BASE_GUNIT_H_