Copilot commented on code in PR #3735:
URL: https://github.com/apache/brpc/pull/3735#discussion_r4537279019


##########
test/brpc_naming_service_unittest.cpp:
##########
@@ -217,6 +217,41 @@ TEST(NamingServiceTest, wrong_name) {
     ASSERT_EQ(ARRAY_SIZE(address_list) - 4, servers.size());
 }
 
+TEST(NamingServiceTest, comment_after_address_is_not_tag) {
+    const char* lines[] = {
+        "# This line is ignored",
+        "10.24.234.17:8080 tag1  # a comment",
+        "10.24.234.17:8090 tag2  # another instance",
+        "10.24.234.18:8080  # a comment without tag",
+        "10.24.234.18:8080  # same instance as last line",
+        "10.24.234.19:8080 #"
+    };
+    butil::TempFile tmp_file;
+    {
+        FILE* fp = fopen(tmp_file.fname(), "w");
+        ASSERT_TRUE(fp != nullptr);
+        for (size_t i = 0; i < ARRAY_SIZE(lines); ++i) {
+            ASSERT_TRUE(fprintf(fp, "%s\n", lines[i]));
+        }
+        fclose(fp);

Review Comment:
   If an `ASSERT_*` inside this block fails (e.g., during the write loop), the 
function returns immediately and `fp` won’t be closed. Consider wrapping 
`FILE*` in an RAII helper (e.g., `std::unique_ptr<FILE, decltype(&fclose)>`) or 
using a scoped wrapper so the file is reliably closed even on early-exit 
assertions.



##########
test/brpc_naming_service_unittest.cpp:
##########
@@ -217,6 +217,41 @@ TEST(NamingServiceTest, wrong_name) {
     ASSERT_EQ(ARRAY_SIZE(address_list) - 4, servers.size());
 }
 
+TEST(NamingServiceTest, comment_after_address_is_not_tag) {
+    const char* lines[] = {
+        "# This line is ignored",
+        "10.24.234.17:8080 tag1  # a comment",
+        "10.24.234.17:8090 tag2  # another instance",
+        "10.24.234.18:8080  # a comment without tag",
+        "10.24.234.18:8080  # same instance as last line",
+        "10.24.234.19:8080 #"
+    };
+    butil::TempFile tmp_file;
+    {
+        FILE* fp = fopen(tmp_file.fname(), "w");
+        ASSERT_TRUE(fp != nullptr);
+        for (size_t i = 0; i < ARRAY_SIZE(lines); ++i) {
+            ASSERT_TRUE(fprintf(fp, "%s\n", lines[i]));
+        }
+        fclose(fp);

Review Comment:
   `fprintf` returns the number of characters written (or a negative value on 
error). Using `ASSERT_TRUE(fprintf(...))` can mis-handle valid `0` returns 
(rare here, but possible in general) and is less precise than checking for 
errors. Prefer asserting `fprintf(...) >= 0` (and optionally also check 
`fclose(fp) == 0`) to make the test’s intent and failure mode clearer.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to