Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 3567aed

Browse files
authored
WithSpan cleanups. (#210)
* Add WithSpanDeathTest. * Remove unused ContextTestPeer.
1 parent 791e19b commit 3567aed

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

opencensus/trace/internal/with_span_benchmark.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,9 @@
2121
#include "opencensus/trace/span.h"
2222

2323
namespace opencensus {
24-
namespace context {
25-
class ContextTestPeer {
26-
public:
27-
static const opencensus::trace::Span& CurrentSpan() {
28-
return Context::InternalMutableCurrent()->span_;
29-
}
30-
};
31-
} // namespace context
3224
namespace trace {
3325
namespace {
3426

35-
using opencensus::context::ContextTestPeer;
36-
3727
void BM_WithSpanUnsampled(benchmark::State& state) {
3828
static ::opencensus::trace::NeverSampler sampler;
3929
auto span = Span::StartSpan("MySpan", /*parent=*/nullptr, {&sampler});

opencensus/trace/internal/with_span_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "opencensus/trace/with_span.h"
1616

1717
#include <iostream>
18+
#include <thread>
1819

1920
#include "gtest/gtest.h"
2021
#include "opencensus/context/context.h"
@@ -106,4 +107,21 @@ TEST(WithSpanTest, DisabledViaConditional) {
106107
span1.End();
107108
}
108109

110+
#ifndef NDEBUG
111+
TEST(WithSpanDeathTest, DestructorOnWrongThread) {
112+
auto span = opencensus::trace::Span::StartSpan("MySpan");
113+
EXPECT_DEATH_IF_SUPPORTED(
114+
{
115+
auto* ws = new opencensus::trace::WithSpan(span);
116+
std::thread t([ws]() {
117+
// Running the destructor in a different thread corrupts its
118+
// thread-local Context. In debug mode, it assert()s.
119+
delete ws;
120+
});
121+
t.join();
122+
},
123+
"must be destructed on the same thread");
124+
}
125+
#endif
126+
109127
} // namespace

0 commit comments

Comments
 (0)