blob: 508e2830875e48a7b1f9faa8c6ee9d895d2bf78b [file] [edit]
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "tracing/thread_context.h"
#include <thread> // NOLINT(build/c++11)
#include "gtest/gtest.h"
#include "tracing/noop_tracing.h"
namespace credentio_tracing {
namespace {
TEST(ThreadContextTest, InitialStateIsNull) {
EXPECT_EQ(ThreadContext::GetCurrentSpan(), nullptr);
}
TEST(ThreadContextTest, SetAndGetCurrentSpan) {
NoopSpan span1;
NoopSpan span2;
EXPECT_EQ(ThreadContext::GetCurrentSpan(), nullptr);
ThreadContext::SetCurrentSpan(&span1);
EXPECT_EQ(ThreadContext::GetCurrentSpan(), &span1);
ThreadContext::SetCurrentSpan(&span2);
EXPECT_EQ(ThreadContext::GetCurrentSpan(), &span2);
ThreadContext::SetCurrentSpan(nullptr);
EXPECT_EQ(ThreadContext::GetCurrentSpan(), nullptr);
}
TEST(ThreadContextTest, ThreadLocalIsolation) {
NoopSpan main_span;
ThreadContext::SetCurrentSpan(&main_span);
std::thread worker([&]() {
EXPECT_EQ(ThreadContext::GetCurrentSpan(), nullptr);
NoopSpan worker_span;
ThreadContext::SetCurrentSpan(&worker_span);
EXPECT_EQ(ThreadContext::GetCurrentSpan(), &worker_span);
});
worker.join();
EXPECT_EQ(ThreadContext::GetCurrentSpan(), &main_span);
ThreadContext::SetCurrentSpan(nullptr);
}
TEST(ThreadContextTest, SetNullWhenAlreadyNull) {
EXPECT_EQ(ThreadContext::GetCurrentSpan(), nullptr);
ThreadContext::SetCurrentSpan(nullptr);
EXPECT_EQ(ThreadContext::GetCurrentSpan(), nullptr);
}
} // namespace
} // namespace credentio_tracing