Add unit test for proper sequencing of task execution and synchronous cleanup.
#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>
using vespalib::Executor;
using namespace proton;
using Task = Executor::Task;
+using vespalib::makeLambdaTask;
namespace {
@@ -83,4 +85,32 @@ TYPED_TEST(ScheduledExecutorTest, test_drop_handle) {
EXPECT_TRUE(latch1.await(60s));
}
+TYPED_TEST(ScheduledExecutorTest, test_only_one_instance_running) {
+ vespalib::Gate latch;
+ std::atomic<uint64_t> counter = 0;
+ auto handleA = this->timer->scheduleAtFixedRate(makeLambdaTask([&]() { counter++; latch.await();}), 0ms, 1ms);
+ std::this_thread::sleep_for(2s);
+ EXPECT_EQ(1, counter);
+ latch.countDown();
+ std::this_thread::sleep_for(2s);
+ EXPECT_GT(counter, 10);
+}
+
+TYPED_TEST(ScheduledExecutorTest, test_sync_delete) {
+ vespalib::Gate latch;
+ std::atomic<uint64_t> counter = 0;
+ std::atomic<uint64_t> reset_counter = 0;
+ 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);
+ EXPECT_EQ(1, counter);
+ EXPECT_EQ(0, reset_counter);
+ latch.countDown();
+ std::this_thread::sleep_for(2s);
+ EXPECT_EQ(1, counter);
+ EXPECT_GT(reset_counter, 10);
+ EXPECT_EQ(nullptr, handleA.get());
+ EXPECT_FALSE(nullptr == handleB.get());
+}
+
GTEST_MAIN_RUN_ALL_TESTS()
