This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new e459f88d Fix port conflicts in parallel unit tests (#3568)
e459f88d is described below

commit e459f88dfdffb10887a3e437fe54fadb5daaf271
Author: Bright Chen <[email protected]>
AuthorDate: Fri Sep 25 20:56:16 2026 +0800

    Fix port conflicts in parallel unit tests (#3568)
    
    * Fix port conflicts in parallel unit tests
    
    * Isolate naming service test state
---
 src/brpc/policy/discovery_naming_service.cpp |  6 ++
 src/brpc/policy/discovery_naming_service.h   |  2 +
 test/brpc_alpn_protocol_unittest.cpp         |  5 +-
 test/brpc_builtin_service_unittest.cpp       | 14 ++---
 test/brpc_checksum_unittest.cpp              |  6 +-
 test/brpc_coroutine_unittest.cpp             |  7 +--
 test/brpc_grpc_protocol_unittest.cpp         |  7 +--
 test/brpc_http_message_unittest.cpp          |  2 +-
 test/brpc_naming_service_unittest.cpp        | 86 ++++++++++++++++++++--------
 test/brpc_rtmp_unittest.cpp                  | 31 +++++-----
 10 files changed, 102 insertions(+), 64 deletions(-)

diff --git a/src/brpc/policy/discovery_naming_service.cpp 
b/src/brpc/policy/discovery_naming_service.cpp
index 2ff163ea..98751130 100644
--- a/src/brpc/policy/discovery_naming_service.cpp
+++ b/src/brpc/policy/discovery_naming_service.cpp
@@ -134,6 +134,12 @@ inline Channel* GetOrNewDiscoveryChannel() {
     return s_discovery_channel;
 }
 
+void ResetDiscoveryChannelForTesting() {
+    delete s_discovery_channel;
+    s_discovery_channel = nullptr;
+    s_init_discovery_channel_once = PTHREAD_ONCE_INIT;
+}
+
 bool DiscoveryRegisterParam::IsValid() const {
     return !appid.empty() && !hostname.empty() && !addrs.empty() &&
             !env.empty() && !zone.empty() && !version.empty();
diff --git a/src/brpc/policy/discovery_naming_service.h 
b/src/brpc/policy/discovery_naming_service.h
index 8a092a8a..3526a2d9 100644
--- a/src/brpc/policy/discovery_naming_service.h
+++ b/src/brpc/policy/discovery_naming_service.h
@@ -82,6 +82,8 @@ private:
     DiscoveryClient _client;
 };
 
+// Reset the process-wide discovery channel. This function is intended for 
tests.
+void ResetDiscoveryChannelForTesting();
 
 } // namespace policy
 } // namespace brpc
diff --git a/test/brpc_alpn_protocol_unittest.cpp 
b/test/brpc_alpn_protocol_unittest.cpp
index 3e0fd138..a7c79222 100644
--- a/test/brpc_alpn_protocol_unittest.cpp
+++ b/test/brpc_alpn_protocol_unittest.cpp
@@ -28,7 +28,7 @@
 #include "butil/fd_guard.h"
 #include "echo.pb.h"
 
-DEFINE_string(listen_addr, "0.0.0.0:8011", "Server listen address.");
+DEFINE_string(listen_addr, "0.0.0.0:0", "Server listen address.");
 
 int main(int argc, char* argv[]) {
     testing::InitGoogleTest(&argc, argv);
@@ -89,8 +89,7 @@ public:
                 reinterpret_cast<const unsigned char*>(raw_alpn.data()), 
raw_alpn.size());
     
         // TCP connect.
-        butil::EndPoint endpoint;
-        butil::str2endpoint(FLAGS_listen_addr.data(), &endpoint);
+        const butil::EndPoint endpoint = _server.listen_address();
 
         int cli_fd = butil::tcp_connect(endpoint, nullptr);
         butil::fd_guard guard(cli_fd);
