Copilot commented on code in PR #14066:
URL: https://github.com/apache/trafficserver/pull/14066#discussion_r4556813567
##########
include/proxy/HostStatus.h:
##########
@@ -90,6 +90,34 @@ struct Reason {
// default is MANUAL
return MANUAL;
}
+
+ static std::string
+ getReasonName(const unsigned int reason)
+ {
Review Comment:
Defining `getReasonName()` fully in the public header makes every includer
pay the compile-time cost (and forces recompiles on any implementation tweak).
If this is not performance-critical at call sites, consider declaring it in the
header and moving the implementation to `HostStatus.cc` to reduce header bloat.
##########
include/proxy/HostStatus.h:
##########
@@ -90,6 +90,34 @@ struct Reason {
// default is MANUAL
return MANUAL;
}
+
+ static std::string
+ getReasonName(const unsigned int reason)
+ {
+ if (reason == 0) {
+ return "none";
+ }
+ if (reason == ALL) {
+ return ALL_REASON;
+ }
+
+ std::string reason_name;
+ auto append_reason = [&reason_name, reason](const unsigned int
flag, const char *name) {
+ if (reason & flag) {
+ if (!reason_name.empty()) {
+ reason_name += ",";
+ }
+ reason_name += name;
+ }
+ };
+
+ append_reason(ACTIVE, ACTIVE_REASON);
+ append_reason(LOCAL, LOCAL_REASON);
+ append_reason(MANUAL, MANUAL_REASON);
+ append_reason(SELF_DETECT, SELF_DETECT_REASON);
+
Review Comment:
`getReasonName()` returns an empty string when `reason` is non-zero but
contains no recognized bits (or only future/unknown bits). That will produce
logs like `reason:` with nothing after it, which is misleading. Consider adding
a fallback when `reason_name` is empty (e.g., return `\"unknown\"` or include
the numeric/hex value) so logs are always informative.
##########
src/proxy/unit_tests/test_HostStatus.cc:
##########
@@ -0,0 +1,38 @@
+/** @file
+
+ Unit tests for HostStatus reason formatting
+
+ @par License
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#include "proxy/HostStatus.h"
+
+#include <catch2/catch_test_macros.hpp>
+
+TEST_CASE("HostStatus reason masks have readable names", "[HostStatus]")
+{
+ REQUIRE(Reason::getReasonName(0) == "none");
+ REQUIRE(Reason::getReasonName(Reason::ACTIVE) == "active");
+ REQUIRE(Reason::getReasonName(Reason::LOCAL) == "local");
+ REQUIRE(Reason::getReasonName(Reason::MANUAL) == "manual");
+ REQUIRE(Reason::getReasonName(Reason::SELF_DETECT) == "self_detect");
+ REQUIRE(Reason::getReasonName(Reason::ALL) == "all");
+ REQUIRE(Reason::getReasonName(Reason::ACTIVE | Reason::LOCAL) ==
"active,local");
+ REQUIRE(Reason::getReasonName(Reason::getReason(Reason::MANUAL_REASON)) ==
"manual");
Review Comment:
The test covers known flags/combinations, but it doesn't cover the behavior
for non-zero unknown bits (which currently yields an empty string). Add a test
case for an unrecognized bit value (and/or a mixed known+unknown mask) to lock
in the intended output—especially if you implement a non-empty fallback for
unknown bits.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]