Commit 8e9eb5c6 authored by Dominik Charousset's avatar Dominik Charousset

Fix rendering of zero duration timespans

parent 728f6d82
......@@ -5,6 +5,14 @@ is based on [Keep a Changelog](https://keepachangelog.com).
## [Unreleased]
## Fixed
- Printing a `config_value` that contains a zero duration `timespan` now
properly prints `0s` instead of `1s` (#1262). This bug most notably showed up
when setting a `timespan` parameter such as `caf.middleman.heartbeat-interval`
via config file or CLI to `0s` and then printing the config parameter, e.g.,
via `--dump-config`.
## [0.18.3] - 2021-05-21
### Added
......
......@@ -188,7 +188,7 @@ template <class Buffer, class Rep, class Period>
void print(Buffer& buf, std::chrono::duration<Rep, Period> x) {
using namespace caf::literals;
if (x.count() == 0) {
auto str = "1s"_sv;
auto str = "0s"_sv;
buf.insert(buf.end(), str.begin(), str.end());
return;
}
......
......@@ -102,33 +102,9 @@ bool stringification_inspector::value(long double x) {
return true;
}
namespace {
template <class Duration>
auto dcast(timespan x) {
return std::chrono::duration_cast<Duration>(x);
}
} // namespace
bool stringification_inspector::value(timespan x) {
namespace sc = std::chrono;
sep();
auto try_print = [this](auto converted, const char* suffix) {
if (converted.count() < 1)
return false;
value(converted.count());
result_ += suffix;
return true;
};
if (try_print(dcast<sc::hours>(x), "h")
|| try_print(dcast<sc::minutes>(x), "min")
|| try_print(dcast<sc::seconds>(x), "s")
|| try_print(dcast<sc::milliseconds>(x), "ms")
|| try_print(dcast<sc::microseconds>(x), "us"))
return true;
value(x.count());
result_ += "ns";
detail::print(result_, x);
return true;
}
......
......@@ -335,7 +335,7 @@ SCENARIO("get_as can convert config values to timespans") {
using namespace std::chrono_literals;
GIVEN("a config value with value 42s") {
auto x = config_value{timespan{42s}};
WHEN("using get_as with timespan") {
WHEN("using get_as with timespan or string") {
THEN("conversion succeeds") {
CHECK_EQ(get_as<timespan>(x), timespan{42s});
CHECK_EQ(get_as<std::string>(x), "42s");
......@@ -351,6 +351,15 @@ SCENARIO("get_as can convert config values to timespans") {
}
}
}
GIVEN("a config value with value 0s") {
auto x = config_value{timespan{0s}};
WHEN("using get_as with timespan or string") {
THEN("conversion succeeds") {
CHECK_EQ(get_as<timespan>(x), timespan{0});
CHECK_EQ(get_as<std::string>(x), "0s");
}
}
}
}
SCENARIO("get_as can convert config values to strings") {
......
......@@ -13,6 +13,8 @@ using namespace caf;
#define CHECK_DEEP_TO_STRING(val, str) CAF_CHECK_EQUAL(deep_to_string(val), str)
CAF_TEST(timespans use the highest unit available when printing) {
CHECK_EQ(to_string(config_value{timespan{0}}), "0s");
CHECK_DEEP_TO_STRING(timespan{0}, "0s");
CHECK_DEEP_TO_STRING(timespan{1}, "1ns");
CHECK_DEEP_TO_STRING(timespan{1'000}, "1us");
CHECK_DEEP_TO_STRING(timespan{1'000'000}, "1ms");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment