Skip to content

Commit 902ee88

Browse files
authored
[TEST] Build the singleton test on windows. (open-telemetry#3183)
1 parent 1cbea09 commit 902ee88

File tree

3 files changed

+116
-81
lines changed

3 files changed

+116
-81
lines changed

api/test/singleton/CMakeLists.txt

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,53 @@
33

44
include(GoogleTest)
55

6-
# Header only singletons are not available in windows yet.
7-
8-
if(NOT WIN32)
9-
10-
add_library(component_a STATIC component_a.cc)
11-
target_link_libraries(component_a opentelemetry_api)
12-
13-
add_library(component_b STATIC component_b.cc)
14-
target_link_libraries(component_b opentelemetry_api)
15-
16-
add_library(component_c SHARED component_c.cc)
17-
set_target_properties(component_c PROPERTIES CXX_VISIBILITY_PRESET default)
18-
target_link_libraries(component_c opentelemetry_api)
19-
20-
add_library(component_d SHARED component_d.cc)
21-
set_target_properties(component_d PROPERTIES CXX_VISIBILITY_PRESET hidden)
22-
target_link_libraries(component_d opentelemetry_api)
23-
24-
add_library(component_e SHARED component_e.cc)
25-
set_target_properties(component_e PROPERTIES CXX_VISIBILITY_PRESET default)
26-
target_link_libraries(component_e opentelemetry_api)
27-
28-
add_library(component_f SHARED component_f.cc)
29-
set_target_properties(component_f PROPERTIES CXX_VISIBILITY_PRESET hidden)
30-
target_link_libraries(component_f opentelemetry_api)
31-
32-
add_library(component_g SHARED component_g.cc)
33-
set_target_properties(component_g PROPERTIES CXX_VISIBILITY_PRESET default)
34-
target_link_libraries(component_g opentelemetry_api)
35-
36-
add_library(component_h SHARED component_h.cc)
37-
set_target_properties(component_h PROPERTIES CXX_VISIBILITY_PRESET hidden)
38-
target_link_libraries(component_h opentelemetry_api)
39-
40-
add_executable(singleton_test singleton_test.cc)
41-
42-
# Not linking with component_g and component_h on purpose
43-
target_link_libraries(
44-
singleton_test
45-
component_a
46-
component_b
47-
component_c
48-
component_d
49-
component_e
50-
component_f
51-
${GTEST_BOTH_LIBRARIES}
52-
${CMAKE_THREAD_LIBS_INIT}
53-
${CMAKE_DL_LIBS}
54-
opentelemetry_api)
55-
56-
gtest_add_tests(
57-
TARGET singleton_test
58-
TEST_PREFIX singleton.
59-
TEST_LIST singleton_test)
60-
61-
endif()
6+
add_library(component_a STATIC component_a.cc)
7+
target_link_libraries(component_a opentelemetry_api)
8+
9+
add_library(component_b STATIC component_b.cc)
10+
target_link_libraries(component_b opentelemetry_api)
11+
12+
add_library(component_c SHARED component_c.cc)
13+
set_target_properties(component_c PROPERTIES CXX_VISIBILITY_PRESET default)
14+
target_link_libraries(component_c opentelemetry_api)
15+
16+
add_library(component_d SHARED component_d.cc)
17+
set_target_properties(component_d PROPERTIES CXX_VISIBILITY_PRESET hidden)
18+
target_link_libraries(component_d opentelemetry_api)
19+
20+
add_library(component_e SHARED component_e.cc)
21+
set_target_properties(component_e PROPERTIES CXX_VISIBILITY_PRESET default)
22+
target_link_libraries(component_e opentelemetry_api)
23+
24+
add_library(component_f SHARED component_f.cc)
25+
set_target_properties(component_f PROPERTIES CXX_VISIBILITY_PRESET hidden)
26+
target_link_libraries(component_f opentelemetry_api)
27+
28+
add_library(component_g SHARED component_g.cc)
29+
set_target_properties(component_g PROPERTIES CXX_VISIBILITY_PRESET default)
30+
target_link_libraries(component_g opentelemetry_api)
31+
32+
add_library(component_h SHARED component_h.cc)
33+
set_target_properties(component_h PROPERTIES CXX_VISIBILITY_PRESET hidden)
34+
target_link_libraries(component_h opentelemetry_api)
35+
36+
add_executable(singleton_test singleton_test.cc)
37+
38+
# Not linking with component_g and component_h on purpose
39+
target_link_libraries(
40+
singleton_test
41+
component_a
42+
component_b
43+
component_c
44+
component_d
45+
component_e
46+
component_f
47+
${GTEST_BOTH_LIBRARIES}
48+
${CMAKE_THREAD_LIBS_INIT}
49+
${CMAKE_DL_LIBS}
50+
opentelemetry_api)
51+
52+
gtest_add_tests(
53+
TARGET singleton_test
54+
TEST_PREFIX singleton.
55+
TEST_LIST singleton_test)

api/test/singleton/singleton_test.cc

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
#include <gtest/gtest.h>
55
#include <stdint.h>
66

7-
/*
8-
TODO:
9-
Once singleton are supported for windows,
10-
expand this test to use ::LoadLibrary, ::GetProcAddress, ::FreeLibrary
11-
*/
12-
#ifndef _WIN32
7+
#ifdef _WIN32
8+
# include <windows.h>
9+
#else
1310
# include <dlfcn.h>
1411
#endif
1512

@@ -58,28 +55,57 @@ void do_something()
5855
#ifndef BAZEL_BUILD
5956
/* Call do_something_in_g() */
6057

58+
# ifdef _WIN32
59+
HMODULE component_g = LoadLibraryA("component_g.dll");
60+
# else
6161
void *component_g = dlopen("libcomponent_g.so", RTLD_NOW);
62+
# endif
63+
6264
EXPECT_NE(component_g, nullptr);
6365

66+
# ifdef _WIN32
67+
auto *func_g = reinterpret_cast<void (*)()>(GetProcAddress(component_g, "do_something_in_g"));
68+
# else
6469
auto *func_g = reinterpret_cast<void (*)()>(dlsym(component_g, "do_something_in_g"));
70+
# endif
71+
6572
EXPECT_NE(func_g, nullptr);
6673

6774
(*func_g)();
6875

76+
# ifdef _WIN32
77+
FreeLibrary(component_g);
78+
# else
6979
dlclose(component_g);
80+
# endif
7081

7182
/* Call do_something_in_h() */
7283

84+
# ifdef _WIN32
85+
HMODULE component_h = LoadLibraryA("component_h.dll");
86+
# else
7387
void *component_h = dlopen("libcomponent_h.so", RTLD_NOW);
88+
# endif
89+
7490
EXPECT_NE(component_h, nullptr);
7591

92+
# ifdef _WIN32
93+
auto *func_h = reinterpret_cast<void (*)()>(GetProcAddress(component_h, "do_something_in_h"));
94+
# else
7695
auto *func_h = reinterpret_cast<void (*)()>(dlsym(component_h, "do_something_in_h"));
96+
# endif
97+
7798
EXPECT_NE(func_h, nullptr);
7899

79100
(*func_h)();
80101

102+
# ifdef _WIN32
103+
FreeLibrary(component_h);
104+
# else
81105
dlclose(component_h);
82-
#endif
106+
# endif
107+
108+
#endif /* BAZEL_BUILD */
83109
}
84110

