diff options
Diffstat (limited to '')
-rw-r--r-- | test/BUILD | 27 | ||||
-rw-r--r-- | test/CMakeLists.txt | 32 | ||||
-rw-r--r-- | test/dynamic_load_test.cpp | 90 | ||||
-rw-r--r-- | test/multiple_tracer_link_test.cpp | 16 | ||||
-rw-r--r-- | test/string_view_test.cpp | 49 | ||||
-rw-r--r-- | test/tracer_a.cpp | 10 | ||||
-rw-r--r-- | test/tracer_b.cpp | 10 | ||||
-rw-r--r-- | test/tracer_test.cpp | 43 | ||||
-rw-r--r-- | test/util_test.cpp | 26 | ||||
-rw-r--r-- | test/value_test.cpp | 62 |
10 files changed, 365 insertions, 0 deletions
diff --git a/test/BUILD b/test/BUILD new file mode 100644 index 0000000..ce9077b --- /dev/null +++ b/test/BUILD @@ -0,0 +1,27 @@ +TEST_NAMES = [ + "string_view_test", + "tracer_test", + "util_test", + "value_test", +] + +[cc_test( + name = test_name, + srcs = [test_name + ".cpp"], + deps = [ + "//:opentracing", + "//3rd_party:catch2", + ], +) for test_name in TEST_NAMES] + +cc_test( + name = "mutiple_tracer_link_test", + srcs = [ + "multiple_tracer_link_test.cpp", + "tracer_a.cpp", + "tracer_b.cpp", + ], + deps = [ + "//:opentracing", + ] +) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..fa2b0ca --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,32 @@ +if (BUILD_SHARED_LIBS) + set(OPENTRACING_LIBRARY opentracing) +else() + set(OPENTRACING_LIBRARY opentracing-static) +endif() + +add_executable(tracer_test tracer_test.cpp) +target_link_libraries(tracer_test ${OPENTRACING_LIBRARY}) +add_test(NAME tracer_test COMMAND tracer_test) + +add_executable(string_view_test string_view_test.cpp) +add_test(NAME string_view_test COMMAND string_view_test) + +add_executable(value_test value_test.cpp) +add_test(NAME value_test COMMAND value_test) + +add_executable(util_test util_test.cpp) +add_test(NAME util_test COMMAND util_test) + +if (BUILD_SHARED_LIBS AND BUILD_MOCKTRACER AND BUILD_DYNAMIC_LOADING) + add_executable(dynamic_load_test dynamic_load_test.cpp) + target_link_libraries(dynamic_load_test ${OPENTRACING_LIBRARY}) + add_dependencies(dynamic_load_test opentracing_mocktracer) + add_test(NAME dynamic_load_test COMMAND dynamic_load_test + --mocktracer_library + $<TARGET_FILE:opentracing_mocktracer>) + + add_executable(multiple_tracer_link_test multiple_tracer_link_test.cpp + tracer_a.cpp tracer_b.cpp) + target_link_libraries(multiple_tracer_link_test ${OPENTRACING_LIBRARY}) + add_test(NAME multiple_tracer_link_test COMMAND multiple_tracer_link_test) +endif() diff --git a/test/dynamic_load_test.cpp b/test/dynamic_load_test.cpp new file mode 100644 index 0000000..8ad76e3 --- /dev/null +++ b/test/dynamic_load_test.cpp @@ -0,0 +1,90 @@ +#include <opentracing/dynamic_load.h> +#include <cstdio> +#include <fstream> +#include <iterator> +#include <random> +using namespace opentracing; + +#define CATCH_CONFIG_RUNNER +#include <opentracing/catch2/catch.hpp> + +static std::string mocktracer_library; + +TEST_CASE("dynamic_load") { + std::string error_message; + + SECTION( + "Dynamically loading a library that doesn't exists gives a proper error " + "code.") { + auto handle_maybe = DynamicallyLoadTracingLibrary("abc/123", error_message); + REQUIRE(!handle_maybe); + CHECK(handle_maybe.error() == dynamic_load_failure_error); + } + + error_message.clear(); + auto handle_maybe = + DynamicallyLoadTracingLibrary(mocktracer_library.c_str(), error_message); + REQUIRE(handle_maybe); + REQUIRE(error_message.empty()); + + SECTION("Creating a tracer from invalid json gives an error.") { + auto tracer_maybe = + handle_maybe->tracer_factory().MakeTracer("abc 123", error_message); + REQUIRE(!tracer_maybe); + } + + SECTION("Creating a tracer with an invalid output_file gives an error.") { + auto tracer_maybe = handle_maybe->tracer_factory().MakeTracer( + R"({"output_file": ""})", error_message); + REQUIRE(!tracer_maybe); + REQUIRE(tracer_maybe.error() == invalid_configuration_error); + } + + SECTION( + "We can create spans from an OpenTracing library dynamically loaded.") { + std::string span_filename{"spans."}; + const auto random_id = std::random_device{}(); + span_filename.append(std::to_string(random_id)); + std::string configuration = R"({ "output_file": ")"; + configuration.append(span_filename); + configuration.append(R"(" })"); + + { + auto tracer_maybe = handle_maybe->tracer_factory().MakeTracer( + configuration.c_str(), error_message); + REQUIRE(tracer_maybe); + auto tracer = *tracer_maybe; + tracer->StartSpan("abc"); + tracer->Close(); + } + + std::ifstream istream{span_filename}; + REQUIRE(istream.good()); + std::string spans_json{std::istreambuf_iterator<char>{istream}, + std::istreambuf_iterator<char>{}}; + istream.close(); + std::remove(span_filename.c_str()); + CHECK(!spans_json.empty()); + } +} + +int main(int argc, char* argv[]) { + Catch::Session session; + + using namespace Catch::clara; + auto cli = session.cli() | Opt(mocktracer_library, + "mocktracer_library")["--mocktracer_library"]; + + session.cli(cli); + int rcode = session.applyCommandLine(argc, argv); + if (rcode != 0) { + return rcode; + } + + if (mocktracer_library.empty()) { + std::cerr << "Must provide mocktracer_library!\n"; + return -1; + } + + return session.run(); +} diff --git a/test/multiple_tracer_link_test.cpp b/test/multiple_tracer_link_test.cpp new file mode 100644 index 0000000..60a9425 --- /dev/null +++ b/test/multiple_tracer_link_test.cpp @@ -0,0 +1,16 @@ +// Links in tracer_a.o and tracer_b.o to verify that there's no multiple +// definition error from OpenTracingMakeTracerFactory. +#include <opentracing/dynamic_load.h> + +extern "C" { +extern OpenTracingMakeTracerFactoryType* const OpenTracingMakeTracerFactory; +} // extern "C" + +int main() { + // Call OpenTracingMakeTracerFactory to make sure it's not elided. + if ((*OpenTracingMakeTracerFactory)(nullptr, nullptr, nullptr, nullptr, + nullptr) != -1) { + return -1; + } + return 0; +} diff --git a/test/string_view_test.cpp b/test/string_view_test.cpp new file mode 100644 index 0000000..33a8bc4 --- /dev/null +++ b/test/string_view_test.cpp @@ -0,0 +1,49 @@ +#include <opentracing/string_view.h> // test include guard +#include <opentracing/string_view.h> + +#define CATCH_CONFIG_MAIN +#include <opentracing/catch2/catch.hpp> + +using namespace opentracing; + +TEST_CASE("string_view") { + SECTION("A default-constructed string_view is empty.") { + string_view ref; + CHECK(nullptr == ref.data()); + CHECK(0 == ref.length()); + } + + SECTION("string_view can be initialized from a c-string.") { + const char* val = "hello world"; + + string_view ref(val); + + CHECK(val == ref.data()); + CHECK(std::strlen(val) == ref.length()); + } + + SECTION("string_view can be initialized from an std::string.") { + const std::string val = "hello world"; + + string_view ref(val); + + CHECK(val == ref.data()); + CHECK(val.length() == ref.length()); + } + + SECTION("A copied string_view points to the same data as its source.") { + const std::string val = "hello world"; + + string_view ref(val); + string_view cpy(ref); + + CHECK(val == cpy.data()); + CHECK(val.length() == cpy.length()); + } + + SECTION("operator[] can be used to access characters in a string_view") { + string_view s = "abc123"; + CHECK(&s[0] == s.data()); + CHECK(&s[1] == s.data() + 1); + } +} diff --git a/test/tracer_a.cpp b/test/tracer_a.cpp new file mode 100644 index 0000000..457b1dd --- /dev/null +++ b/test/tracer_a.cpp @@ -0,0 +1,10 @@ +#include <opentracing/dynamic_load.h> + +static int OpenTracingMakeTracerFactoryFct( + const char* /*opentracing_version*/, + const char* /*opentracing_abi_version*/, const void** /*error_category*/, + void* /*error_message*/, void** /*tracer_factory*/) { + return -1; +} + +OPENTRACING_DECLARE_IMPL_FACTORY(OpenTracingMakeTracerFactoryFct); diff --git a/test/tracer_b.cpp b/test/tracer_b.cpp new file mode 100644 index 0000000..457b1dd --- /dev/null +++ b/test/tracer_b.cpp @@ -0,0 +1,10 @@ +#include <opentracing/dynamic_load.h> + +static int OpenTracingMakeTracerFactoryFct( + const char* /*opentracing_version*/, + const char* /*opentracing_abi_version*/, const void** /*error_category*/, + void* /*error_message*/, void** /*tracer_factory*/) { + return -1; +} + +OPENTRACING_DECLARE_IMPL_FACTORY(OpenTracingMakeTracerFactoryFct); diff --git a/test/tracer_test.cpp b/test/tracer_test.cpp new file mode 100644 index 0000000..eca3a36 --- /dev/null +++ b/test/tracer_test.cpp @@ -0,0 +1,43 @@ +#include <opentracing/ext/tags.h> +#include <opentracing/noop.h> +#include <opentracing/tracer.h> +using namespace opentracing; + +#define CATCH_CONFIG_MAIN +#include <opentracing/catch2/catch.hpp> + +TEST_CASE("tracer") { + auto tracer = MakeNoopTracer(); + + auto span1 = tracer->StartSpan("a"); + CHECK(span1); + + SECTION("Spans provide references to the tracer that created them.") { + CHECK(&span1->tracer() == tracer.get()); + } + + SECTION("Ensure basic operations compile.") { + auto span2 = tracer->StartSpan("b", {ChildOf(&span1->context())}); + CHECK(span2); + span2->SetOperationName("b1"); + span2->SetTag("x", true); + span2->SetTag(opentracing::ext::span_kind, + opentracing::ext::span_kind_rpc_client); + CHECK(span2->BaggageItem("y").empty()); + span2->Log({{"event", "xyz"}, {"abc", 123}}); + span2->Finish(); + } + + SECTION("A reference to a null SpanContext is ignored.") { + StartSpanOptions options; + ChildOf(nullptr).Apply(options); + CHECK(options.references.size() == 0); + } +} + +TEST_CASE("A tracer can be globally registered") { + CHECK(!Tracer::IsGlobalTracerRegistered()); + auto tracer = MakeNoopTracer(); + CHECK(Tracer::InitGlobal(tracer) != nullptr); + CHECK(Tracer::IsGlobalTracerRegistered()); +} diff --git a/test/util_test.cpp b/test/util_test.cpp new file mode 100644 index 0000000..9c1e30d --- /dev/null +++ b/test/util_test.cpp @@ -0,0 +1,26 @@ +#include <opentracing/util.h> +#include <cmath> +#include <cstdlib> // Work around to https://stackoverflow.com/a/30084734. +using namespace opentracing; + +#define CATCH_CONFIG_MAIN +#include <opentracing/catch2/catch.hpp> + +TEST_CASE("convert_time_point") { + SECTION("We can convert between time points of different clocks") { + // Check that converting from a system_clock time_point to a steady_clock + // time point and then back again produces approximately the same + // system_clock time_point. + auto t1 = SystemClock::now(); + auto t2 = convert_time_point<SteadyClock>(t1); + auto t3 = convert_time_point<SystemClock>(t2); + auto difference = std::abs( + std::chrono::duration_cast<std::chrono::microseconds>(t3 - t1).count()); + CHECK(difference < 100); + } + + SECTION("Converting times from the same clock gives the identity") { + auto t = SystemClock::now(); + CHECK(t == convert_time_point<SystemClock>(t)); + } +} diff --git a/test/value_test.cpp b/test/value_test.cpp new file mode 100644 index 0000000..cd84165 --- /dev/null +++ b/test/value_test.cpp @@ -0,0 +1,62 @@ +#include <opentracing/value.h> +using namespace opentracing; + +#define CATCH_CONFIG_MAIN +#include <opentracing/catch2/catch.hpp> + +TEST_CASE("Value") { + SECTION("Signed integers get converted to int64_t.") { + Value v1(123); + CHECK(v1.is<int64_t>()); + + Value v2(static_cast<short>(123)); + CHECK(v2.is<int64_t>()); + } + + SECTION("Unsigned integers get converted to uint64_t.") { + Value v1(123u); + CHECK(v1.is<uint64_t>()); + + Value v2(static_cast<unsigned short>(123)); + CHECK(v2.is<uint64_t>()); + } + + SECTION("Bool values are deduced as bool.") { + Value v1(true); + // Workaround for "disabled expansion of recursive macro" warning. + const auto is_bool = v1.is<bool>(); + CHECK(is_bool); + } + + SECTION("Floating point numbers are converted to double.") { + Value v1(1.0); + CHECK(v1.is<double>()); + Value v2(1.0f); + CHECK(v2.is<double>()); + } + + SECTION("std::string values are deduced as std::string.") { + Value v1(std::string("abc")); + CHECK(v1.is<std::string>()); + } + + SECTION("c-string values are deduced as c-strings.") { + Value v1("abc"); + CHECK(v1.is<const char*>()); + } + + SECTION("Complex values are permitted.") { + Value v1(Values{Value(1), Value(2)}); + (void)v1; + + Value v2(Dictionary{{"abc", Value(123)}}); + (void)v2; + } + + SECTION("Value types can be compared for equality.") { + Value v1{1}, v2{2}, v3{1.0}; + CHECK(v1 == v1); + CHECK(v1 != v2); + CHECK(v1 != v3); + } +} |