Fix the test WebSocket...UnlockEndpointIsDelayed

Test WebSocketEndpointLockManagerTest.UnlockEndpointIsDelayed failed on
the Chromium Memory FYI on Chromium Mac (valgrind)(2) bot. Change the
test to measure the time taken, rather than wait the expected amount
of time, which should make it more deterministic.

BUG=442756, 451999
TEST=net_unittests

Review URL: https://codereview.chromium.org/871843003

Cr-Commit-Position: refs/heads/master@{#313243}
diff --git a/net/socket/websocket_endpoint_lock_manager_unittest.cc b/net/socket/websocket_endpoint_lock_manager_unittest.cc
index 2974b5a3..1603a3b 100644
--- a/net/socket/websocket_endpoint_lock_manager_unittest.cc
+++ b/net/socket/websocket_endpoint_lock_manager_unittest.cc
@@ -92,6 +92,23 @@
   bool called_;
 };
 
+class BlockingWaiter : public FakeWaiter {
+ public:
+  void WaitForLock() {
+    while (!called()) {
+      run_loop_.Run();
+    }
+  }
+
+  void GotEndpointLock() override {
+    FakeWaiter::GotEndpointLock();
+    run_loop_.Quit();
+  }
+
+ private:
+  base::RunLoop run_loop_;
+};
+
 class WebSocketEndpointLockManagerTest : public ::testing::Test {
  protected:
   WebSocketEndpointLockManagerTest()
@@ -273,21 +290,23 @@
 
 // UnlockEndpoint() should normally have a delay.
 TEST_F(WebSocketEndpointLockManagerTest, UnlockEndpointIsDelayed) {
-  const base::TimeDelta one_millisecond = base::TimeDelta::FromMilliseconds(1);
-  instance()->SetUnlockDelayForTesting(one_millisecond);
-  FakeWaiter waiters[2];
-  EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &waiters[0]));
-  EXPECT_EQ(ERR_IO_PENDING,
-            instance()->LockEndpoint(DummyEndpoint(), &waiters[1]));
+  using base::TimeTicks;
 
+  const base::TimeDelta unlock_delay = base::TimeDelta::FromMilliseconds(1);
+  instance()->SetUnlockDelayForTesting(unlock_delay);
+  FakeWaiter fake_waiter;
+  BlockingWaiter blocking_waiter;
+  EXPECT_EQ(OK, instance()->LockEndpoint(DummyEndpoint(), &fake_waiter));
+  EXPECT_EQ(ERR_IO_PENDING,
+            instance()->LockEndpoint(DummyEndpoint(), &blocking_waiter));
+
+  TimeTicks before_unlock = TimeTicks::Now();
   instance()->UnlockEndpoint(DummyEndpoint());
   RunUntilIdle();
-  EXPECT_FALSE(waiters[1].called());
-  base::RunLoop run_loop;
-  base::MessageLoop::current()->PostDelayedTask(
-      FROM_HERE, run_loop.QuitClosure(), one_millisecond);
-  run_loop.Run();
-  EXPECT_TRUE(waiters[1].called());
+  EXPECT_FALSE(blocking_waiter.called());
+  blocking_waiter.WaitForLock();
+  TimeTicks after_unlock = TimeTicks::Now();
+  EXPECT_GE(after_unlock - before_unlock, unlock_delay);
   instance()->SetUnlockDelayForTesting(base::TimeDelta());
   UnlockDummyEndpoint(1);
 }