Skip to content

Commit

Permalink
fix clear and new test
Browse files Browse the repository at this point in the history
Signed-off-by: dentiny <[email protected]>
  • Loading branch information
dentiny committed Feb 10, 2025
1 parent 0d5ad28 commit 9d2ebc3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ray/util/spdlog_newliner_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class spdlog_newliner_sink final : public spdlog::sinks::base_sink<Mutex> {
if (!buffer_.empty()) {
spdlog::details::log_msg new_log_msg;
new_log_msg.payload = std::string_view{buffer_.data(), buffer_.length()};
buffer_.clear();
internal_sink_->log(std::move(new_log_msg));
buffer_.clear();
}
internal_sink_->flush();
}
Expand Down
21 changes: 21 additions & 0 deletions src/ray/util/tests/spdlog_newliner_sink_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ std::shared_ptr<spdlog::logger> CreateLogger() {
return logger;
}

// Testing senario: Keep writing to spdlog after flush, and check whether all written
// content is correctly reflected.
TEST(NewlinerSinkTest, WriteAfterFlush) {
auto logger = CreateLogger();
constexpr std::string_view kContent = "hello";

// First time write and flush.
testing::internal::CaptureStdout();
logger->log(spdlog::level::info, kContent);
logger->flush();
std::string stdout_content = testing::internal::GetCapturedStdout();
EXPECT_EQ(stdout_content, kContent);

// Write after flush.
testing::internal::CaptureStdout();
logger->log(spdlog::level::info, kContent);
logger->flush();
stdout_content = testing::internal::GetCapturedStdout();
EXPECT_EQ(stdout_content, kContent);
}

TEST(NewlinerSinkTest, AppendAndFlushTest) {
// Case-1: string with newliner at the end.
{
Expand Down

0 comments on commit 9d2ebc3

Please sign in to comment.