Skip to content

Commit

Permalink
tests/bounded_property_test: test for optional<T> user friendly
Browse files Browse the repository at this point in the history
set_value

the test fails with a bad any cast exception, when passing a value to
set_property that do not match T (like a ms to a optional<ms>)

(cherry picked from commit 89fd98a)
  • Loading branch information
andijcr authored and Michal Maslanka committed Feb 19, 2024
1 parent b4a4314 commit 69cde13
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/v/config/tests/bounded_property_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@

#include <seastar/testing/thread_test_case.hh>

#include <chrono>
#include <optional>

static_assert(config::detail::bounds<config::numeric_integral_bounds<int>>);
static_assert(config::detail::bounds<config::numeric_bounds<double>>);

namespace {

using namespace std::literals;

struct test_config : public config::config_store {
config::bounded_property<int32_t> bounded_int;
config::bounded_property<std::optional<int32_t>> bounded_int_opt;
config::bounded_property<int16_t> odd_constraint;
config::bounded_property<double, config::numeric_bounds> bounded_double;
config::bounded_property<std::optional<double>, config::numeric_bounds>
bounded_double_opt;
config::bounded_property<std::optional<std::chrono::milliseconds>>
bounded_opt_ms;

test_config()
: bounded_int(
Expand Down Expand Up @@ -62,7 +67,14 @@ struct test_config : public config::config_store {
"An options float with some bounds set",
{},
std::nullopt,
{.min = -1, .max = 2.236067977}) {}
{.min = -1, .max = 2.236067977})
, bounded_opt_ms(
*this,
"bounded_opt_ms",
"An optional duration",
{},
std::nullopt,
{.min = 5ms}) {}
};

SEASTAR_THREAD_TEST_CASE(numeric_integral_bounds) {
Expand Down Expand Up @@ -165,4 +177,18 @@ SEASTAR_THREAD_TEST_CASE(numeric_fp_bounds) {
BOOST_TEST(cfg.bounded_double() == 2.236067977);
}

SEASTAR_THREAD_TEST_CASE(bounded_property_set_value) {
auto cfg = test_config();

BOOST_TEST_INFO("fully specified optional");
cfg.bounded_opt_ms.set_value(std::optional{20ms});
BOOST_CHECK_EQUAL(cfg.bounded_opt_ms(), 20ms);
BOOST_TEST_INFO("std::nullopt");
cfg.bounded_opt_ms.set_value(std::nullopt);
BOOST_CHECK(!cfg.bounded_opt_ms());
BOOST_TEST_INFO("a value convertible to the optional::value type");
cfg.bounded_opt_ms.set_value(2h);
BOOST_CHECK_EQUAL(cfg.bounded_opt_ms(), 2h);
}

} // namespace

0 comments on commit 69cde13

Please sign in to comment.