Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add rand sleep ut #1844

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading