From 902ee88bef59dccb84ea0405657d26c77182f0e3 Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 17 Dec 2024 19:49:16 +0100 Subject: [PATCH] [TEST] Build the singleton test on windows. (#3183) --- api/test/singleton/CMakeLists.txt | 106 +++++++++++++-------------- api/test/singleton/singleton_test.cc | 89 +++++++++++++++------- ci/do_ci.ps1 | 2 + 3 files changed, 116 insertions(+), 81 deletions(-) diff --git a/api/test/singleton/CMakeLists.txt b/api/test/singleton/CMakeLists.txt index 450308cd82..7c30d6e571 100644 --- a/api/test/singleton/CMakeLists.txt +++ b/api/test/singleton/CMakeLists.txt @@ -3,59 +3,53 @@ include(GoogleTest) -# Header only singletons are not available in windows yet. - -if(NOT WIN32) - - add_library(component_a STATIC component_a.cc) - target_link_libraries(component_a opentelemetry_api) - - add_library(component_b STATIC component_b.cc) - target_link_libraries(component_b opentelemetry_api) - - add_library(component_c SHARED component_c.cc) - set_target_properties(component_c PROPERTIES CXX_VISIBILITY_PRESET default) - target_link_libraries(component_c opentelemetry_api) - - add_library(component_d SHARED component_d.cc) - set_target_properties(component_d PROPERTIES CXX_VISIBILITY_PRESET hidden) - target_link_libraries(component_d opentelemetry_api) - - add_library(component_e SHARED component_e.cc) - set_target_properties(component_e PROPERTIES CXX_VISIBILITY_PRESET default) - target_link_libraries(component_e opentelemetry_api) - - add_library(component_f SHARED component_f.cc) - set_target_properties(component_f PROPERTIES CXX_VISIBILITY_PRESET hidden) - target_link_libraries(component_f opentelemetry_api) - - add_library(component_g SHARED component_g.cc) - set_target_properties(component_g PROPERTIES CXX_VISIBILITY_PRESET default) - target_link_libraries(component_g opentelemetry_api) - - add_library(component_h SHARED component_h.cc) - set_target_properties(component_h PROPERTIES CXX_VISIBILITY_PRESET hidden) - target_link_libraries(component_h opentelemetry_api) - - add_executable(singleton_test singleton_test.cc) - - # Not linking with component_g and component_h on purpose - target_link_libraries( - singleton_test - component_a - component_b - component_c - component_d - component_e - component_f - ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} - ${CMAKE_DL_LIBS} - opentelemetry_api) - - gtest_add_tests( - TARGET singleton_test - TEST_PREFIX singleton. - TEST_LIST singleton_test) - -endif() +add_library(component_a STATIC component_a.cc) +target_link_libraries(component_a opentelemetry_api) + +add_library(component_b STATIC component_b.cc) +target_link_libraries(component_b opentelemetry_api) + +add_library(component_c SHARED component_c.cc) +set_target_properties(component_c PROPERTIES CXX_VISIBILITY_PRESET default) +target_link_libraries(component_c opentelemetry_api) + +add_library(component_d SHARED component_d.cc) +set_target_properties(component_d PROPERTIES CXX_VISIBILITY_PRESET hidden) +target_link_libraries(component_d opentelemetry_api) + +add_library(component_e SHARED component_e.cc) +set_target_properties(component_e PROPERTIES CXX_VISIBILITY_PRESET default) +target_link_libraries(component_e opentelemetry_api) + +add_library(component_f SHARED component_f.cc) +set_target_properties(component_f PROPERTIES CXX_VISIBILITY_PRESET hidden) +target_link_libraries(component_f opentelemetry_api) + +add_library(component_g SHARED component_g.cc) +set_target_properties(component_g PROPERTIES CXX_VISIBILITY_PRESET default) +target_link_libraries(component_g opentelemetry_api) + +add_library(component_h SHARED component_h.cc) +set_target_properties(component_h PROPERTIES CXX_VISIBILITY_PRESET hidden) +target_link_libraries(component_h opentelemetry_api) + +add_executable(singleton_test singleton_test.cc) + +# Not linking with component_g and component_h on purpose +target_link_libraries( + singleton_test + component_a + component_b + component_c + component_d + component_e + component_f + ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ${CMAKE_DL_LIBS} + opentelemetry_api) + +gtest_add_tests( + TARGET singleton_test + TEST_PREFIX singleton. + TEST_LIST singleton_test) diff --git a/api/test/singleton/singleton_test.cc b/api/test/singleton/singleton_test.cc index d212221118..8b8cb1587d 100644 --- a/api/test/singleton/singleton_test.cc +++ b/api/test/singleton/singleton_test.cc @@ -4,12 +4,9 @@ #include #include -/* - TODO: - Once singleton are supported for windows, - expand this test to use ::LoadLibrary, ::GetProcAddress, ::FreeLibrary -*/ -#ifndef _WIN32 +#ifdef _WIN32 +# include +#else # include #endif @@ -58,28 +55,57 @@ void do_something() #ifndef BAZEL_BUILD /* Call do_something_in_g() */ +# ifdef _WIN32 + HMODULE component_g = LoadLibraryA("component_g.dll"); +# else void *component_g = dlopen("libcomponent_g.so", RTLD_NOW); +# endif + EXPECT_NE(component_g, nullptr); +# ifdef _WIN32 + auto *func_g = reinterpret_cast(GetProcAddress(component_g, "do_something_in_g")); +# else auto *func_g = reinterpret_cast(dlsym(component_g, "do_something_in_g")); +# endif + EXPECT_NE(func_g, nullptr); (*func_g)(); +# ifdef _WIN32 + FreeLibrary(component_g); +# else dlclose(component_g); +# endif /* Call do_something_in_h() */ +# ifdef _WIN32 + HMODULE component_h = LoadLibraryA("component_h.dll"); +# else void *component_h = dlopen("libcomponent_h.so", RTLD_NOW); +# endif + EXPECT_NE(component_h, nullptr); +# ifdef _WIN32 + auto *func_h = reinterpret_cast(GetProcAddress(component_h, "do_something_in_h")); +# else auto *func_h = reinterpret_cast(dlsym(component_h, "do_something_in_h")); +# endif + EXPECT_NE(func_h, nullptr); (*func_h)(); +# ifdef _WIN32 + FreeLibrary(component_h); +# else dlclose(component_h); -#endif +# endif + +#endif /* BAZEL_BUILD */ } int span_a_lib_count = 0; @@ -316,6 +342,14 @@ void cleanup_otel() trace_api::Provider::SetTracerProvider(provider); } +// TODO: Remove once windows api singletons are supported. +// See https://github.com/open-telemetry/opentelemetry-cpp/issues/2534 +#ifdef _WIN32 +# define RUN_FAILING_WINDOWS_TEST 0 +#else +# define RUN_FAILING_WINDOWS_TEST 1 +#endif + TEST(SingletonTest, Uniqueness) { do_something(); @@ -357,26 +391,31 @@ TEST(SingletonTest, Uniqueness) EXPECT_EQ(span_b_lib_count, 1); EXPECT_EQ(span_b_f1_count, 2); EXPECT_EQ(span_b_f2_count, 1); - EXPECT_EQ(span_c_lib_count, 1); - EXPECT_EQ(span_c_f1_count, 2); - EXPECT_EQ(span_c_f2_count, 1); - EXPECT_EQ(span_d_lib_count, 1); - EXPECT_EQ(span_d_f1_count, 2); - EXPECT_EQ(span_d_f2_count, 1); - EXPECT_EQ(span_e_lib_count, 1); - EXPECT_EQ(span_e_f1_count, 2); - EXPECT_EQ(span_e_f2_count, 1); - EXPECT_EQ(span_f_lib_count, 1); - EXPECT_EQ(span_f_f1_count, 2); - EXPECT_EQ(span_f_f2_count, 1); + +#if RUN_FAILING_WINDOWS_TEST + EXPECT_EQ(span_c_lib_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_c_f1_count, 2); // Fails with shared libraries on Windows + EXPECT_EQ(span_c_f2_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_d_lib_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_d_f1_count, 2); // Fails with shared libraries on Windows + EXPECT_EQ(span_d_f2_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_e_lib_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_e_f1_count, 2); // Fails with shared libraries on Windows + EXPECT_EQ(span_e_f2_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_f_lib_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_f_f1_count, 2); // Fails with shared libraries on Windows + EXPECT_EQ(span_f_f2_count, 1); // Fails with shared libraries on Windows +#endif #ifndef BAZEL_BUILD - EXPECT_EQ(span_g_lib_count, 1); - EXPECT_EQ(span_g_f1_count, 2); - EXPECT_EQ(span_g_f2_count, 1); - EXPECT_EQ(span_h_lib_count, 1); - EXPECT_EQ(span_h_f1_count, 2); - EXPECT_EQ(span_h_f2_count, 1); +# if RUN_FAILING_WINDOWS_TEST + EXPECT_EQ(span_g_lib_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_g_f1_count, 2); // Fails with shared libraries on Windows + EXPECT_EQ(span_g_f2_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_h_lib_count, 1); // Fails with shared libraries on Windows + EXPECT_EQ(span_h_f1_count, 2); // Fails with shared libraries on Windows + EXPECT_EQ(span_h_f2_count, 1); // Fails with shared libraries on Windows +# endif #endif EXPECT_EQ(unknown_span_count, 0); diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index e9c1f64646..a04ec938f5 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -27,6 +27,8 @@ $PLUGIN_DIR = Join-Path "$SRC_DIR" "plugin" $VCPKG_DIR = Join-Path "$SRC_DIR" "tools" "vcpkg" +$Env:CTEST_OUTPUT_ON_FAILURE = "1" + switch ($action) { "bazel.build" { bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS --action_env=VCPKG_DIR=$VCPKG_DIR --deleted_packages=opentracing-shim -- //...