Copilot commented on code in PR #13579:
URL: https://github.com/apache/trafficserver/pull/13579#discussion_r4359803095


##########
src/proxy/http/unit_tests/test_HttpSessionManager.cc:
##########
@@ -0,0 +1,356 @@
+/** @file
+
+  Unit tests for ServerSessionPool::acquireSession.
+
+  @section license 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/http/HttpSessionManager.h"
+#include "proxy/http/HttpConfig.h"
+
+#include <proxy/PoolableSession.h>
+#include <iocore/net/SessionSharingAPIEnums.h>
+#include <tscore/CryptoHash.h>
+#include <tscore/ink_assert.h>
+#include <tscore/ink_inet.h>
+#include <tsutil/Metrics.h>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cstdint>
+#include <cstring>
+#include <memory>
+#include <vector>
+
+namespace
+{
+
+/** A minimal PoolableSession that can be pooled and matched.
+ *
+ * Only the remote address and the hostname hash participate in the match
+ * paths exercised here, so no NetVConnection is required. The session must
+ * not be multiplexing, otherwise acquireSession consults the HttpSM.
+ */
+class TestPoolableSession : public PoolableSession
+{
+public:
+  TestPoolableSession(char const *addr_str, char const *hostname)
+  {
+    ink_release_assert(ats_ip_pton(addr_str, &_remote_addr) == 0);
+    this->attach_hostname(hostname);
+  }

Review Comment:
   The tests rely on `TestPoolableSession` being non-multiplexing, but this is 
only stated in a comment and not enforced in the test double. If 
`PoolableSession`’s default multiplexing capability changes (or becomes 
dependent on other state), these tests may start implicitly requiring an 
`HttpSM` and become brittle. Make the “not multiplexing” behavior explicit via 
the relevant `PoolableSession` API/override/flag used by `acquireSession` so 
the test’s precondition is guaranteed by construction.



##########
src/proxy/http/unit_tests/test_HttpSessionManager.cc:
##########
@@ -0,0 +1,356 @@
+/** @file
+
+  Unit tests for ServerSessionPool::acquireSession.
+
+  @section license 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/http/HttpSessionManager.h"
+#include "proxy/http/HttpConfig.h"
+
+#include <proxy/PoolableSession.h>
+#include <iocore/net/SessionSharingAPIEnums.h>
+#include <tscore/CryptoHash.h>
+#include <tscore/ink_assert.h>
+#include <tscore/ink_inet.h>
+#include <tsutil/Metrics.h>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cstdint>
+#include <cstring>
+#include <memory>
+#include <vector>
+
+namespace
+{
+
+/** A minimal PoolableSession that can be pooled and matched.
+ *
+ * Only the remote address and the hostname hash participate in the match
+ * paths exercised here, so no NetVConnection is required. The session must
+ * not be multiplexing, otherwise acquireSession consults the HttpSM.
+ */
+class TestPoolableSession : public PoolableSession
+{
+public:
+  TestPoolableSession(char const *addr_str, char const *hostname)
+  {
+    ink_release_assert(ats_ip_pton(addr_str, &_remote_addr) == 0);
+    this->attach_hostname(hostname);
+  }
+
+  void
+  new_connection(NetVConnection *, MIOBuffer *, IOBufferReader *) override
+  {
+  }
+  void
+  start() override
+  {
+  }
+  void
+  release(ProxyTransaction *) override
+  {
+  }
+  void
+  destroy() override
+  {
+  }
+  void
+  free() override
+  {
+  }
+  void
+  increment_current_active_connections_stat() override
+  {
+  }
+  void
+  decrement_current_active_connections_stat() override
+  {
+  }
+
+  int
+  get_transact_count() const override
+  {
+    return 0;
+  }
+
+  const char *
+  get_protocol_string() const override
+  {
+    return "test";
+  }
+
+  IOBufferReader *
+  get_remote_reader() override
+  {
+    return nullptr;
+  }
+
+  void
+  do_io_close(int /* lerrno ATS_UNUSED */ = -1) override
+  {
+    ++close_count;
+  }
+
+  sockaddr const *
+  get_remote_addr() const override
+  {
+    return &_remote_addr.sa;
+  }
+
+  int close_count = 0;
+
+private:
+  IpEndpoint _remote_addr;
+};
+
+/// Owns the test sessions and hands raw pointers to the pool under test.
+class SessionFactory
+{
+public:
+  TestPoolableSession *
+  make(char const *addr_str, char const *hostname)
+  {
+    return 
_sessions.emplace_back(std::make_unique<TestPoolableSession>(addr_str, 
hostname)).get();
+  }
+
+private:
+  std::vector<std::unique_ptr<TestPoolableSession>> _sessions;
+};
+
+/// The pool bookkeeping updates this gauge, which is normally initialized via 
HttpConfig.
+/// Create it here (when needed) and restore its prior value on every exit 
path so tests don't leak state.
+class PooledConnectionsGaugeGuard
+{
+public:
+  PooledConnectionsGaugeGuard()
+  {
+    if (http_rsb.pooled_server_connections == nullptr) {
+      http_rsb.pooled_server_connections = 
Metrics::Gauge::createPtr("proxy.process.http.pooled_server_connections");
+    }
+    _saved = Metrics::Gauge::load(http_rsb.pooled_server_connections);
+  }
+
+  ~PooledConnectionsGaugeGuard() { 
Metrics::Gauge::store(http_rsb.pooled_server_connections, _saved); }

Review Comment:
   This guard mutates process-global state 
(`http_rsb.pooled_server_connections` pointer and its gauge value) without 
synchronization. If the unit test runner executes tests concurrently (e.g., 
Catch2 parallel runs or other in-process concurrency), this can introduce data 
races/flakiness. Consider protecting initialization/value restore with a 
process-wide lock and/or `std::call_once` for gauge creation, or moving metric 
initialization to a single-threaded test-suite setup path (so individual test 
cases don’t race on global metric state).



##########
src/proxy/http/unit_tests/test_HttpSessionManager.cc:
##########
@@ -0,0 +1,356 @@
+/** @file
+
+  Unit tests for ServerSessionPool::acquireSession.
+
+  @section license 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/http/HttpSessionManager.h"
+#include "proxy/http/HttpConfig.h"
+
+#include <proxy/PoolableSession.h>
+#include <iocore/net/SessionSharingAPIEnums.h>
+#include <tscore/CryptoHash.h>
+#include <tscore/ink_assert.h>
+#include <tscore/ink_inet.h>
+#include <tsutil/Metrics.h>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cstdint>
+#include <cstring>
+#include <memory>
+#include <vector>
+
+namespace
+{
+
+/** A minimal PoolableSession that can be pooled and matched.
+ *
+ * Only the remote address and the hostname hash participate in the match
+ * paths exercised here, so no NetVConnection is required. The session must
+ * not be multiplexing, otherwise acquireSession consults the HttpSM.
+ */
+class TestPoolableSession : public PoolableSession
+{
+public:
+  TestPoolableSession(char const *addr_str, char const *hostname)
+  {
+    ink_release_assert(ats_ip_pton(addr_str, &_remote_addr) == 0);
+    this->attach_hostname(hostname);
+  }
+
+  void
+  new_connection(NetVConnection *, MIOBuffer *, IOBufferReader *) override
+  {
+  }
+  void
+  start() override
+  {
+  }
+  void
+  release(ProxyTransaction *) override
+  {
+  }
+  void
+  destroy() override
+  {
+  }
+  void
+  free() override
+  {
+  }
+  void
+  increment_current_active_connections_stat() override
+  {
+  }
+  void
+  decrement_current_active_connections_stat() override
+  {
+  }
+
+  int
+  get_transact_count() const override
+  {
+    return 0;
+  }
+
+  const char *
+  get_protocol_string() const override
+  {
+    return "test";
+  }
+
+  IOBufferReader *
+  get_remote_reader() override
+  {
+    return nullptr;
+  }
+
+  void
+  do_io_close(int /* lerrno ATS_UNUSED */ = -1) override
+  {
+    ++close_count;
+  }
+
+  sockaddr const *
+  get_remote_addr() const override
+  {
+    return &_remote_addr.sa;
+  }
+
+  int close_count = 0;
+
+private:
+  IpEndpoint _remote_addr;
+};
+
+/// Owns the test sessions and hands raw pointers to the pool under test.
+class SessionFactory
+{
+public:
+  TestPoolableSession *
+  make(char const *addr_str, char const *hostname)
+  {
+    return 
_sessions.emplace_back(std::make_unique<TestPoolableSession>(addr_str, 
hostname)).get();
+  }
+
+private:
+  std::vector<std::unique_ptr<TestPoolableSession>> _sessions;
+};
+
+/// The pool bookkeeping updates this gauge, which is normally initialized via 
HttpConfig.
+/// Create it here (when needed) and restore its prior value on every exit 
path so tests don't leak state.
+class PooledConnectionsGaugeGuard
+{
+public:
+  PooledConnectionsGaugeGuard()
+  {
+    if (http_rsb.pooled_server_connections == nullptr) {
+      http_rsb.pooled_server_connections = 
Metrics::Gauge::createPtr("proxy.process.http.pooled_server_connections");
+    }
+    _saved = Metrics::Gauge::load(http_rsb.pooled_server_connections);
+  }
+
+  ~PooledConnectionsGaugeGuard() { 
Metrics::Gauge::store(http_rsb.pooled_server_connections, _saved); }

Review Comment:
   The comment claims this guard prevents state leaks, but if 
`http_rsb.pooled_server_connections` starts as `nullptr`, the guard permanently 
initializes it (global pointer state changes even after restoring the gauge’s 
value). Either (a) adjust the comment to clarify that only the *gauge value* is 
restored (not the pointer/global initialization), or (b) track whether the 
guard created the gauge and restore the prior pointer state accordingly (if 
that’s safe/desired within the project’s metric registry constraints).



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

Reply via email to