Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Unified Diff: webrtc/base/gunit.h

Issue 2024813004: Improving the fake clock and using it to fix a flaky STUN timeout test. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Revising comments etc. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/base/gunit.h
diff --git a/webrtc/base/gunit.h b/webrtc/base/gunit.h
index e705322e6f827fd3dac94c2bb75a474376be977f..229513324b7bf7f93d14e4db75a4ffa9cc08228d 100644
--- a/webrtc/base/gunit.h
+++ b/webrtc/base/gunit.h
@@ -11,6 +11,7 @@
#ifndef WEBRTC_BASE_GUNIT_H_
#define WEBRTC_BASE_GUNIT_H_
+#include "webrtc/base/fakeclock.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/thread.h"
#if defined(GTEST_RELATIVE_PATH)
@@ -23,7 +24,8 @@
#define WAIT(ex, timeout) \
for (int64_t start = rtc::TimeMillis(); \
!(ex) && rtc::TimeMillis() < start + timeout;) \
- rtc::Thread::Current()->ProcessMessages(1);
+ rtc::Thread::Current()->ProcessMessages(0); \
+ rtc::Thread::Current()->SleepMs(1);
pthatcher1 2016/06/01 22:38:14 Did you intend the sleep to be in the for loop? I
pthatcher1 2016/06/01 22:38:14 Would it make sense to have an AdvanceTime method
Taylor Brandstetter 2016/06/01 23:28:37 That seems like too much hidden magic for me. g_cl
Taylor Brandstetter 2016/06/01 23:28:37 Oops. I blame whoever initially wrote this code an
// This returns the result of the test in res, so that we don't re-evaluate
// the expression in the XXXX_WAIT macros below, since that causes problems
@@ -33,7 +35,8 @@
int64_t start = rtc::TimeMillis(); \
res = (ex); \
while (!res && rtc::TimeMillis() < start + timeout) { \
- rtc::Thread::Current()->ProcessMessages(1); \
+ rtc::Thread::Current()->ProcessMessages(0); \
+ rtc::Thread::Current()->SleepMs(1); \
res = (ex); \
} \
} while (0)
@@ -85,4 +88,41 @@
} \
} while (0)
+// Wait until "ex" is true, or "timeout" expires, using fake clock where
+// messages are processed every millisecond.
+#define SIMULATED_WAIT(ex, clock, timeout) \
pthatcher1 2016/06/01 22:38:14 Would it make sense for the clock to be the last a
Taylor Brandstetter 2016/06/01 23:28:37 Sure, I don't have any preference.
+ for (int64_t start = rtc::TimeMillis(); \
+ !(ex) && rtc::TimeMillis() < start + timeout;) \
+ clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1));
+
+// This returns the result of the test in res, so that we don't re-evaluate
+// the expression in the XXXX_WAIT macros below, since that causes problems
+// when the expression is only true the first time you check it.
+#define SIMULATED_WAIT_(ex, clock, timeout, res) \
+ do { \
+ int64_t start = rtc::TimeMillis(); \
+ res = (ex); \
+ while (!res && rtc::TimeMillis() < start + timeout) { \
+ clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); \
+ res = (ex); \
+ } \
+ } while (0)
+
+// The typical EXPECT_XXXX, but done until true or a timeout with a fake clock.
+#define EXPECT_TRUE_SIMULATED_WAIT(ex, clock, timeout) \
+ do { \
+ bool res; \
+ SIMULATED_WAIT_(ex, clock, timeout, res); \
+ if (!res) \
+ EXPECT_TRUE(ex); \
+ } while (0)
+
+#define EXPECT_EQ_SIMULATED_WAIT(v1, v2, clock, timeout) \
+ do { \
+ bool res; \
+ SIMULATED_WAIT_(v1 == v2, clock, timeout, res); \
+ if (!res) \
+ EXPECT_EQ(v1, v2); \
+ } while (0)
+
#endif // WEBRTC_BASE_GUNIT_H_

Powered by Google App Engine
This is Rietveld 408576698