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

Unified Diff: webrtc/p2p/base/stunrequest_unittest.cc

Issue 2300143005: Fixed flaky StunRequestTests which depended on the wall clock (Closed)
Patch Set: Removed the no-longer-needed constant Created 4 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/p2p/base/stunrequest_unittest.cc
diff --git a/webrtc/p2p/base/stunrequest_unittest.cc b/webrtc/p2p/base/stunrequest_unittest.cc
index 5fb34a85d263e1ec90e74dd59711e3cb9d97557a..b91b5d8e66fd1c408e9288827e1a9bad400c0a7a 100644
--- a/webrtc/p2p/base/stunrequest_unittest.cc
+++ b/webrtc/p2p/base/stunrequest_unittest.cc
@@ -9,6 +9,7 @@
*/
#include "webrtc/p2p/base/stunrequest.h"
+#include "webrtc/base/fakeclock.h"
#include "webrtc/base/gunit.h"
#include "webrtc/base/helpers.h"
#include "webrtc/base/logging.h"
@@ -17,12 +18,6 @@
using namespace cricket;
-// STUN timeout (with all retries) is 9500ms.
-// Add some margin of error for slow bots.
-// TODO(deadbeef): Use simulated clock instead of just increasing timeouts to
-// fix flaky tests.
-static const int kTimeoutMs = 15000;
-
class StunRequestTest : public testing::Test,
public sigslot::has_slots<> {
public:
@@ -150,18 +145,21 @@ TEST_F(StunRequestTest, TestUnexpected) {
// Test that requests are sent at the right times, and that the 9th request
// (sent at 7900 ms) can be properly replied to.
TEST_F(StunRequestTest, TestBackoff) {
+ rtc::ScopedFakeClock fake_clock;
StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
int64_t start = rtc::TimeMillis();
manager_.Send(new StunRequestThunker(req, this));
StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
for (int i = 0; i < 9; ++i) {
- while (request_count_ == i)
- rtc::Thread::Current()->ProcessMessages(1);
+ while (request_count_ == i) {
+ rtc::Thread::Current()->ProcessMessages(0);
+ fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1));
+ }
honghaiz3 2016/09/06 21:26:27 I think the while loop can be replaced with SIMUL
skvlad 2016/09/06 21:50:29 Thanks for the suggestion! Didn't know about it. I
int64_t elapsed = rtc::TimeMillis() - start;
LOG(LS_INFO) << "STUN request #" << (i + 1)
<< " sent at " << elapsed << " ms";
- EXPECT_GE(TotalDelay(i + 1), elapsed);
+ EXPECT_EQ(TotalDelay(i), elapsed);
}
EXPECT_TRUE(manager_.CheckResponse(res));
@@ -174,11 +172,16 @@ TEST_F(StunRequestTest, TestBackoff) {
// Test that we timeout properly if no response is received in 9500 ms.
TEST_F(StunRequestTest, TestTimeout) {
+ rtc::ScopedFakeClock fake_clock;
StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
manager_.Send(new StunRequestThunker(req, this));
- rtc::Thread::Current()->ProcessMessages(kTimeoutMs);
+ // Simulate the 9500 ms STUN timeout in 100ms steps
+ for (int i = 0; i < 95; i++) {
+ rtc::Thread::Current()->ProcessMessages(0);
+ fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(100));
+ }
EXPECT_FALSE(manager_.CheckResponse(res));
EXPECT_TRUE(response_ == NULL);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698