diff --git a/core/tests/CMakeLists.txt b/core/tests/CMakeLists.txt index 53e6a2ed..d641a852 100644 --- a/core/tests/CMakeLists.txt +++ b/core/tests/CMakeLists.txt @@ -1,5 +1,5 @@ -add_executable(prometheus_test +add_executable(prometheus_core_test builder_test.cc check_names_test.cc counter_test.cc @@ -13,13 +13,13 @@ add_executable(prometheus_test utils_test.cc ) -target_link_libraries(prometheus_test +target_link_libraries(prometheus_core_test PRIVATE ${PROJECT_NAME}::core GTest::gmock_main ) add_test( - NAME prometheus_test - COMMAND prometheus_test + NAME prometheus_core_test + COMMAND prometheus_core_test ) diff --git a/pull/include/prometheus/exposer.h b/pull/include/prometheus/exposer.h index c730360c..0917ba98 100644 --- a/pull/include/prometheus/exposer.h +++ b/pull/include/prometheus/exposer.h @@ -26,6 +26,8 @@ class PROMETHEUS_CPP_PULL_EXPORT Exposer { ~Exposer(); void RegisterCollectable(const std::weak_ptr& collectable); + std::vector GetListeningPorts() const; + private: std::unique_ptr server_; std::vector> collectables_; diff --git a/pull/src/exposer.cc b/pull/src/exposer.cc index 966d04ae..ad22b7e0 100644 --- a/pull/src/exposer.cc +++ b/pull/src/exposer.cc @@ -30,4 +30,9 @@ void Exposer::RegisterCollectable( const std::weak_ptr& collectable) { collectables_.push_back(collectable); } + +std::vector Exposer::GetListeningPorts() const { + return server_->getListeningPorts(); +} + } // namespace prometheus diff --git a/pull/tests/CMakeLists.txt b/pull/tests/CMakeLists.txt index 26e40e61..729c6ee0 100644 --- a/pull/tests/CMakeLists.txt +++ b/pull/tests/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(integration) +add_subdirectory(unit) diff --git a/pull/tests/unit/BUILD.bazel b/pull/tests/unit/BUILD.bazel new file mode 100644 index 00000000..fc174146 --- /dev/null +++ b/pull/tests/unit/BUILD.bazel @@ -0,0 +1,13 @@ +cc_test( + name = "unit", + srcs = glob([ + "*.cc", + "*.h", + ]), + copts = ["-Iexternal/googletest/include"], + linkstatic = True, + deps = [ + "//pull", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/pull/tests/unit/CMakeLists.txt b/pull/tests/unit/CMakeLists.txt new file mode 100644 index 00000000..5c5c4857 --- /dev/null +++ b/pull/tests/unit/CMakeLists.txt @@ -0,0 +1,15 @@ + +add_executable(prometheus_pull_test + exposer_test.cc +) + +target_link_libraries(prometheus_pull_test + PRIVATE + ${PROJECT_NAME}::pull + GTest::gmock_main +) + +add_test( + NAME prometheus_pull_test + COMMAND prometheus_pull_test +) diff --git a/pull/tests/unit/exposer_test.cc b/pull/tests/unit/exposer_test.cc new file mode 100644 index 00000000..0c46f95b --- /dev/null +++ b/pull/tests/unit/exposer_test.cc @@ -0,0 +1,25 @@ +#include "prometheus/exposer.h" + +#include + +namespace prometheus { +namespace { + +using namespace testing; + +TEST(ExposerTest, listenOnDistinctPorts) { + Exposer firstExposer{"0.0.0.0:0"}; + auto firstExposerPorts = firstExposer.GetListeningPorts(); + ASSERT_EQ(1u, firstExposerPorts.size()); + EXPECT_NE(0, firstExposerPorts.front()); + + Exposer secondExposer{"0.0.0.0:0"}; + auto secondExposerPorts = secondExposer.GetListeningPorts(); + ASSERT_EQ(1u, secondExposerPorts.size()); + EXPECT_NE(0, secondExposerPorts.front()); + + EXPECT_NE(firstExposerPorts, secondExposerPorts); +} + +} // namespace +} // namespace prometheus