85111
int span_a_lib_count = 0;
@@ -316,6 +342,14 @@ void cleanup_otel()
316342
trace_api::Provider::SetTracerProvider(provider);
317343
}
318344

345+
// TODO: Remove once windows api singletons are supported.
346+
// See https://github.com/open-telemetry/opentelemetry-cpp/issues/2534
347+
#ifdef _WIN32
348+
# define RUN_FAILING_WINDOWS_TEST 0
349+
#else
350+
# define RUN_FAILING_WINDOWS_TEST 1
351+
#endif
352+
319353
TEST(SingletonTest, Uniqueness)
320354
{
321355
do_something();
@@ -357,26 +391,31 @@ TEST(SingletonTest, Uniqueness)
357391
EXPECT_EQ(span_b_lib_count, 1);
358392
EXPECT_EQ(span_b_f1_count, 2);
359393
EXPECT_EQ(span_b_f2_count, 1);
360-
EXPECT_EQ(span_c_lib_count, 1);
361-
EXPECT_EQ(span_c_f1_count, 2);
362-
EXPECT_EQ(span_c_f2_count, 1);
363-
EXPECT_EQ(span_d_lib_count, 1);
364-
EXPECT_EQ(span_d_f1_count, 2);
365-
EXPECT_EQ(span_d_f2_count, 1);
366-
EXPECT_EQ(span_e_lib_count, 1);
367-
EXPECT_EQ(span_e_f1_count, 2);
368-
EXPECT_EQ(span_e_f2_count, 1);
369-
EXPECT_EQ(span_f_lib_count, 1);
370-
EXPECT_EQ(span_f_f1_count, 2);
371-
EXPECT_EQ(span_f_f2_count, 1);
394+
395+
#if RUN_FAILING_WINDOWS_TEST
396+
EXPECT_EQ(span_c_lib_count, 1); // Fails with shared libraries on Windows
397+
EXPECT_EQ(span_c_f1_count, 2); // Fails with shared libraries on Windows
398+
EXPECT_EQ(span_c_f2_count, 1); // Fails with shared libraries on Windows
399+
EXPECT_EQ(span_d_lib_count, 1); // Fails with shared libraries on Windows
400+
EXPECT_EQ(span_d_f1_count, 2); // Fails with shared libraries on Windows
401+
EXPECT_EQ(span_d_f2_count, 1); // Fails with shared libraries on Windows
402+
EXPECT_EQ(span_e_lib_count, 1); // Fails with shared libraries on Windows
403+
EXPECT_EQ(span_e_f1_count, 2); // Fails with shared libraries on Windows
404+
EXPECT_EQ(span_e_f2_count, 1); // Fails with shared libraries on Windows
405+
EXPECT_EQ(span_f_lib_count, 1); // Fails with shared libraries on Windows
406+
EXPECT_EQ(span_f_f1_count, 2); // Fails with shared libraries on Windows
407+
EXPECT_EQ(span_f_f2_count, 1); // Fails with shared libraries on Windows
408+
#endif
372409

373410
#ifndef BAZEL_BUILD
374-
EXPECT_EQ(span_g_lib_count, 1);
375-
EXPECT_EQ(span_g_f1_count, 2);
376-
EXPECT_EQ(span_g_f2_count, 1);
377-
EXPECT_EQ(span_h_lib_count, 1);
378-
EXPECT_EQ(span_h_f1_count, 2);
379-
EXPECT_EQ(span_h_f2_count, 1);
411+
# if RUN_FAILING_WINDOWS_TEST
412+
EXPECT_EQ(span_g_lib_count, 1); // Fails with shared libraries on Windows
413+
EXPECT_EQ(span_g_f1_count, 2); // Fails with shared libraries on Windows
414+
EXPECT_EQ(span_g_f2_count, 1); // Fails with shared libraries on Windows
415+
EXPECT_EQ(span_h_lib_count, 1); // Fails with shared libraries on Windows
416+
EXPECT_EQ(span_h_f1_count, 2); // Fails with shared libraries on Windows
417+
EXPECT_EQ(span_h_f2_count, 1); // Fails with shared libraries on Windows
418+
# endif
380419
#endif
381420

382421
EXPECT_EQ(unknown_span_count, 0);

ci/do_ci.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ $PLUGIN_DIR = Join-Path "$SRC_DIR" "plugin"
2727

2828
$VCPKG_DIR = Join-Path "$SRC_DIR" "tools" "vcpkg"
2929

30+
$Env:CTEST_OUTPUT_ON_FAILURE = "1"
31+
3032
switch ($action) {
3133
"bazel.build" {
3234
bazel $BAZEL_STARTUP_OPTIONS build $BAZEL_OPTIONS --action_env=VCPKG_DIR=$VCPKG_DIR --deleted_packages=opentracing-shim -- //...

0 commit comments

Comments
 (0)