#include #include #include #include #include #include using namespace std::chrono_literals; namespace CurrentMetrics { extern const Metric BackgroundPoolTask; } using namespace DB; static std::atomic 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 getBackgroundJob() override { return JobAndPool{[] { std::this_thread::sleep_for(1s); counter++; }, PoolType::MERGE_MUTATE}; } }; using TestExecutorPtr = std::unique_ptr; TEST(BackgroundExecutor, TestMetric) { auto & context_holder = getContext(); std::vector executors; for (size_t i = 0; i < 100; ++i) executors.emplace_back(std::make_unique(const_cast(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); }