blob: 986d4dcb623d70000679589b91d85fad00ca919e [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"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000016#include "testing/gtest/include/gtest/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018// Wait until "ex" is true, or "timeout" expires.
Peter Boström0c4e06b2015-10-07 12:23:21 +020019#define WAIT(ex, timeout) \
20 for (uint32_t start = rtc::Time(); !(ex) && rtc::Time() < start + timeout;) \
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021 rtc::Thread::Current()->ProcessMessages(1);
22
23// This returns the result of the test in res, so that we don't re-evaluate
24// the expression in the XXXX_WAIT macros below, since that causes problems
25// when the expression is only true the first time you check it.
Peter Boström0c4e06b2015-10-07 12:23:21 +020026#define WAIT_(ex, timeout, res) \
27 do { \
28 uint32_t start = rtc::Time(); \
29 res = (ex); \
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 while (!res && rtc::Time() < start + timeout) { \
Peter Boström0c4e06b2015-10-07 12:23:21 +020031 rtc::Thread::Current()->ProcessMessages(1); \
32 res = (ex); \
33 } \
torbjornga0892572015-12-16 18:38:31 -080034 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
36// The typical EXPECT_XXXX and ASSERT_XXXXs, but done until true or a timeout.
37#define EXPECT_TRUE_WAIT(ex, timeout) \
38 do { \
39 bool res; \
40 WAIT_(ex, timeout, res); \
41 if (!res) EXPECT_TRUE(ex); \
torbjornga0892572015-12-16 18:38:31 -080042 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043
44#define EXPECT_EQ_WAIT(v1, v2, timeout) \
45 do { \
46 bool res; \
47 WAIT_(v1 == v2, timeout, res); \
48 if (!res) EXPECT_EQ(v1, v2); \
torbjornga0892572015-12-16 18:38:31 -080049 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51#define ASSERT_TRUE_WAIT(ex, timeout) \
52 do { \
53 bool res; \
54 WAIT_(ex, timeout, res); \
55 if (!res) ASSERT_TRUE(ex); \
torbjornga0892572015-12-16 18:38:31 -080056 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057
58#define ASSERT_EQ_WAIT(v1, v2, timeout) \
59 do { \
60 bool res; \
61 WAIT_(v1 == v2, timeout, res); \
62 if (!res) ASSERT_EQ(v1, v2); \
torbjornga0892572015-12-16 18:38:31 -080063 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
65// Version with a "soft" timeout and a margin. This logs if the timeout is
66// exceeded, but it only fails if the expression still isn't true after the
67// margin time passes.
68#define EXPECT_TRUE_WAIT_MARGIN(ex, timeout, margin) \
69 do { \
70 bool res; \
71 WAIT_(ex, timeout, res); \
72 if (res) { \
73 break; \
74 } \
75 LOG(LS_WARNING) << "Expression " << #ex << " still not true after " << \
76 timeout << "ms; waiting an additional " << margin << "ms"; \
77 WAIT_(ex, margin, res); \
78 if (!res) { \
79 EXPECT_TRUE(ex); \
80 } \
torbjornga0892572015-12-16 18:38:31 -080081 } while (0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083#endif // WEBRTC_BASE_GUNIT_H_