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] Build the singleton test on windows. #3183

Merged
merged 13 commits into from
Dec 17, 2024
106 changes: 50 additions & 56 deletions api/test/singleton/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
89 changes: 64 additions & 25 deletions api/test/singleton/singleton_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
#include <gtest/gtest.h>
#include <stdint.h>

/*
TODO:
Once singleton are supported for windows,
expand this test to use ::LoadLibrary, ::GetProcAddress, ::FreeLibrary
*/
#ifndef _WIN32
#ifdef _WIN32
# include <windows.h>
#else
# include <dlfcn.h>
#endif

Expand Down Expand Up @@ -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<void (*)()>(GetProcAddress(component_g, "do_something_in_g"));
# else
auto *func_g = reinterpret_cast<void (*)()>(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<void (*)()>(GetProcAddress(component_h, "do_something_in_h"));
# else
auto *func_h = reinterpret_cast<void (*)()>(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;
Expand Down Expand Up @@ -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
marcalff marked this conversation as resolved.
Show resolved Hide resolved
#else
# define RUN_FAILING_WINDOWS_TEST 1
#endif

TEST(SingletonTest, Uniqueness)
{
do_something();
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions ci/do_ci.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 -- //...
Expand Down
Loading