]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | #include <opentracing/dynamic_load.h> |
2 | #include <cstdio> | |
3 | #include <fstream> | |
4 | #include <iterator> | |
5 | #include <random> | |
6 | using namespace opentracing; | |
7 | ||
8 | #define CATCH_CONFIG_RUNNER | |
9 | #include <opentracing/catch2/catch.hpp> | |
10 | ||
11 | static std::string mocktracer_library; | |
12 | ||
13 | TEST_CASE("dynamic_load") { | |
14 | std::string error_message; | |
15 | ||
16 | SECTION( | |
17 | "Dynamically loading a library that doesn't exists gives a proper error " | |
18 | "code.") { | |
19 | auto handle_maybe = DynamicallyLoadTracingLibrary("abc/123", error_message); | |
20 | REQUIRE(!handle_maybe); | |
21 | CHECK(handle_maybe.error() == dynamic_load_failure_error); | |
22 | } | |
23 | ||
24 | error_message.clear(); | |
25 | auto handle_maybe = | |
26 | DynamicallyLoadTracingLibrary(mocktracer_library.c_str(), error_message); | |
27 | REQUIRE(handle_maybe); | |
28 | REQUIRE(error_message.empty()); | |
29 | ||
30 | SECTION("Creating a tracer from invalid json gives an error.") { | |
31 | auto tracer_maybe = | |
32 | handle_maybe->tracer_factory().MakeTracer("abc 123", error_message); | |
33 | REQUIRE(!tracer_maybe); | |
34 | } | |
35 | ||
36 | SECTION("Creating a tracer with an invalid output_file gives an error.") { | |
37 | auto tracer_maybe = handle_maybe->tracer_factory().MakeTracer( | |
38 | R"({"output_file": ""})", error_message); | |
39 | REQUIRE(!tracer_maybe); | |
40 | REQUIRE(tracer_maybe.error() == invalid_configuration_error); | |
41 | } | |
42 | ||
43 | SECTION( | |
44 | "We can create spans from an OpenTracing library dynamically loaded.") { | |
45 | std::string span_filename{"spans."}; | |
46 | const auto random_id = std::random_device{}(); | |
47 | span_filename.append(std::to_string(random_id)); | |
48 | std::string configuration = R"({ "output_file": ")"; | |
49 | configuration.append(span_filename); | |
50 | configuration.append(R"(" })"); | |
51 | ||
52 | { | |
53 | auto tracer_maybe = handle_maybe->tracer_factory().MakeTracer( | |
54 | configuration.c_str(), error_message); | |
55 | REQUIRE(tracer_maybe); | |
56 | auto tracer = *tracer_maybe; | |
57 | tracer->StartSpan("abc"); | |
58 | tracer->Close(); | |
59 | } | |
60 | ||
61 | std::ifstream istream{span_filename}; | |
62 | REQUIRE(istream.good()); | |
63 | std::string spans_json{std::istreambuf_iterator<char>{istream}, | |
64 | std::istreambuf_iterator<char>{}}; | |
65 | istream.close(); | |
66 | std::remove(span_filename.c_str()); | |
67 | CHECK(!spans_json.empty()); | |
68 | } | |
69 | } | |
70 | ||
71 | int main(int argc, char* argv[]) { | |
72 | Catch::Session session; | |
73 | ||
74 | using namespace Catch::clara; | |
75 | auto cli = session.cli() | Opt(mocktracer_library, | |
76 | "mocktracer_library")["--mocktracer_library"]; | |
77 | ||
78 | session.cli(cli); | |
79 | int rcode = session.applyCommandLine(argc, argv); | |
80 | if (rcode != 0) { | |
81 | return rcode; | |
82 | } | |
83 | ||
84 | if (mocktracer_library.empty()) { | |
85 | std::cerr << "Must provide mocktracer_library!\n"; | |
86 | return -1; | |
87 | } | |
88 | ||
89 | return session.run(); | |
90 | } |