From 2bab7af0be4f1d6389d56caaf9c0823016d77db7 Mon Sep 17 00:00:00 2001 From: liqiang Date: Mon, 14 Oct 2024 05:53:48 +0000 Subject: [PATCH] chore: add rand sleep ut --- .../TargetSubscriberSchedulerUnittest.cpp | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/core/unittest/prometheus/TargetSubscriberSchedulerUnittest.cpp b/core/unittest/prometheus/TargetSubscriberSchedulerUnittest.cpp index 983e401253..0fff0eac2a 100644 --- a/core/unittest/prometheus/TargetSubscriberSchedulerUnittest.cpp +++ b/core/unittest/prometheus/TargetSubscriberSchedulerUnittest.cpp @@ -34,6 +34,7 @@ class TargetSubscriberSchedulerUnittest : public ::testing::Test { void OnInitScrapeJobEvent(); void TestProcess(); void TestParseTargetGroups(); + void TestBuildScrapeSchedulerSet(); protected: void SetUp() override { @@ -125,6 +126,25 @@ class TargetSubscriberSchedulerUnittest : public ::testing::Test { "__meta_kubernetes_service_labelpresent_component": "true", "__meta_kubernetes_endpoint_ready": "true" } + }, + { + "targets": [ + "192.168.22.33:6443" + ], + "labels": { + "__address__": "192.168.22.33:6443", + "__meta_kubernetes_endpoint_port_protocol": "TCP", + "__meta_kubernetes_service_label_provider": "kubernetes", + "__meta_kubernetes_endpoints_name": "kubernetes", + "__meta_kubernetes_service_name": "kubernetes", + "__meta_kubernetes_endpoints_labelpresent_endpointslice_kubernetes_io_skip_mirror": "true", + "__meta_kubernetes_service_labelpresent_provider": "true", + "__meta_kubernetes_endpoint_port_name": "https", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_service_label_component": "apiserver", + "__meta_kubernetes_service_labelpresent_component": "true", + "__meta_kubernetes_endpoint_ready": "true" + } } ])JSON"; } @@ -160,7 +180,7 @@ void TargetSubscriberSchedulerUnittest::TestProcess() { // if status code is 200 mHttpResponse.mStatusCode = 200; targetSubscriber->OnSubscription(mHttpResponse, 0); - APSARA_TEST_EQUAL(2UL, targetSubscriber->mScrapeSchedulerMap.size()); + APSARA_TEST_EQUAL(3UL, targetSubscriber->mScrapeSchedulerMap.size()); } void TargetSubscriberSchedulerUnittest::TestParseTargetGroups() { @@ -169,12 +189,34 @@ void TargetSubscriberSchedulerUnittest::TestParseTargetGroups() { std::vector newScrapeSchedulerSet; APSARA_TEST_TRUE(targetSubscriber->ParseScrapeSchedulerGroup(mHttpResponse.mBody, newScrapeSchedulerSet)); - APSARA_TEST_EQUAL(2UL, newScrapeSchedulerSet.size()); + APSARA_TEST_EQUAL(3UL, newScrapeSchedulerSet.size()); +} + +void TargetSubscriberSchedulerUnittest::TestBuildScrapeSchedulerSet() { + // prepare data + std::shared_ptr targetSubscriber = std::make_shared(); + APSARA_TEST_TRUE(targetSubscriber->Init(mConfig["ScrapeConfig"])); + std::vector newScrapeSchedulerSet; + APSARA_TEST_TRUE(targetSubscriber->ParseScrapeSchedulerGroup(mHttpResponse.mBody, newScrapeSchedulerSet)); + APSARA_TEST_EQUAL(3UL, newScrapeSchedulerSet.size()); + + auto result = targetSubscriber->BuildScrapeSchedulerSet(newScrapeSchedulerSet); + + vector> startTimeList; + startTimeList.reserve(result.size()); + for (auto& it : result) { + startTimeList.emplace_back(it.second->GetId(), it.second->GetNextExecTime()); + } + APSARA_TEST_EQUAL(3UL, startTimeList.size()); + APSARA_TEST_NOT_EQUAL(startTimeList[0].second, startTimeList[1].second); + APSARA_TEST_NOT_EQUAL(startTimeList[1].second, startTimeList[2].second); + APSARA_TEST_NOT_EQUAL(startTimeList[0].second, startTimeList[2].second); } UNIT_TEST_CASE(TargetSubscriberSchedulerUnittest, OnInitScrapeJobEvent) UNIT_TEST_CASE(TargetSubscriberSchedulerUnittest, TestProcess) UNIT_TEST_CASE(TargetSubscriberSchedulerUnittest, TestParseTargetGroups) +UNIT_TEST_CASE(TargetSubscriberSchedulerUnittest, TestBuildScrapeSchedulerSet) } // namespace logtail