Skip to content

Commit

Permalink
chore: add rand sleep ut (#1844)
Browse files Browse the repository at this point in the history
  • Loading branch information
catdogpandas authored Nov 6, 2024
1 parent 67e0d7b commit 0b6ddd2
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions core/unittest/prometheus/TargetSubscriberSchedulerUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TargetSubscriberSchedulerUnittest : public ::testing::Test {
void OnInitScrapeJobEvent();
void TestProcess();
void TestParseTargetGroups();
void TestBuildScrapeSchedulerSet();

protected:
void SetUp() override {
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -160,7 +180,7 @@ void TargetSubscriberSchedulerUnittest::TestProcess() {
// if status code is 200
mHttpResponse.SetStatusCode(200);
targetSubscriber->OnSubscription(mHttpResponse, 0);
APSARA_TEST_EQUAL(2UL, targetSubscriber->mScrapeSchedulerMap.size());
APSARA_TEST_EQUAL(3UL, targetSubscriber->mScrapeSchedulerMap.size());
}

void TargetSubscriberSchedulerUnittest::TestParseTargetGroups() {
Expand All @@ -169,12 +189,34 @@ void TargetSubscriberSchedulerUnittest::TestParseTargetGroups() {

std::vector<Labels> newScrapeSchedulerSet;
APSARA_TEST_TRUE(targetSubscriber->ParseScrapeSchedulerGroup(*mHttpResponse.GetBody<string>(), newScrapeSchedulerSet));
APSARA_TEST_EQUAL(2UL, newScrapeSchedulerSet.size());
APSARA_TEST_EQUAL(3UL, newScrapeSchedulerSet.size());
}

void TargetSubscriberSchedulerUnittest::TestBuildScrapeSchedulerSet() {
// prepare data
std::shared_ptr<TargetSubscriberScheduler> targetSubscriber = std::make_shared<TargetSubscriberScheduler>();
APSARA_TEST_TRUE(targetSubscriber->Init(mConfig["ScrapeConfig"]));
std::vector<Labels> newScrapeSchedulerSet;
APSARA_TEST_TRUE(targetSubscriber->ParseScrapeSchedulerGroup(*mHttpResponse.GetBody<string>(), newScrapeSchedulerSet));
APSARA_TEST_EQUAL(3UL, newScrapeSchedulerSet.size());

auto result = targetSubscriber->BuildScrapeSchedulerSet(newScrapeSchedulerSet);

vector<pair<string, chrono::steady_clock::time_point>> 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

Expand Down

0 comments on commit 0b6ddd2

Please sign in to comment.