[third_party][leveldb] Ensure tests do not flake.

Use a timeout in a loop instead of a single timeout in multithreaded
leveldb tests to ensure there is no flake.

TESTED=levedb_tests

LE-571 #done

Change-Id: I3a90258b06d5845ff28634fddc3ea500a47020bf
diff --git a/util/env_test.cc b/util/env_test.cc
index 070109b..f0cb541 100644
--- a/util/env_test.cc
+++ b/util/env_test.cc
@@ -21,14 +21,13 @@
 class EnvTest {
  public:
   Env* env_;
-  EnvTest() : env_(Env::Default()) { }
+  EnvTest() : env_(Env::Default()) {}
 };
 
 static void SetBool(void* ptr) {
   reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
 }
 
-
 TEST(EnvTest, ReadWrite) {
   Random rnd(test::RandomSeed());
 
@@ -79,7 +78,9 @@
 TEST(EnvTest, RunImmediately) {
   port::AtomicPointer called(nullptr);
   env_->Schedule(&SetBool, &called);
-  env_->SleepForMicroseconds(kDelayMicros);
+  while (called.NoBarrier_Load() == nullptr) {
+    env_->SleepForMicroseconds(kDelayMicros);
+  }
   ASSERT_TRUE(called.NoBarrier_Load() != nullptr);
 }
 
@@ -87,15 +88,15 @@
   port::AtomicPointer last_id(nullptr);
 
   struct CB {
-    port::AtomicPointer* last_id_ptr;   // Pointer to shared slot
-    uintptr_t id;             // Order# for the execution of this callback
+    port::AtomicPointer* last_id_ptr;  // Pointer to shared slot
+    uintptr_t id;  // Order# for the execution of this callback
 
-    CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
+    CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) {}
 
     static void Run(void* v) {
       CB* cb = reinterpret_cast<CB*>(v);
       void* cur = cb->last_id_ptr->NoBarrier_Load();
-      ASSERT_EQ(cb->id-1, reinterpret_cast<uintptr_t>(cur));
+      ASSERT_EQ(cb->id - 1, reinterpret_cast<uintptr_t>(cur));
       cb->last_id_ptr->Release_Store(reinterpret_cast<void*>(cb->id));
     }
   };
@@ -110,8 +111,14 @@
   env_->Schedule(&CB::Run, &cb3);
   env_->Schedule(&CB::Run, &cb4);
 
-  env_->SleepForMicroseconds(kDelayMicros);
-  void* cur = last_id.Acquire_Load();
+  void* cur;
+  while (true) {
+    env_->SleepForMicroseconds(kDelayMicros);
+    cur = last_id.Acquire_Load();
+    if (reinterpret_cast<uintptr_t>(cur) == 4) {
+      break;
+    }
+  }
   ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
 }
 
@@ -120,7 +127,7 @@
   int val GUARDED_BY(mu);
   int num_running GUARDED_BY(mu);
 
-  State(int val, int num_running) : val(val), num_running(num_running) { }
+  State(int val, int num_running) : val(val), num_running(num_running) {}
 };
 
 static void ThreadBody(void* arg) {
@@ -159,8 +166,8 @@
   ASSERT_TRUE(!env_->FileExists(non_existent_file));
 
   RandomAccessFile* random_access_file;
-  Status status = env_->NewRandomAccessFile(
-      non_existent_file, &random_access_file);
+  Status status =
+      env_->NewRandomAccessFile(non_existent_file, &random_access_file);
   ASSERT_TRUE(status.IsNotFound());
 
   SequentialFile* sequential_file;