diff --git a/test/brpc_builtin_service_unittest.cpp 
b/test/brpc_builtin_service_unittest.cpp
index 281c60df..2389b99b 100644
--- a/test/brpc_builtin_service_unittest.cpp
+++ b/test/brpc_builtin_service_unittest.cpp
@@ -562,13 +562,13 @@ TEST_F(BuiltinServiceTest, customized_health) {
     brpc::ServerOptions opt;
     MyHealthReporter hr;
     opt.health_reporter = &hr;
-    ASSERT_EQ(0, _server.Start(9798, &opt));
+    ASSERT_EQ(0, _server.Start(0, &opt));
     brpc::HealthRequest req;
     brpc::HealthResponse res;
     brpc::ChannelOptions copt;
     copt.protocol = brpc::PROTOCOL_HTTP;
     brpc::Channel chan;
-    ASSERT_EQ(0, chan.Init("127.0.0.1:9798", &copt));
+    ASSERT_EQ(0, chan.Init(_server.listen_address(), &copt));
     brpc::Controller cntl;
     cntl.http_request().uri() = "/health";
     chan.CallMethod(nullptr, &cntl, &req, &res, nullptr);
@@ -597,7 +597,7 @@ public:
 
 TEST_F(BuiltinServiceTest, normal_grpc_health) {
     brpc::ServerOptions opt;
-    ASSERT_EQ(0, _server.Start(9798, &opt));
+    ASSERT_EQ(0, _server.Start(0, &opt));
 
     grpc::health::v1::HealthCheckResponse response;
     grpc::health::v1::HealthCheckRequest request;
@@ -606,7 +606,7 @@ TEST_F(BuiltinServiceTest, normal_grpc_health) {
     brpc::ChannelOptions copt;
     copt.protocol = "h2:grpc";
     brpc::Channel chan;
-    ASSERT_EQ(0, chan.Init("127.0.0.1:9798", &copt));
+    ASSERT_EQ(0, chan.Init(_server.listen_address(), &copt));
     grpc::health::v1::Health_Stub stub(&chan);
     stub.Check(&cntl, &request, &response, nullptr);
     EXPECT_FALSE(cntl.Failed()) << cntl.ErrorText();
@@ -624,7 +624,7 @@ TEST_F(BuiltinServiceTest, customized_grpc_health) {
     brpc::ServerOptions opt;
     MyGrpcHealthReporter hr;
     opt.health_reporter = &hr;
-    ASSERT_EQ(0, _server.Start(9798, &opt));
+    ASSERT_EQ(0, _server.Start(0, &opt));
 
     grpc::health::v1::HealthCheckResponse response;
     grpc::health::v1::HealthCheckRequest request;
@@ -634,7 +634,7 @@ TEST_F(BuiltinServiceTest, customized_grpc_health) {
     brpc::ChannelOptions copt;
     copt.protocol = "h2:grpc";
     brpc::Channel chan;
-    ASSERT_EQ(0, chan.Init("127.0.0.1:9798", &copt));
+    ASSERT_EQ(0, chan.Init(_server.listen_address(), &copt));
 
     grpc::health::v1::Health_Stub stub(&chan);
     stub.Check(&cntl, &request, &response, nullptr);
@@ -760,7 +760,7 @@ TEST_F(BuiltinServiceTest, bad_method) {
 
 TEST_F(BuiltinServiceTest, vars) {
     // Start server to show bvars inside 
-    ASSERT_EQ(0, _server.Start("127.0.0.1:9798", nullptr));
+    ASSERT_EQ(0, _server.Start(0, nullptr));
     brpc::VarsService service;
     brpc::VarsRequest req;
     brpc::VarsResponse res;
diff --git a/test/brpc_checksum_unittest.cpp b/test/brpc_checksum_unittest.cpp
index 7d5a2665..7811cd6c 100644
--- a/test/brpc_checksum_unittest.cpp
+++ b/test/brpc_checksum_unittest.cpp
@@ -152,10 +152,9 @@ class ChecksumAttachmentEndToEndTest : public 
::testing::Test {
 protected:
     void SetUp() override {
         ASSERT_EQ(0, server_.AddService(&svc_, 
brpc::SERVER_DOESNT_OWN_SERVICE));
-        ASSERT_EQ(0, server_.Start(port_, nullptr));
+        ASSERT_EQ(0, server_.Start(0, nullptr));
         brpc::ChannelOptions options;
-        ASSERT_EQ(0, channel_.Init(butil::EndPoint(butil::my_ip(), port_),
-                                    &options));
+        ASSERT_EQ(0, channel_.Init(server_.listen_address(), &options));
     }
 
     void TearDown() override {
@@ -163,7 +162,6 @@ protected:
         server_.Join();
     }
 
-    const int port_ = 8934;
     brpc::Server server_;
     ChecksumEchoServiceImpl svc_;
     brpc::Channel channel_;
diff --git a/test/brpc_coroutine_unittest.cpp b/test/brpc_coroutine_unittest.cpp
index 0d549560..d4dcdb18 100644
--- a/test/brpc_coroutine_unittest.cpp
+++ b/test/brpc_coroutine_unittest.cpp
@@ -176,17 +176,14 @@ Awaitable<void> func(brpc::Channel& channel, int* out) {
 }
 
 TEST_F(CoroutineTest, coroutine) {
-    butil::EndPoint ep;
-    ASSERT_EQ(0, str2endpoint("127.0.0.1:8613", &ep));
-
     brpc::Server server;
     EchoServiceImpl service;
     server.AddService(&service, brpc::SERVER_DOESNT_OWN_SERVICE);
-    ASSERT_EQ(0, server.Start(ep, nullptr));
+    ASSERT_EQ(0, server.Start(0, nullptr));
 
     brpc::Channel channel;
     brpc::ChannelOptions options;
-    ASSERT_EQ(0, channel.Init(ep, &options));
+    ASSERT_EQ(0, channel.Init(server.listen_address(), &options));
 
     int out = 0;
     Coroutine coro(func(channel, &out));
diff --git a/test/brpc_grpc_protocol_unittest.cpp 
b/test/brpc_grpc_protocol_unittest.cpp
index 5a1bc8df..6136dda7 100644
--- a/test/brpc_grpc_protocol_unittest.cpp
+++ b/test/brpc_grpc_protocol_unittest.cpp
@@ -41,7 +41,6 @@ int main(int argc, char* argv[]) {
 
 namespace {
 
-const std::string g_server_addr = "127.0.0.1:8011";
 const std::string g_prefix = "Hello, ";
 const std::string g_req = "wyt";
 const int64_t g_timeout_ms = 1000;
@@ -103,11 +102,11 @@ class GrpcTest : public ::testing::Test {
 protected:
     GrpcTest() {
         EXPECT_EQ(0, _server.AddService(&_svc, 
brpc::SERVER_DOESNT_OWN_SERVICE));
-        EXPECT_EQ(0, _server.Start(g_server_addr.c_str(), nullptr));
+        EXPECT_EQ(0, _server.Start(0, nullptr));
         brpc::ChannelOptions options;
         options.protocol = g_protocol;
         options.timeout_ms = g_timeout_ms;
-        EXPECT_EQ(0, _channel.Init(g_server_addr.c_str(), "", &options));
+        EXPECT_EQ(0, _channel.Init(_server.listen_address(), &options));
     }
 
     virtual ~GrpcTest() {};
@@ -191,7 +190,7 @@ TEST_F(GrpcTest, RpcTimedOut) {
     brpc::ChannelOptions options;
     options.protocol = g_protocol;
     options.timeout_ms = g_timeout_ms;
-    EXPECT_EQ(0, channel.Init(g_server_addr.c_str(), "", &options));
+    EXPECT_EQ(0, channel.Init(_server.listen_address(), &options));
 
     test::GrpcRequest req;
     test::GrpcResponse res;
diff --git a/test/brpc_http_message_unittest.cpp 
b/test/brpc_http_message_unittest.cpp
index 90c9dbdd..078659be 100644
--- a/test/brpc_http_message_unittest.cpp
+++ b/test/brpc_http_message_unittest.cpp
@@ -707,7 +707,7 @@ TEST(HttpMessageTest, find_method_property_by_uri) {
     brpc::Server server;
     ASSERT_EQ(0, server.AddService(new test::EchoService(),
                                    brpc::SERVER_OWNS_SERVICE));
-    ASSERT_EQ(0, server.Start(9237, nullptr));
+    ASSERT_EQ(0, server.Start(0, nullptr));
     std::string unknown_method;
     brpc::Server::MethodProperty* mp = nullptr;
               
diff --git a/test/brpc_naming_service_unittest.cpp 
b/test/brpc_naming_service_unittest.cpp
index 570324fe..8ef33150 100644
--- a/test/brpc_naming_service_unittest.cpp
+++ b/test/brpc_naming_service_unittest.cpp
@@ -44,6 +44,7 @@ namespace policy {
 
 DECLARE_bool(consul_enable_degrade_to_file_naming_service);
 DECLARE_string(consul_file_naming_service_dir);
+DECLARE_string(consul_agent_addr);
 DECLARE_string(consul_service_discovery_url);
 DECLARE_string(discovery_api_addr);
 DECLARE_string(discovery_env);
@@ -69,6 +70,16 @@ bool IsIPListEqual(const std::set<butil::ip_t>& s1, const 
std::set<butil::ip_t>&
     return true;
 }
 
+class ScopedDiscoveryChannelReset {
+public:
+    ScopedDiscoveryChannelReset() {
+        brpc::policy::ResetDiscoveryChannelForTesting();
+    }
+    ~ScopedDiscoveryChannelReset() {
+        brpc::policy::ResetDiscoveryChannelForTesting();
+    }
+};
+
 TEST(NamingServiceTest, sanity) {
     std::vector<brpc::ServerNode> servers;
 
@@ -216,9 +227,7 @@ public:
         brpc::ClosureGuard done_guard(done);
         brpc::Controller* cntl = (brpc::Controller*)cntl_base;
         cntl->http_response().set_content_type("text/plain");
-        cntl->response_attachment().append(
-            "0.0.0.0:8635 tag1\r\n0.0.0.0:8636 tag2\n"
-            "0.0.0.0:8635 tag3\r\n0.0.0.0:8636\r\n");
+        cntl->response_attachment().append(_list_names);
         list_names_count.fetch_add(1);
     }
     void Touch(google::protobuf::RpcController*,
@@ -229,6 +238,7 @@ public:
         touch_count.fetch_add(1);
     }
 
+    std::string _list_names;
     butil::atomic<int64_t> list_names_count;
     butil::atomic<int64_t> touch_count;
 };
@@ -237,16 +247,18 @@ TEST(NamingServiceTest, remotefile) {
     brpc::Server server1;
     UserNamingServiceImpl svc1;
     ASSERT_EQ(0, server1.AddService(&svc1, brpc::SERVER_DOESNT_OWN_SERVICE));
-    ASSERT_EQ(0, server1.Start("localhost:8635", nullptr));
+    ASSERT_EQ(0, server1.Start(0, nullptr));
     brpc::Server server2;
     UserNamingServiceImpl svc2;
     ASSERT_EQ(0, server2.AddService(&svc2, brpc::SERVER_DOESNT_OWN_SERVICE));
-    ASSERT_EQ(0, server2.Start("localhost:8636", nullptr));
-
-    butil::EndPoint n1;
-    ASSERT_EQ(0, butil::str2endpoint("0.0.0.0:8635", &n1));
-    butil::EndPoint n2;
-    ASSERT_EQ(0, butil::str2endpoint("0.0.0.0:8636", &n2));
+    ASSERT_EQ(0, server2.Start(0, nullptr));
+
+    const butil::EndPoint n1 = server1.listen_address();
+    const butil::EndPoint n2 = server2.listen_address();
+    svc1._list_names = butil::string_printf(
+        "%s tag1\r\n%s tag2\n%s tag3\r\n%s\r\n",
+        butil::endpoint2str(n1).c_str(), butil::endpoint2str(n2).c_str(),
+        butil::endpoint2str(n1).c_str(), butil::endpoint2str(n2).c_str());
     std::vector<brpc::ServerNode> expected_servers;
     expected_servers.push_back(brpc::ServerNode(n1, "tag1"));
     expected_servers.push_back(brpc::ServerNode(n2, "tag2"));
@@ -256,14 +268,19 @@ TEST(NamingServiceTest, remotefile) {
 
     std::vector<brpc::ServerNode> servers;
     brpc::policy::RemoteFileNamingService rfns;
-    ASSERT_EQ(0, rfns.GetServers("0.0.0.0:8635/UserNamingService/ListNames", 
&servers));
+    const std::string rpc_service_name = butil::string_printf(
+        "%s/UserNamingService/ListNames", butil::endpoint2str(n1).c_str());
+    ASSERT_EQ(0, rfns.GetServers(rpc_service_name.c_str(), &servers));
     ASSERT_EQ(expected_servers.size(), servers.size());
     std::sort(servers.begin(), servers.end());
     for (size_t i = 0; i < expected_servers.size(); ++i) {
         ASSERT_EQ(expected_servers[i], servers[i]);
     }
 
-    ASSERT_EQ(0, 
rfns.GetServers("http://0.0.0.0:8635/UserNamingService/ListNames";, &servers));
+    const std::string http_service_name = butil::string_printf(
+        "http://%s/UserNamingService/ListNames";,
+        butil::endpoint2str(n1).c_str());
+    ASSERT_EQ(0, rfns.GetServers(http_service_name.c_str(), &servers));
     ASSERT_EQ(expected_servers.size(), servers.size());
     std::sort(servers.begin(), servers.end());
     for (size_t i = 0; i < expected_servers.size(); ++i) {
@@ -411,8 +428,8 @@ public:
 };
 
 TEST(NamingServiceTest, consul_with_backup_file) {
+    GFLAGS_NAMESPACE::FlagSaver flags_saver;
     brpc::policy::FLAGS_consul_enable_degrade_to_file_naming_service = true;
-    const int saved_hc_interval = brpc::FLAGS_health_check_interval;
     brpc::FLAGS_health_check_interval = 1;
     const char *address_list[] =  {
         "10.127.0.1:1234",
@@ -442,15 +459,17 @@ TEST(NamingServiceTest, consul_with_backup_file) {
 
     brpc::Server server;
     ConsulNamingServiceImpl svc;
-    std::string restful_map(brpc::policy::FLAGS_consul_service_discovery_url);
-    restful_map.append("/");
+    std::string restful_map("/v1/health/service/");
     restful_map.append(service_name);
     restful_map.append("   => ListNames");
     ASSERT_EQ(0, server.AddService(&svc,
                                    brpc::SERVER_DOESNT_OWN_SERVICE,
                                    restful_map.c_str()));
-    ASSERT_EQ(0, server.Start("localhost:8500", nullptr));
+    ASSERT_EQ(0, server.Start(0, nullptr));
+    brpc::policy::FLAGS_consul_agent_addr = butil::string_printf(
+        "http://%s";, butil::endpoint2str(server.listen_address()).c_str());
 
+    brpc::policy::ConsulNamingService dynamic_cns;
     bthread_usleep(5000000);
 
     butil::EndPoint n1;
@@ -463,13 +482,12 @@ TEST(NamingServiceTest, consul_with_backup_file) {
     std::sort(expected_servers.begin(), expected_servers.end());
 
     servers.clear();
-    ASSERT_EQ(0, cns.GetServers(service_name, &servers));
+    ASSERT_EQ(0, dynamic_cns.GetServers(service_name, &servers));
     ASSERT_EQ(expected_servers.size(), servers.size());
     std::sort(servers.begin(), servers.end());
     for (size_t i = 0; i < expected_servers.size(); ++i) {
         ASSERT_EQ(expected_servers[i], servers[i]);
     }
-    brpc::FLAGS_health_check_interval = saved_hc_interval;
 }
 
 
@@ -541,7 +559,7 @@ static const std::string s_fetchs_result = R"({
     }
 })";
 
-static std::string s_nodes_result = R"({
+static const char s_nodes_result[] = R"({
     "code": 0,
     "message": "0",
     "ttl": 1,
@@ -571,13 +589,17 @@ public:
         , _cancel_count(0) {}
     virtual ~DiscoveryNamingServiceImpl() {}
 
+    void SetNodesResult(const std::string& nodes_result) {
+        _nodes_result = nodes_result;
+    }
+
     void Nodes(google::protobuf::RpcController* cntl_base,
                const test::HttpRequest*,
                test::HttpResponse*,
                google::protobuf::Closure* done) {
         brpc::ClosureGuard done_guard(done);
         brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
-        cntl->response_attachment().append(s_nodes_result);
+        cntl->response_attachment().append(_nodes_result);
     }
 
     void Fetchs(google::protobuf::RpcController* cntl_base,
@@ -650,10 +672,12 @@ private:
     int _cancel_count;
 
     std::set<std::string> _addrs;
+    std::string _nodes_result;
 };
 
 TEST(NamingServiceTest, discovery_sanity) {
-    brpc::policy::FLAGS_discovery_api_addr = 
"http://127.0.0.1:8635/discovery/nodes";;
+    GFLAGS_NAMESPACE::FlagSaver flags_saver;
+    ScopedDiscoveryChannelReset reset_discovery_channel;
     brpc::policy::FLAGS_discovery_renew_interval_s = 1;
     brpc::Server server;
     DiscoveryNamingServiceImpl svc;
@@ -665,8 +689,19 @@ TEST(NamingServiceTest, discovery_sanity) {
         "/discovery/cancel => Cancel";
     ASSERT_EQ(0, server.AddService(&svc, brpc::SERVER_DOESNT_OWN_SERVICE,
                 rest_mapping.c_str()));
-    ASSERT_EQ(0, server.Start("localhost:8635", nullptr));
-
+    ASSERT_EQ(0, server.Start(0, nullptr));
+    brpc::policy::FLAGS_discovery_api_addr = butil::string_printf(
+        "http://%s/discovery/nodes";, butil::endpoint2str(
+            server.listen_address()).c_str());
+
+    const std::string server_address =
+        butil::endpoint2str(server.listen_address()).c_str();
+    std::string nodes_result(s_nodes_result);
+    const size_t server_address_pos = nodes_result.find("127.0.0.1:8635");
+    ASSERT_NE(std::string::npos, server_address_pos);
+    nodes_result.replace(server_address_pos, strlen("127.0.0.1:8635"),
+                         server_address);
+    svc.SetNodesResult(nodes_result);
     brpc::policy::DiscoveryNamingService dcns;
     std::vector<brpc::ServerNode> servers;
     ASSERT_EQ(0, dcns.GetServers("admin.test", &servers));
@@ -819,7 +854,7 @@ TEST(NamingServiceTest, nacos) {
     ASSERT_EQ(0, server.AddService(&svc, brpc::SERVER_DOESNT_OWN_SERVICE,
                                    "/nacos/v1/auth/login => Login, "
                                    "/nacos/v1/ns/instance/list => List"));
-    ASSERT_EQ(0, server.Start("localhost:8848", nullptr));
+    ASSERT_EQ(0, server.Start(0, nullptr));
 
     bthread_usleep(5000000);
 
@@ -829,7 +864,8 @@ TEST(NamingServiceTest, nacos) {
 
     const char* service_name =
         "serviceName=test&groupName=g1&namespaceId=n1&clusters=wx";
-    brpc::policy::FLAGS_nacos_address = "http://localhost:8848";;
+    brpc::policy::FLAGS_nacos_address = butil::string_printf(
+        "http://%s";, butil::endpoint2str(server.listen_address()).c_str());
     brpc::policy::FLAGS_nacos_username = "nacos";
     brpc::policy::FLAGS_nacos_password = "nacos";
 
diff --git a/test/brpc_rtmp_unittest.cpp b/test/brpc_rtmp_unittest.cpp
index 397e5d3b..da2e77af 100644
--- a/test/brpc_rtmp_unittest.cpp
+++ b/test/brpc_rtmp_unittest.cpp
@@ -1173,14 +1173,14 @@ TEST(RtmpTest, successfully_play_streams) {
     brpc::Server server;
     brpc::ServerOptions server_opt;
     server_opt.rtmp_service = &rtmp_service;
-    ASSERT_EQ(0, server.Start(8571, &server_opt));
+    ASSERT_EQ(0, server.Start(0, &server_opt));
 
     brpc::RtmpClientOptions rtmp_opt;
     rtmp_opt.app = "hello";
     rtmp_opt.swfUrl = "anything";
     rtmp_opt.tcUrl = "rtmp://heheda";
     brpc::RtmpClient rtmp_client;
-    ASSERT_EQ(0, rtmp_client.Init("localhost:8571", rtmp_opt));
+    ASSERT_EQ(0, rtmp_client.Init(server.listen_address(), rtmp_opt));
 
     // Create multiple streams.
     const int NSTREAM = 2;
@@ -1205,14 +1205,14 @@ TEST(RtmpTest, fail_to_play_streams) {
     brpc::Server server;
     brpc::ServerOptions server_opt;
     server_opt.rtmp_service = &rtmp_service;
-    ASSERT_EQ(0, server.Start(8571, &server_opt));
+    ASSERT_EQ(0, server.Start(0, &server_opt));
 
     brpc::RtmpClientOptions rtmp_opt;
     rtmp_opt.app = "hello";
     rtmp_opt.swfUrl = "anything";
     rtmp_opt.tcUrl = "rtmp://heheda";
     brpc::RtmpClient rtmp_client;
-    ASSERT_EQ(0, rtmp_client.Init("localhost:8571", rtmp_opt));
+    ASSERT_EQ(0, rtmp_client.Init(server.listen_address(), rtmp_opt));
 
     // Create multiple streams.
     const int NSTREAM = 2;
@@ -1236,14 +1236,14 @@ TEST(RtmpTest, successfully_publish_streams) {
     brpc::Server server;
     brpc::ServerOptions server_opt;
     server_opt.rtmp_service = &rtmp_service;
-    ASSERT_EQ(0, server.Start(8571, &server_opt));
+    ASSERT_EQ(0, server.Start(0, &server_opt));
 
     brpc::RtmpClientOptions rtmp_opt;
     rtmp_opt.app = "hello";
     rtmp_opt.swfUrl = "anything";
     rtmp_opt.tcUrl = "rtmp://heheda";
     brpc::RtmpClient rtmp_client;
-    ASSERT_EQ(0, rtmp_client.Init("localhost:8571", rtmp_opt));
+    ASSERT_EQ(0, rtmp_client.Init(server.listen_address(), rtmp_opt));
 
     // Create multiple streams.
     const int NSTREAM = 2;
@@ -1299,14 +1299,14 @@ TEST(RtmpTest, failed_to_publish_streams) {
     brpc::Server server;
     brpc::ServerOptions server_opt;
     server_opt.rtmp_service = &rtmp_service;
-    ASSERT_EQ(0, server.Start(8575, &server_opt));
+    ASSERT_EQ(0, server.Start(0, &server_opt));
 
     brpc::RtmpClientOptions rtmp_opt;
     rtmp_opt.app = "hello";
     rtmp_opt.swfUrl = "anything";
     rtmp_opt.tcUrl = "rtmp://heheda";
     brpc::RtmpClient rtmp_client;
-    ASSERT_EQ(0, rtmp_client.Init("localhost:8575", rtmp_opt));
+    ASSERT_EQ(0, rtmp_client.Init(server.listen_address(), rtmp_opt));
 
     // Create multiple streams.
     const int NSTREAM = 2;
@@ -1409,14 +1409,14 @@ TEST(RtmpTest, destroy_client_streams_during_creation) {
     brpc::Server server;
     brpc::ServerOptions server_opt;
     server_opt.rtmp_service = &rtmp_service;
-    ASSERT_EQ(0, server.Start(8574, &server_opt));
+    ASSERT_EQ(0, server.Start(0, &server_opt));
 
     brpc::RtmpClientOptions rtmp_opt;
     rtmp_opt.app = "hello";
     rtmp_opt.swfUrl = "anything";
     rtmp_opt.tcUrl = "rtmp://heheda";
     brpc::RtmpClient rtmp_client;
-    ASSERT_EQ(0, rtmp_client.Init("localhost:8574", rtmp_opt));
+    ASSERT_EQ(0, rtmp_client.Init(server.listen_address(), rtmp_opt));
 
     // Create multiple streams.
     const int NSTREAM = 2;
@@ -1441,14 +1441,14 @@ TEST(RtmpTest, 
destroy_retrying_client_streams_during_creation) {
     brpc::Server server;
     brpc::ServerOptions server_opt;
     server_opt.rtmp_service = &rtmp_service;
-    ASSERT_EQ(0, server.Start(8574, &server_opt));
+    ASSERT_EQ(0, server.Start(0, &server_opt));
 
     brpc::RtmpClientOptions rtmp_opt;
     rtmp_opt.app = "hello";
     rtmp_opt.swfUrl = "anything";
     rtmp_opt.tcUrl = "rtmp://heheda";
     brpc::RtmpClient rtmp_client;
-    ASSERT_EQ(0, rtmp_client.Init("localhost:8574", rtmp_opt));
+    ASSERT_EQ(0, rtmp_client.Init(server.listen_address(), rtmp_opt));
 
     // Create multiple streams.
     const int NSTREAM = 2;
@@ -1474,14 +1474,15 @@ TEST(RtmpTest, retrying_stream) {
     brpc::Server server;
     brpc::ServerOptions server_opt;
     server_opt.rtmp_service = &rtmp_service;
-    ASSERT_EQ(0, server.Start(8576, &server_opt));
+    ASSERT_EQ(0, server.Start(0, &server_opt));
+    const butil::EndPoint server_address = server.listen_address();
 
     brpc::RtmpClientOptions rtmp_opt;
     rtmp_opt.app = "hello";
     rtmp_opt.swfUrl = "anything";
     rtmp_opt.tcUrl = "rtmp://heheda";
     brpc::RtmpClient rtmp_client;
-    ASSERT_EQ(0, rtmp_client.Init("localhost:8576", rtmp_opt));
+    ASSERT_EQ(0, rtmp_client.Init(server_address, rtmp_opt));
 
     // Create multiple streams.
     const int NSTREAM = 2;
@@ -1500,7 +1501,7 @@ TEST(RtmpTest, retrying_stream) {
     server.Join();
     LOG(INFO) << "Stopped server and sleep for a while";
     sleep(3);
-    ASSERT_EQ(0, server.Start(8576, &server_opt));
+    ASSERT_EQ(0, server.Start(server_address, &server_opt));
     sleep(3);
     for (int i = 0; i < NSTREAM; ++i) {
         ASSERT_EQ(1, cstreams[i]->_called_on_first_message);


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

Reply via email to