From d0ceb35e7ecfd3d7f62b0c2c03e114d3ece5d64a Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 27 Mar 2024 13:14:23 +0800 Subject: [PATCH 1/3] test/boost: print runtime_error using e.what() before this change, we rely on the default-generated fmt::formatter created from operator<<, but fmt v10 dropped the default-generated formatter. but fortunately, fmt v10 brings the builtin formatter for classes derived from `std::exception`. but before switching to {fmt} v10, and after dropping `FMT_DEPRECATED_OSTREAM` macro, we need to print out `std::runtime_error`. so far, we don't have a shared place for formatter for `std::runtime_error`. so we are addressing the needs on a case-by-case basis. in this change, we just print it using `e.what()`. it's behavior is identical to what we have now. Refs #13245 Signed-off-by: Kefu Chai --- test/boost/database_test.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/boost/database_test.cc b/test/boost/database_test.cc index 55f6aab9c2..909c59b12f 100644 --- a/test/boost/database_test.cc +++ b/test/boost/database_test.cc @@ -972,9 +972,9 @@ SEASTAR_THREAD_TEST_CASE(read_max_size) { } } catch (std::runtime_error& e) { if (should_throw) { - testlog.trace("Exception thrown, as expected: {}", e); + testlog.trace("Exception thrown, as expected: {}", e.what()); } else { - BOOST_FAIL(fmt::format("Expected no exception, but caught: {}", e)); + BOOST_FAIL(fmt::format("Expected no exception, but caught: {}", e.what())); } } } @@ -1044,7 +1044,7 @@ SEASTAR_THREAD_TEST_CASE(unpaged_mutation_read_global_limit) { BOOST_REQUIRE(size != 0); BOOST_FAIL("Expected exception, but none was thrown."); } catch (std::runtime_error& e) { - testlog.trace("Exception thrown, as expected: {}", e); + testlog.trace("Exception thrown, as expected: {}", e.what()); } } }, std::move(cfg)).get(); From 4f8c1a4729b9b9282abc11752a7045d9969a59e9 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 27 Mar 2024 13:16:51 +0800 Subject: [PATCH 2/3] test/lib: do not print with fmt::to_string() we should not format a variable unless we want to print it. in this case, we format `first_row` using `fmt::to_string()` to a string, and then insert the string to another string, despite that this is in a cold path, this is still a anti pattern -- both convoluted, and not performant. so let's just pass `first_row` to `format()`. Refs #13245 Signed-off-by: Kefu Chai --- test/lib/cql_assertions.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/cql_assertions.cc b/test/lib/cql_assertions.cc index cebfddf743..f52eacf781 100644 --- a/test/lib/cql_assertions.cc +++ b/test/lib/cql_assertions.cc @@ -38,7 +38,7 @@ rows_assertions::is_empty() { auto row_count = rs.size(); if (row_count != 0) { auto&& first_row = *rs.rows().begin(); - fail(format("Expected no rows, but got {:d}. First row: {}", row_count, fmt::to_string(first_row))); + fail(format("Expected no rows, but got {:d}. First row: {}", row_count, first_row)); } return {*this}; } From 71a519dee806100a8acfd51eec67cf1fc823c425 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 27 Mar 2024 13:20:43 +0800 Subject: [PATCH 3/3] test: unit: add fmt::formatter for test_data in tests this change is created in same spirit of d1c35f943d. before this change, we rely on the default-generated fmt::formatter created from operator<<, but fmt v10 dropped the default-generated formatter. in this change, we define formatters for test_data in radix_tree_stress_test.cc, and drop its operator<<. Refs #13245 Signed-off-by: Kefu Chai --- test/unit/radix_tree_stress_test.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/unit/radix_tree_stress_test.cc b/test/unit/radix_tree_stress_test.cc index d5a3f476d7..5279f4aae5 100644 --- a/test/unit/radix_tree_stress_test.cc +++ b/test/unit/radix_tree_stress_test.cc @@ -41,10 +41,11 @@ public: } }; -std::ostream& operator<<(std::ostream& out, const test_data& d) { - out << d.value(); - return out; -} +template <> struct fmt::formatter : fmt::formatter { + auto format(const test_data& d, fmt::format_context& ctx) const { + return fmt::format_to(ctx.out(), "{}", d.value()); + } +}; using test_tree = tree;