ClickHouse/src/Storages/tests/gtest_background_executor.cpp

63 lines
1.6 KiB
C++
Raw Normal View History

2020-10-15 10:22:02 +00:00
#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:
TestJobExecutor(Context & context)
:IBackgroundJobExecutor(
context,
TaskSleepSettings{},
{PoolConfig{PoolType::MERGE_MUTATE, 4, CurrentMetrics::BackgroundPoolTask}})
{}
protected:
String getBackgroundJobName() const override
{
return "TestJob";
}
std::optional<JobAndPool> getBackgroundJob() override
{
return JobAndPool{[] { std::this_thread::sleep_for(1s); counter++; }, PoolType::MERGE_MUTATE};
}
};
using TestExecutorPtr = std::unique_ptr<TestJobExecutor>;
TEST(BackgroundExecutor, TestMetric)
{
auto & context_holder = getContext();
std::vector<TestExecutorPtr> executors;
for (size_t i = 0; i < 100; ++i)
executors.emplace_back(std::make_unique<TestJobExecutor>(const_cast<Context &>(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);
}