From cbf1afcae2a666b1ede5570ba794ab98deeeb104 Mon Sep 17 00:00:00 2001 From: Karen Xu Date: Fri, 13 Nov 2020 15:56:05 -0500 Subject: [PATCH] Test Logger::log() with a processor --- sdk/test/logs/logger_sdk_test.cc | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/sdk/test/logs/logger_sdk_test.cc b/sdk/test/logs/logger_sdk_test.cc index 329bb4a935..6c36b68c0c 100644 --- a/sdk/test/logs/logger_sdk_test.cc +++ b/sdk/test/logs/logger_sdk_test.cc @@ -34,3 +34,38 @@ TEST(LoggerSDK, LogToNullProcessor) r.name = "Test log"; logger->log(r); } + +class DummyProcessor : public LogProcessor +{ + void OnReceive(std::unique_ptr &&record) noexcept {} + void ForceFlush(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} + void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept {} +}; + +TEST(LoggerSDK, LogToAProcessor) +{ + // Create an API LoggerProvider and logger + auto api_lp = std::shared_ptr(new LoggerProvider()); + auto logger = api_lp->GetLogger("logger"); + + // Cast the API LoggerProvider to an SDK Logger Provider and assert that it is still the same + // LoggerProvider by checking that getting a logger with the same name as the previously defined + // logger is the same instance + auto lp = static_cast(api_lp.get()); + auto logger2 = lp->GetLogger("logger"); + ASSERT_EQ(logger, logger2); + + // Set a processor for the LoggerProvider + std::shared_ptr processor = std::shared_ptr(new DummyProcessor()); + lp->SetProcessor(processor); + ASSERT_EQ(processor, lp->GetProcessor()); + + // Should later introduce a way to assert that + // the logger's processor is the same as "proc" + // and that the logger's processor is the same as lp's processor + + // Log a sample log record to the processor + opentelemetry::logs::LogRecord r; + r.name = "Test log"; + logger->log(r); +}