diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:44:33 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:44:33 +0000 |
commit | b196c6498d22e47bb9d0da0153068ec54eac7956 (patch) | |
tree | 1a994a492581e93224a7ee6455f5d4e9d2ec8e59 /test/dynamic_load_test.cpp | |
parent | Initial commit. (diff) | |
download | opentracing-cpp-upstream.tar.xz opentracing-cpp-upstream.zip |
Adding upstream version 1.6.0.upstream/1.6.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/dynamic_load_test.cpp')
-rw-r--r-- | test/dynamic_load_test.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
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(); +} |