ClickHouse/src/Storages/tests/gtest_background_executor.cpp
Ivan 495c6e03aa
Replace all Context references with std::weak_ptr (#22297)
* Replace all Context references with std::weak_ptr

* Fix shared context captured by value

* Fix build

* Fix Context with named sessions

* Fix copy context

* Fix gcc build

* Merge with master and fix build

* Fix gcc-9 build
2021-04-11 02:33:54 +03:00

63 lines
1.6 KiB
C++

#include <gtest/gtest.h>
#include <Storages/MergeTree/BackgroundJobsExecutor.h>
#include <Common/CurrentMetrics.h>
#include <Common/tests/gtest_global_context.h>
#include <memory>
#include <chrono>
using namespace std::chrono_literals;
namespace CurrentMetrics
{
extern const Metric BackgroundPoolTask;
}
using namespace DB;
static std::atomic<Int64> counter{0};
class TestJobExecutor : public IBackgroundJobExecutor
{
public:
explicit TestJobExecutor(ContextPtr local_context)
:IBackgroundJobExecutor(
local_context,
BackgroundTaskSchedulingSettings{},
{PoolConfig{PoolType::MERGE_MUTATE, 4, CurrentMetrics::BackgroundPoolTask}})
{}
protected:
String getBackgroundTaskName() const override
{
return "TestTask";
}
std::optional<JobAndPool> getBackgroundJob() override
{
return JobAndPool{[] { std::this_thread::sleep_for(1s); counter++; return true; }, PoolType::MERGE_MUTATE};
}
};
using TestExecutorPtr = std::unique_ptr<TestJobExecutor>;
TEST(BackgroundExecutor, TestMetric)
{
const auto & context_holder = getContext();
std::vector<TestExecutorPtr> executors;
for (size_t i = 0; i < 100; ++i)
executors.emplace_back(std::make_unique<TestJobExecutor>(context_holder.context));
for (size_t i = 0; i < 100; ++i)
executors[i]->start();
for (size_t i = 0; i < 100; ++i)
{
EXPECT_TRUE(CurrentMetrics::values[CurrentMetrics::BackgroundPoolTask].load() <= 4);
std::this_thread::sleep_for(200ms);
}
for (size_t i = 0; i < 100; ++i)
executors[i]->finish();
/// Sanity check
EXPECT_TRUE(counter > 50);
}