From e481703ffc5f53cd53bf3411589f75826cf99c6b Mon Sep 17 00:00:00 2001
From: Daijiro Fukuda <fukuda@clear-code.com>
Date: Mon, 13 Mar 2023 03:01:02 +0900
Subject: [PATCH] SocketManager: Use open_auto_path

Use ServerEngine's new feature:

https://github.com/treasure-data/serverengine/pull/143

On Windows, this prevents SocketManager from wrongly selecting an
unavailable port, such as a port in excluded port range.

Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
---
 lib/fluent/supervisor.rb            | 10 ++++------
 lib/fluent/test/driver/base.rb      | 14 ++++++++++----
 lib/fluent/test/startup_shutdown.rb | 14 ++++++--------
 test/plugin/test_in_http.rb         |  5 ++---
 test/plugin_helper/test_server.rb   | 13 +++++++++----
 5 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/lib/fluent/supervisor.rb b/lib/fluent/supervisor.rb
index 043e447f5a..3894c04db1 100644
--- a/lib/fluent/supervisor.rb
+++ b/lib/fluent/supervisor.rb
@@ -66,9 +66,8 @@ def before_run
       if config[:disable_shared_socket]
         $log.info "shared socket for multiple workers is disabled"
       else
-        socket_manager_path = ServerEngine::SocketManager::Server.generate_path
-        ServerEngine::SocketManager::Server.open(socket_manager_path)
-        ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
+        server = ServerEngine::SocketManager::Server.open_auto_path
+        ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = server.path.to_s
       end
     end
 
@@ -801,9 +800,8 @@ def configure(supervisor: false)
     private
 
     def create_socket_manager
-      socket_manager_path = ServerEngine::SocketManager::Server.generate_path
-      ServerEngine::SocketManager::Server.open(socket_manager_path)
-      ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
+      server = ServerEngine::SocketManager::Server.open_auto_path
+      ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = server.path.to_s
     end
 
     def show_plugin_config
diff --git a/lib/fluent/test/driver/base.rb b/lib/fluent/test/driver/base.rb
index b53f863db4..d56163d1cd 100644
--- a/lib/fluent/test/driver/base.rb
+++ b/lib/fluent/test/driver/base.rb
@@ -16,6 +16,7 @@
 
 require 'fluent/config'
 require 'fluent/config/element'
+require 'fluent/env'
 require 'fluent/log'
 require 'fluent/clock'
 
@@ -102,11 +103,16 @@ def run(timeout: nil, start: true, shutdown: true, &block)
 
         def instance_start
           if @instance.respond_to?(:server_wait_until_start)
-            @socket_manager_path = ServerEngine::SocketManager::Server.generate_path
-            if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path)
-              FileUtils.rm_f @socket_manager_path
+            if Fluent.windows?
+              @socket_manager_server = ServerEngine::SocketManager::Server.open_auto_path
+              @socket_manager_path = @socket_manager_server.path
+            else
+              @socket_manager_path = ServerEngine::SocketManager::Server.generate_path
+              if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path)
+                FileUtils.rm_f @socket_manager_path
+              end
+              @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path)
             end
-            @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path)
             ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_path.to_s
           end
 
diff --git a/lib/fluent/test/startup_shutdown.rb b/lib/fluent/test/startup_shutdown.rb
index 62da263f24..9280c0c098 100644
--- a/lib/fluent/test/startup_shutdown.rb
+++ b/lib/fluent/test/startup_shutdown.rb
@@ -21,9 +21,8 @@ module Fluent
   module Test
     module StartupShutdown
       def startup
-        socket_manager_path = ServerEngine::SocketManager::Server.generate_path
-        @server = ServerEngine::SocketManager::Server.open(socket_manager_path)
-        ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
+        @server = ServerEngine::SocketManager::Server.open_auto_path
+        ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @server.path.to_s
       end
 
       def shutdown
@@ -31,15 +30,14 @@ def shutdown
       end
 
       def self.setup
-        @socket_manager_path = ServerEngine::SocketManager::Server.generate_path
-        @server = ServerEngine::SocketManager::Server.open(@socket_manager_path)
-        ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_path.to_s
+        @server = ServerEngine::SocketManager::Server.open_auto_path
+        ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @server.path.to_s
       end
 
       def self.teardown
         @server.close
-        # on Windows, socket_manager_path is a TCP port number
-        FileUtils.rm_f @socket_manager_path unless Fluent.windows?
+        # on Windows, the path is a TCP port number
+        FileUtils.rm_f @server.path unless Fluent.windows?
       end
     end
   end
diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb
index 93cada30a8..ad6dc02e43 100644
--- a/test/plugin/test_in_http.rb
+++ b/test/plugin/test_in_http.rb
@@ -7,9 +7,8 @@
 class HttpInputTest < Test::Unit::TestCase
   class << self
     def startup
-      socket_manager_path = ServerEngine::SocketManager::Server.generate_path
-      @server = ServerEngine::SocketManager::Server.open(socket_manager_path)
-      ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = socket_manager_path.to_s
+      @server = ServerEngine::SocketManager::Server.open_auto_path
+      ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @server.path.to_s
     end
 
     def shutdown
diff --git a/test/plugin_helper/test_server.rb b/test/plugin_helper/test_server.rb
index a08734ae46..aaf059a2dc 100644
--- a/test/plugin_helper/test_server.rb
+++ b/test/plugin_helper/test_server.rb
@@ -16,11 +16,16 @@ class Dummy < Fluent::Plugin::TestBase
 
   setup do
     @port = unused_port
-    @socket_manager_path = ServerEngine::SocketManager::Server.generate_path
-    if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path)
-      FileUtils.rm_f @socket_manager_path
+    if Fluent.windows?
+      @socket_manager_server = ServerEngine::SocketManager::Server.open_auto_path
+      @socket_manager_path = @socket_manager_server.path
+    else
+      @socket_manager_path = ServerEngine::SocketManager::Server.generate_path
+      if @socket_manager_path.is_a?(String) && File.exist?(@socket_manager_path)
+        FileUtils.rm_f @socket_manager_path
+      end
+      @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path)
     end
-    @socket_manager_server = ServerEngine::SocketManager::Server.open(@socket_manager_path)
     ENV['SERVERENGINE_SOCKETMANAGER_PATH'] = @socket_manager_path.to_s
 
     @d = Dummy.new