summaryrefslogtreecommitdiffstats
path: root/searchcore
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-05-15 10:05:08 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2023-05-15 10:05:08 +0000
commitd353b99f58fa5aef9da820f0a561748d2578a9f3 (patch)
tree5bdc7f081caa4d201f066ae847219a7d6c28a147 /searchcore
parentaa9b9b6b556ec1f378dced42eb4319ebcd14f168 (diff)
Ensure thread safety of handlA.
Use a loop with short wait to speed up test.
Diffstat (limited to 'searchcore')
-rw-r--r--searchcore/src/tests/proton/common/timer/timer_test.cpp16
1 files changed, 10 insertions, 6 deletions
diff --git a/searchcore/src/tests/proton/common/timer/timer_test.cpp b/searchcore/src/tests/proton/common/timer/timer_test.cpp
index 4ff970df84e..63b4a1a6bf3 100644
--- a/searchcore/src/tests/proton/common/timer/timer_test.cpp
+++ b/searchcore/src/tests/proton/common/timer/timer_test.cpp
@@ -5,7 +5,6 @@
#include <vespa/searchcore/proton/common/scheduledexecutor.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/count_down_latch.h>
-#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/util/threadstackexecutor.h>
#include <vespa/vespalib/util/lambdatask.h>
#include <thread>
@@ -90,7 +89,7 @@ TYPED_TEST(ScheduledExecutorTest, test_only_one_instance_running) {
std::this_thread::sleep_for(2s);
EXPECT_EQ(1, counter);
latch.countDown();
- std::this_thread::sleep_for(2s);
+ while (counter <= 10) { std::this_thread::sleep_for(1ms); }
EXPECT_GT(counter, 10);
}
@@ -98,15 +97,20 @@ TYPED_TEST(ScheduledExecutorTest, test_sync_delete) {
vespalib::Gate latch;
std::atomic<uint64_t> counter = 0;
std::atomic<uint64_t> reset_counter = 0;
+ std::mutex handleLock;
auto handleA = this->timer->scheduleAtFixedRate(makeLambdaTask([&]() { counter++; latch.await();}), 0ms, 1ms);
- auto handleB = this->timer->scheduleAtFixedRate(makeLambdaTask([&]() { handleA.reset(); reset_counter++; }), 0ms, 1ms);
- std::this_thread::sleep_for(2s);
+ auto handleB = this->timer->scheduleAtFixedRate(makeLambdaTask([&]() {
+ std::lock_guard guard(handleLock);
+ handleA.reset();
+ reset_counter++;
+ }), 0ms, 1ms);
+ while (counter < 1) { std::this_thread::sleep_for(1ms); }
EXPECT_EQ(1, counter);
EXPECT_EQ(0, reset_counter);
latch.countDown();
- std::this_thread::sleep_for(2s);
+ while (reset_counter <= 10) { std::this_thread::sleep_for(1ms); }
EXPECT_EQ(1, counter);
- EXPECT_GT(reset_counter, 10);
+ std::lock_guard guard(handleLock);
EXPECT_EQ(nullptr, handleA.get());
EXPECT_FALSE(nullptr == handleB.get());
}