bolar_2 backup
This commit is contained in:
107
host_tests/trace_context_test.cc
Normal file
107
host_tests/trace_context_test.cc
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "diagnostics/trace_context.h"
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace borla;
|
||||
|
||||
void TestFormatIdShape()
|
||||
{
|
||||
std::array<char, TraceContext::kIdCapacity> output{};
|
||||
const size_t size = TraceContext::FormatId(
|
||||
"turn", "AA-BB-CC-DD-EE-FF", 123456,
|
||||
output.data(), output.size());
|
||||
assert(std::strcmp(
|
||||
output.data(), "turn-AA-BB-CC-DD-EE-FF-123456") == 0);
|
||||
assert(size == std::strlen(output.data()));
|
||||
}
|
||||
|
||||
void TestFormatIdDefaultsAndTruncation()
|
||||
{
|
||||
std::array<char, TraceContext::kIdCapacity> output{};
|
||||
|
||||
// Missing inputs must still produce a greppable id, never an empty one.
|
||||
TraceContext::FormatId(
|
||||
nullptr, nullptr, 7, output.data(), output.size());
|
||||
assert(std::strcmp(output.data(), "dev-dev-7") == 0);
|
||||
TraceContext::FormatId("", "", 7, output.data(), output.size());
|
||||
assert(std::strcmp(output.data(), "dev-dev-7") == 0);
|
||||
|
||||
// A short buffer truncates and stays NUL-terminated, and the reported
|
||||
// length never points past the buffer.
|
||||
std::array<char, 8> tiny{};
|
||||
const size_t size = TraceContext::FormatId(
|
||||
"turn", "AA-BB-CC", 999999, tiny.data(), tiny.size());
|
||||
assert(tiny[tiny.size() - 1] == '\0');
|
||||
assert(size == std::strlen(tiny.data()));
|
||||
assert(size < tiny.size());
|
||||
|
||||
// Zero capacity must not write anything.
|
||||
assert(TraceContext::FormatId(
|
||||
"turn", "AA", 1, output.data(), 0) == 0);
|
||||
assert(TraceContext::FormatId(
|
||||
"turn", "AA", 1, nullptr, 16) == 0);
|
||||
}
|
||||
|
||||
void TestScopeLifecycle()
|
||||
{
|
||||
// Before anything is set, ids read as "-" rather than null.
|
||||
assert(TraceContext::TraceId() != nullptr);
|
||||
assert(TraceContext::SessionId() != nullptr);
|
||||
assert(TraceContext::RequestId() != nullptr);
|
||||
|
||||
TraceContext::Initialize("3C:DC:75:FB:12:FC");
|
||||
|
||||
// Colons are folded so ids stay safe inside "field=value" log grammar.
|
||||
TraceContext::RefreshTrace("ws");
|
||||
assert(std::strstr(TraceContext::TraceId(), "3C-DC-75-FB-12-FC") !=
|
||||
nullptr);
|
||||
assert(std::strchr(TraceContext::TraceId(), ':') == nullptr);
|
||||
assert(std::strncmp(TraceContext::TraceId(), "ws-", 3) == 0);
|
||||
|
||||
// The server owns the session scope; the device adopts it verbatim.
|
||||
TraceContext::SetSession("srv-session-42");
|
||||
assert(std::strcmp(
|
||||
TraceContext::SessionId(), "srv-session-42") == 0);
|
||||
TraceContext::SetSession("");
|
||||
assert(std::strcmp(TraceContext::SessionId(), "-") == 0);
|
||||
TraceContext::SetSession(nullptr);
|
||||
assert(std::strcmp(TraceContext::SessionId(), "-") == 0);
|
||||
|
||||
// ...but the device proposes one per connect, so the handshake never sends
|
||||
// the unset "-" (which the server would adopt verbatim) nor the previous
|
||||
// connection's id.
|
||||
const char* proposed = TraceContext::RefreshSession();
|
||||
assert(std::strncmp(proposed, "s-", 2) == 0);
|
||||
assert(std::strcmp(proposed, "-") != 0);
|
||||
assert(std::strcmp(proposed, TraceContext::SessionId()) == 0);
|
||||
assert(std::strstr(proposed, "3C-DC-75-FB-12-FC") != nullptr);
|
||||
// A server-supplied id still wins over the proposal.
|
||||
TraceContext::SetSession("srv-session-7");
|
||||
assert(std::strcmp(
|
||||
TraceContext::SessionId(), "srv-session-7") == 0);
|
||||
|
||||
// Each HTTP call gets a fresh request id, returned for the header.
|
||||
const char* first = TraceContext::NewRequestId();
|
||||
assert(std::strncmp(first, "req-", 4) == 0);
|
||||
assert(std::strcmp(first, TraceContext::RequestId()) == 0);
|
||||
|
||||
// A trace id pushed down from the server replaces the generated one.
|
||||
TraceContext::SetTrace("srv-trace-7");
|
||||
assert(std::strcmp(TraceContext::TraceId(), "srv-trace-7") == 0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
TestFormatIdShape();
|
||||
TestFormatIdDefaultsAndTruncation();
|
||||
TestScopeLifecycle();
|
||||
std::cout << "trace context tests passed" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user