Fix race in unit test.

Summary: Avoid falling victim to race condition.

Test Plan: Run the unit test

Reviewers: sdong igor

CC: leveldb@

Task ID: #7312624

Blame Rev:
main
krad 10 years ago
parent 0a019d74a0
commit 6199cba998
  1. 28
      db/db_test.cc

@ -72,21 +72,39 @@ static std::string RandomString(Random* rnd, int len) {
namespace anon { namespace anon {
class AtomicCounter { class AtomicCounter {
private: private:
Env* env_;
port::Mutex mu_; port::Mutex mu_;
port::CondVar cond_count_;
int count_; int count_;
public: public:
AtomicCounter() : count_(0) { } AtomicCounter(Env* env = NULL) : env_(env), cond_count_(&mu_), count_(0) {}
void Increment() { void Increment() {
MutexLock l(&mu_); MutexLock l(&mu_);
count_++; count_++;
cond_count_.SignalAll();
} }
int Read() { int Read() {
MutexLock l(&mu_); MutexLock l(&mu_);
return count_; return count_;
} }
bool WaitFor(int count) {
MutexLock l(&mu_);
uint64_t start = env_->NowMicros();
while (count_ < count) {
uint64_t now = env_->NowMicros();
cond_count_.TimedWait(now + /*1s*/ 1 * 000 * 000);
if (env_->NowMicros() - start > /*1s*/ 1 * 000 * 000) {
return false;
}
}
return true;
}
void Reset() { void Reset() {
MutexLock l(&mu_); MutexLock l(&mu_);
count_ = 0; count_ = 0;
cond_count_.SignalAll();
} }
}; };
@ -159,7 +177,11 @@ class SpecialEnv : public EnvWrapper {
bool no_sleep_; bool no_sleep_;
explicit SpecialEnv(Env* base) explicit SpecialEnv(Env* base)
: EnvWrapper(base), rnd_(301), addon_time_(0), no_sleep_(false) { : EnvWrapper(base),
rnd_(301),
sleep_counter_(this),
addon_time_(0),
no_sleep_(false) {
delay_sstable_sync_.store(false, std::memory_order_release); delay_sstable_sync_.store(false, std::memory_order_release);
drop_writes_.store(false, std::memory_order_release); drop_writes_.store(false, std::memory_order_release);
no_space_.store(false, std::memory_order_release); no_space_.store(false, std::memory_order_release);
@ -7553,7 +7575,7 @@ TEST_F(DBTest, DropWrites) {
ASSERT_LT(CountFiles(), num_files + 3); ASSERT_LT(CountFiles(), num_files + 3);
// Check that compaction attempts slept after errors // Check that compaction attempts slept after errors
ASSERT_GE(env_->sleep_counter_.Read(), 5); ASSERT_TRUE(env_->sleep_counter_.WaitFor(5));
} while (ChangeCompactOptions()); } while (ChangeCompactOptions());
} }

Loading…
Cancel
Save