|
14 | 14 |
|
15 | 15 | #include "opencensus/exporters/trace/stdout/stdout_exporter.h" |
16 | 16 |
|
| 17 | +#include <iostream> |
| 18 | +#include <sstream> |
| 19 | + |
17 | 20 | #include "absl/time/clock.h" |
| 21 | +#include "gmock/gmock.h" |
18 | 22 | #include "gtest/gtest.h" |
| 23 | +#include "opencensus/trace/exporter/span_exporter.h" |
19 | 24 | #include "opencensus/trace/internal/local_span_store.h" |
20 | 25 | #include "opencensus/trace/span.h" |
21 | 26 |
|
22 | 27 | namespace opencensus { |
23 | | -namespace exporters { |
24 | 28 | namespace trace { |
| 29 | +namespace exporter { |
| 30 | + |
| 31 | +class SpanExporterTestPeer { |
| 32 | + public: |
| 33 | + static constexpr auto& ExportForTesting = SpanExporter::ExportForTesting; |
| 34 | +}; |
| 35 | + |
| 36 | +} // namespace exporter |
| 37 | +} // namespace trace |
| 38 | +} // namespace opencensus |
| 39 | + |
25 | 40 | namespace { |
26 | 41 |
|
27 | | -TEST(TraceExporterTest, ExportTrace) { |
28 | | - StdoutExporter::Register(); |
| 42 | +using ::testing::HasSubstr; |
| 43 | + |
| 44 | +TEST(StdoutExporterTest, Export) { |
| 45 | + std::stringstream s; |
| 46 | + ::opencensus::exporters::trace::StdoutExporter::Register(&s); |
29 | 47 | static ::opencensus::trace::AlwaysSampler sampler; |
30 | 48 | ::opencensus::trace::StartSpanOptions opts = {&sampler}; |
31 | 49 |
|
32 | 50 | auto span1 = ::opencensus::trace::Span::StartSpan("Span1", nullptr, opts); |
33 | | - absl::SleepFor(absl::Milliseconds(100)); |
34 | 51 | auto span2 = ::opencensus::trace::Span::StartSpan("Span2", &span1, opts); |
35 | | - absl::SleepFor(absl::Milliseconds(200)); |
36 | 52 | auto span3 = ::opencensus::trace::Span::StartSpan("Span3", &span2, opts); |
37 | | - absl::SleepFor(absl::Milliseconds(300)); |
| 53 | + span3.AddAnnotation("Needle."); |
38 | 54 | span3.End(); |
39 | 55 | span2.End(); |
40 | 56 | span1.End(); |
41 | 57 |
|
42 | | - // Wait for exporter. |
43 | | - absl::SleepFor(absl::Milliseconds(5200)); |
| 58 | + opencensus::trace::exporter::SpanExporterTestPeer::ExportForTesting(); |
| 59 | + const std::string str = s.str(); |
| 60 | + std::cout << str; |
| 61 | + EXPECT_THAT(str, HasSubstr("Needle.")); |
44 | 62 | } |
45 | 63 |
|
46 | 64 | } // namespace |
47 | | -} // namespace trace |
48 | | -} // namespace exporters |
49 | | -} // namespace opencensus |
|
0 commit comments