mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Fix thread safety issue
The test could fail if threads needed a bit more time to spin up. Test only change. PiperOrigin-RevId: 561097187
This commit is contained in:
committed by
Copybara-Service
parent
ea9e479981
commit
263b1b4e85
@@ -28,7 +28,7 @@ namespace {
|
||||
const int kMaxThreads = 5;
|
||||
}
|
||||
|
||||
TEST(MultiThreadExecutorTest, ConsructorDestructorWorks) {
|
||||
TEST(MultiThreadExecutorTest, ConstructorDestructorWorks) {
|
||||
MultiThreadExecutor executor(kMaxThreads);
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ TEST(MultiThreadExecutorTest, JobsExecuteInParallel) {
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < kMaxThreads; ++i) {
|
||||
executor.Execute([&count, &mutex, &test_cond, &thread_cond]() {
|
||||
executor.Execute([&]() {
|
||||
absl::MutexLock lock(&mutex);
|
||||
count++;
|
||||
test_cond.Signal();
|
||||
@@ -69,12 +69,9 @@ TEST(MultiThreadExecutorTest, JobsExecuteInParallel) {
|
||||
}
|
||||
|
||||
{
|
||||
absl::Duration duration = absl::Milliseconds(kMaxThreads * 100);
|
||||
absl::MutexLock lock(&mutex);
|
||||
while (count < kMaxThreads) {
|
||||
absl::Time start = absl::Now();
|
||||
if (test_cond.WaitWithTimeout(&mutex, duration)) break;
|
||||
duration -= absl::Now() - start;
|
||||
if (test_cond.WaitWithTimeout(&mutex, absl::Seconds(30))) break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,12 +79,9 @@ TEST(MultiThreadExecutorTest, JobsExecuteInParallel) {
|
||||
thread_cond.SignalAll();
|
||||
|
||||
{
|
||||
absl::Duration duration = absl::Milliseconds(kMaxThreads * 100);
|
||||
absl::MutexLock lock(&mutex);
|
||||
while (count > 0) {
|
||||
absl::Time start = absl::Now();
|
||||
if (test_cond.WaitWithTimeout(&mutex, duration)) break;
|
||||
duration -= absl::Now() - start;
|
||||
if (test_cond.WaitWithTimeout(&mutex, absl::Seconds(30))) break;
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(count, 0);
|
||||
|
||||
Reference in New Issue
Block a user