From 0634bd5e4488c2a11895beaf60e341074914ff82 Mon Sep 17 00:00:00 2001 From: Watson Date: Sun, 20 Oct 2024 17:25:38 +0900 Subject: [PATCH] tests: fix unused_port It obtains unused port number for TCP by unused_port method and the number has been used in UDP. And that number may be already used by UDP sockets. This patch will obtain and use unused ports appropriately for each protocol. Signed-off-by: Watson --- test/command/test_cat.rb | 2 +- test/command/test_fluentd.rb | 2 +- test/helper.rb | 34 ++++-- test/plugin/test_in_forward.rb | 3 +- test/plugin/test_in_http.rb | 2 +- test/plugin/test_in_monitor_agent.rb | 12 +- test/plugin/test_in_syslog.rb | 43 ++++--- test/plugin/test_in_tcp.rb | 2 +- test/plugin/test_in_udp.rb | 2 +- test/plugin/test_out_forward.rb | 3 +- test/plugin/test_out_stream.rb | 2 +- test/plugin_helper/test_http_server_helper.rb | 2 +- test/plugin_helper/test_server.rb | 105 +++++++++++------- test/plugin_helper/test_socket.rb | 2 +- 14 files changed, 134 insertions(+), 82 deletions(-) diff --git a/test/command/test_cat.rb b/test/command/test_cat.rb index 51c1a20bbb..f1e1067823 100644 --- a/test/command/test_cat.rb +++ b/test/command/test_cat.rb @@ -18,7 +18,7 @@ def setup @primary = create_primary metadata = @primary.buffer.new_metadata @chunk = create_chunk(@primary, metadata, @es) - @port = unused_port + @port = unused_port(protocol: :tcp) end def teardown diff --git a/test/command/test_fluentd.rb b/test/command/test_fluentd.rb index 60e0b131ed..0b2106f129 100644 --- a/test/command/test_fluentd.rb +++ b/test/command/test_fluentd.rb @@ -1175,7 +1175,7 @@ def multi_workers_ready?; true; end end end - sub_test_case 'sahred socket options' do + sub_test_case 'shared socket options' do test 'enable shared socket by default' do conf = "" conf_path = create_conf_file('empty.conf', conf) diff --git a/test/helper.rb b/test/helper.rb index 9dc8633423..4e9f028a9c 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -71,17 +71,31 @@ class Test::Unit::AssertionFailedError < StandardError include Fluent::Test::Helpers -def unused_port(num = 1, protocol: :tcp, bind: "0.0.0.0") +def unused_port(num = 1, protocol:, bind: "0.0.0.0") case protocol - when :tcp + when :tcp, :tls unused_port_tcp(num) when :udp unused_port_udp(num, bind: bind) + when :all + unused_port_tcp_udp(num) else raise ArgumentError, "unknown protocol: #{protocol}" end end +def unused_port_tcp_udp(num = 1) + raise "not support num > 1" if num > 1 + + # The default maximum number of file descriptors in macOS is 256. + # It might need to set num to a smaller value than that. + tcp_ports = unused_port_tcp(200) + port = unused_port_udp(1, port_list: tcp_ports) + raise "can't find unused port" unless port + + port +end + def unused_port_tcp(num = 1) ports = [] sockets = [] @@ -90,7 +104,7 @@ def unused_port_tcp(num = 1) sockets << s ports << s.addr[1] end - sockets.each{|s| s.close } + sockets.each(&:close) if num == 1 return ports.first else @@ -100,12 +114,15 @@ def unused_port_tcp(num = 1) PORT_RANGE_AVAILABLE = (1024...65535) -def unused_port_udp(num = 1, bind: "0.0.0.0") +def unused_port_udp(num = 1, port_list: [], bind: "0.0.0.0") family = IPAddr.new(IPSocket.getaddress(bind)).ipv4? ? ::Socket::AF_INET : ::Socket::AF_INET6 ports = [] sockets = [] - while ports.size < num - port = rand(PORT_RANGE_AVAILABLE) + + use_random_port = port_list.empty? + i = 0 + loop do + port = use_random_port ? rand(PORT_RANGE_AVAILABLE) : port_list[i] u = UDPSocket.new(family) if (u.bind(bind, port) rescue nil) ports << port @@ -113,8 +130,11 @@ def unused_port_udp(num = 1, bind: "0.0.0.0") else u.close end + i += 1 + break if ports.size >= num + break if !use_random_port && i >= port_list.size end - sockets.each{|s| s.close } + sockets.each(&:close) if num == 1 return ports.first else diff --git a/test/plugin/test_in_forward.rb b/test/plugin/test_in_forward.rb index 9a3b46fd9a..cbebe91593 100644 --- a/test/plugin/test_in_forward.rb +++ b/test/plugin/test_in_forward.rb @@ -18,7 +18,8 @@ def setup Fluent::Test.setup @responses = [] # for testing responses after sending data @d = nil - @port = unused_port + # forward plugin uses TCP and UDP sockets on the same port number + @port = unused_port(protocol: :all) end def teardown diff --git a/test/plugin/test_in_http.rb b/test/plugin/test_in_http.rb index 3b9107f224..e60e30b402 100644 --- a/test/plugin/test_in_http.rb +++ b/test/plugin/test_in_http.rb @@ -18,7 +18,7 @@ def shutdown def setup Fluent::Test.setup - @port = unused_port + @port = unused_port(protocol: :tcp) end def teardown diff --git a/test/plugin/test_in_monitor_agent.rb b/test/plugin/test_in_monitor_agent.rb index 76e0a26a18..eb8d2dea7c 100644 --- a/test/plugin/test_in_monitor_agent.rb +++ b/test/plugin/test_in_monitor_agent.rb @@ -392,7 +392,7 @@ def test_enable_input_metrics(with_config) end test "emit" do - port = unused_port + port = unused_port(protocol: :tcp) d = create_driver(" @type monitor_agent bind '127.0.0.1' @@ -451,7 +451,7 @@ def get(uri, header = {}) sub_test_case "servlets" do setup do - @port = unused_port + @port = unused_port(protocol: :tcp) # check @type and type in one configuration conf = <<-EOC @@ -759,7 +759,7 @@ def write(chunk) end setup do - @port = unused_port + @port = unused_port(protocol: :tcp) # check @type and type in one configuration conf = <<-EOC @@ -840,7 +840,7 @@ def write(chunk) sub_test_case "check the port number of http server" do test "on single worker environment" do - port = unused_port + port = unused_port(protocol: :tcp) d = create_driver(" @type monitor_agent bind '127.0.0.1' @@ -851,7 +851,7 @@ def write(chunk) end test "worker_id = 2 on multi worker environment" do - port = unused_port + port = unused_port(protocol: :tcp) Fluent::SystemConfig.overwrite_system_config('workers' => 4) do d = Fluent::Test::Driver::Input.new(Fluent::Plugin::MonitorAgentInput) d.instance.instance_eval{ @_fluentd_worker_id = 2 } @@ -905,7 +905,7 @@ def filter(tag, time, record) end test "plugins have a variable named buffer does not throws NoMethodError" do - port = unused_port + port = unused_port(protocol: :tcp) d = create_driver(" @type monitor_agent bind '127.0.0.1' diff --git a/test/plugin/test_in_syslog.rb b/test/plugin/test_in_syslog.rb index f715e1fef9..868e77bb04 100755 --- a/test/plugin/test_in_syslog.rb +++ b/test/plugin/test_in_syslog.rb @@ -5,24 +5,24 @@ class SyslogInputTest < Test::Unit::TestCase def setup Fluent::Test.setup - @port = unused_port + @port = unused_port(protocol: :udp) end def teardown @port = nil end - def ipv4_config + def ipv4_config(port = @port) %[ - port #{@port} + port #{port} bind 127.0.0.1 tag syslog ] end - def ipv6_config + def ipv6_config(port = @port) %[ - port #{@port} + port #{port} bind ::1 tag syslog ] @@ -69,7 +69,8 @@ def test_configure_resolve_hostname(param) 'Use transport and protocol' => ["protocol_type udp\n\n ", :udp, :tcp]) def test_configure_protocol(param) conf, proto_type, transport_proto_type = *param - d = create_driver([ipv4_config, conf].join("\n")) + port = unused_port(protocol: proto_type ? proto_type : transport_proto_type) + d = create_driver([ipv4_config(port), conf].join("\n")) assert_equal(d.instance.protocol_type, proto_type) assert_equal(d.instance.transport_config.protocol, transport_proto_type) @@ -158,12 +159,13 @@ def test_msg_size_udp_for_large_msg end def test_msg_size_with_tcp - d = create_driver([ipv4_config, " \n"].join("\n")) + port = unused_port(protocol: :tcp) + d = create_driver([ipv4_config(port), " \n"].join("\n")) tests = create_test_case d.run(expect_emits: 2) do tests.each {|test| - TCPSocket.open('127.0.0.1', @port) do |s| + TCPSocket.open('127.0.0.1', port) do |s| s.send(test['msg'], 0) end } @@ -189,11 +191,12 @@ def test_emit_rfc5452 end def test_msg_size_with_same_tcp_connection - d = create_driver([ipv4_config, " \n"].join("\n")) + port = unused_port(protocol: :tcp) + d = create_driver([ipv4_config(port), " \n"].join("\n")) tests = create_test_case d.run(expect_emits: 2) do - TCPSocket.open('127.0.0.1', @port) do |s| + TCPSocket.open('127.0.0.1', port) do |s| tests.each {|test| s.send(test['msg'], 0) } @@ -347,12 +350,13 @@ def compare_test_result(events, tests, options = {}) sub_test_case 'octet counting frame' do def test_msg_size_with_tcp - d = create_driver([ipv4_config, " \n", 'frame_type octet_count'].join("\n")) + port = unused_port(protocol: :tcp) + d = create_driver([ipv4_config(port), " \n", 'frame_type octet_count'].join("\n")) tests = create_test_case d.run(expect_emits: 2) do tests.each {|test| - TCPSocket.open('127.0.0.1', @port) do |s| + TCPSocket.open('127.0.0.1', port) do |s| s.send(test['msg'], 0) end } @@ -363,11 +367,12 @@ def test_msg_size_with_tcp end def test_msg_size_with_same_tcp_connection - d = create_driver([ipv4_config, " \n", 'frame_type octet_count'].join("\n")) + port = unused_port(protocol: :tcp) + d = create_driver([ipv4_config(port), " \n", 'frame_type octet_count'].join("\n")) tests = create_test_case d.run(expect_emits: 2) do - TCPSocket.open('127.0.0.1', @port) do |s| + TCPSocket.open('127.0.0.1', port) do |s| tests.each {|test| s.send(test['msg'], 0) } @@ -469,7 +474,8 @@ def test_emit_unmatched_lines_with_address end def test_send_keepalive_packet_is_disabled_by_default - d = create_driver(ipv4_config + %[ + port = unused_port(protocol: :tcp) + d = create_driver(ipv4_config(port) + %[ protocol tcp @@ -479,19 +485,20 @@ def test_send_keepalive_packet_is_disabled_by_default def test_send_keepalive_packet_can_be_enabled addr = "127.0.0.1" - d = create_driver(ipv4_config + %[ + port = unused_port(protocol: :tcp) + d = create_driver(ipv4_config(port) + %[ send_keepalive_packet true ]) assert_true d.instance.send_keepalive_packet mock.proxy(d.instance).server_create_connection( - :in_syslog_tcp_server, @port, + :in_syslog_tcp_server, port, bind: addr, resolve_name: nil, send_keepalive_packet: true) d.run do - TCPSocket.open(addr, @port) + TCPSocket.open(addr, port) end end diff --git a/test/plugin/test_in_tcp.rb b/test/plugin/test_in_tcp.rb index c1d917332e..a27e00bba3 100755 --- a/test/plugin/test_in_tcp.rb +++ b/test/plugin/test_in_tcp.rb @@ -5,7 +5,7 @@ class TcpInputTest < Test::Unit::TestCase def setup Fluent::Test.setup - @port = unused_port + @port = unused_port(protocol: :tcp) end def teardown diff --git a/test/plugin/test_in_udp.rb b/test/plugin/test_in_udp.rb index dcc63e1f8f..1f1bb457cb 100755 --- a/test/plugin/test_in_udp.rb +++ b/test/plugin/test_in_udp.rb @@ -5,7 +5,7 @@ class UdpInputTest < Test::Unit::TestCase def setup Fluent::Test.setup - @port = unused_port + @port = unused_port(protocol: :udp) end def teardown diff --git a/test/plugin/test_out_forward.rb b/test/plugin/test_out_forward.rb index 438caf8fa2..306d9d8234 100644 --- a/test/plugin/test_out_forward.rb +++ b/test/plugin/test_out_forward.rb @@ -12,7 +12,8 @@ def setup FileUtils.rm_rf(TMP_DIR) FileUtils.mkdir_p(TMP_DIR) @d = nil - @target_port = unused_port + # forward plugin uses TCP and UDP sockets on the same port number + @target_port = unused_port(protocol: :all) end def teardown diff --git a/test/plugin/test_out_stream.rb b/test/plugin/test_out_stream.rb index 05e9011aab..f19328f5be 100644 --- a/test/plugin/test_out_stream.rb +++ b/test/plugin/test_out_stream.rb @@ -54,7 +54,7 @@ class TcpOutputTest < Test::Unit::TestCase def setup super - @port = unused_port + @port = unused_port(protocol: :tcp) end def teardown diff --git a/test/plugin_helper/test_http_server_helper.rb b/test/plugin_helper/test_http_server_helper.rb index 24312a9843..bc9853c013 100644 --- a/test/plugin_helper/test_http_server_helper.rb +++ b/test/plugin_helper/test_http_server_helper.rb @@ -14,7 +14,7 @@ class HttpHelperTest < Test::Unit::TestCase CERT_CA_DIR = File.expand_path(File.dirname(__FILE__) + '/data/cert/with_ca') def setup - @port = unused_port + @port = unused_port(protocol: :tcp) end def teardown diff --git a/test/plugin_helper/test_server.rb b/test/plugin_helper/test_server.rb index 347938eca3..63beb0bd70 100644 --- a/test/plugin_helper/test_server.rb +++ b/test/plugin_helper/test_server.rb @@ -15,7 +15,7 @@ class Dummy < Fluent::Plugin::TestBase TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/../tmp/plugin_helper_server") setup do - @port = unused_port + @port = unused_port(protocol: :tcp) if Fluent.windows? @socket_manager_server = ServerEngine::SocketManager::Server.open @socket_manager_path = @socket_manager_server.path @@ -233,11 +233,12 @@ class Dummy < Fluent::Plugin::TestBase # 'server_create_connection tcp' => [:server_create_connection, :unix], ) test 'raise error if udp options specified for tcp/tls/unix' do |(m, proto)| + port = unused_port(protocol: proto) assert_raise ArgumentError do - @d.__send__(m, :myserver, @port, proto: proto, max_bytes: 128){|x| x } + @d.__send__(m, :myserver, port, proto: proto, max_bytes: 128){|x| x } end assert_raise ArgumentError do - @d.__send__(m, :myserver, @port, proto: proto, flags: 1){|x| x } + @d.__send__(m, :myserver, port, proto: proto, flags: 1){|x| x } end end @@ -245,8 +246,9 @@ class Dummy < Fluent::Plugin::TestBase 'server_create udp' => [:server_create, :udp], ) test 'raise error if tcp/tls options specified for udp' do |(m, proto)| + port = unused_port(protocol: proto) assert_raise(ArgumentError.new("BUG: linger_timeout is available for tcp/tls")) do - @d.__send__(m, :myserver, @port, proto: proto, linger_timeout: 1, max_bytes: 128){|x| x } + @d.__send__(m, :myserver, port, proto: proto, linger_timeout: 1, max_bytes: 128){|x| x } end end @@ -254,8 +256,9 @@ class Dummy < Fluent::Plugin::TestBase 'server_create udp' => [:server_create, :udp], ) test 'raise error if tcp/tls/unix backlog options specified for udp' do |(m, proto)| + port = unused_port(protocol: proto) assert_raise(ArgumentError.new("BUG: backlog is available for tcp/tls")) do - @d.__send__(m, :myserver, @port, proto: proto, backlog: 500){|x| x } + @d.__send__(m, :myserver, port, proto: proto, backlog: 500){|x| x } end end @@ -263,8 +266,9 @@ class Dummy < Fluent::Plugin::TestBase 'server_create udp' => [:server_create, :udp], ) test 'raise error if tcp/tls send_keepalive_packet option is specified for udp' do |(m, proto)| + port = unused_port(protocol: proto) assert_raise(ArgumentError.new("BUG: send_keepalive_packet is available for tcp/tls")) do - @d.__send__(m, :myserver, @port, proto: proto, send_keepalive_packet: true){|x| x } + @d.__send__(m, :myserver, port, proto: proto, send_keepalive_packet: true){|x| x } end end @@ -276,8 +280,9 @@ class Dummy < Fluent::Plugin::TestBase # 'server_create_connection unix' => [:server_create_connection, :unix, {}], ) test 'raise error if tls options specified for tcp/udp/unix' do |(m, proto, kwargs)| + port = unused_port(protocol: proto) assert_raise(ArgumentError.new("BUG: tls_options is available only for tls")) do - @d.__send__(m, :myserver, @port, proto: proto, tls_options: {}, **kwargs){|x| x } + @d.__send__(m, :myserver, port, proto: proto, tls_options: {}, **kwargs){|x| x } end end @@ -289,7 +294,8 @@ class Dummy < Fluent::Plugin::TestBase 'server_create_connection tls' => [:server_create_connection, :tls, {tls_options: {insecure: true}}], ) test 'can bind specified IPv4 address' do |(m, proto, kwargs)| - @d.__send__(m, :myserver, @port, proto: proto, bind: "127.0.0.1", **kwargs){|x| x } + port = unused_port(protocol: proto) + @d.__send__(m, :myserver, port, proto: proto, bind: "127.0.0.1", **kwargs){|x| x } assert_equal "127.0.0.1", @d._servers.first.bind assert_equal "127.0.0.1", @d._servers.first.server.instance_eval{ instance_variable_defined?(:@listen_socket) ? @listen_socket : @_io }.addr[3] end @@ -303,7 +309,8 @@ class Dummy < Fluent::Plugin::TestBase ) test 'can bind specified IPv6 address' do |(m, proto, kwargs)| # if available omit "IPv6 unavailable here" unless ipv6_enabled? - @d.__send__(m, :myserver, @port, proto: proto, bind: "::1", **kwargs){|x| x } + port = unused_port(protocol: proto) + @d.__send__(m, :myserver, port, proto: proto, bind: "::1", **kwargs){|x| x } assert_equal "::1", @d._servers.first.bind assert_equal "::1", @d._servers.first.server.instance_eval{ instance_variable_defined?(:@listen_socket) ? @listen_socket : @_io }.addr[3] end @@ -320,10 +327,11 @@ class Dummy < Fluent::Plugin::TestBase test 'can create 2 or more servers which share same bind address and port if shared option is true' do |(m, proto, kwargs)| begin d2 = Dummy.new; d2.start; d2.after_start + port = unused_port(protocol: proto) assert_nothing_raised do - @d.__send__(m, :myserver, @port, proto: proto, **kwargs){|x| x } - d2.__send__(m, :myserver, @port, proto: proto, **kwargs){|x| x } + @d.__send__(m, :myserver, port, proto: proto, **kwargs){|x| x } + d2.__send__(m, :myserver, port, proto: proto, **kwargs){|x| x } end ensure d2.stop; d2.before_shutdown; d2.shutdown; d2.after_shutdown; d2.close; d2.terminate @@ -344,12 +352,13 @@ class Dummy < Fluent::Plugin::TestBase test 'cannot create 2 or more servers using same bind address and port if shared option is false' do |(m, proto, kwargs)| begin d2 = Dummy.new; d2.start; d2.after_start + port = unused_port(protocol: proto) assert_nothing_raised do - @d.__send__(m, :myserver, @port, proto: proto, shared: false, **kwargs){|x| x } + @d.__send__(m, :myserver, port, proto: proto, shared: false, **kwargs){|x| x } end assert_raise(Errno::EADDRINUSE, Errno::EACCES) do - d2.__send__(m, :myserver, @port, proto: proto, **kwargs){|x| x } + d2.__send__(m, :myserver, port, proto: proto, **kwargs){|x| x } end ensure d2.stop; d2.before_shutdown; d2.shutdown; d2.after_shutdown; d2.close; d2.terminate @@ -365,16 +374,18 @@ class Dummy < Fluent::Plugin::TestBase # 'unix' => [:unix, {}], ) test 'raise error if block argument is not specified or too many' do |(proto, kwargs)| + port = unused_port(protocol: proto) assert_raise(ArgumentError.new("BUG: block must have 1 or 2 arguments")) do - @d.server_create(:myserver, @port, proto: proto, **kwargs){ 1 } + @d.server_create(:myserver, port, proto: proto, **kwargs){ 1 } end assert_raise(ArgumentError.new("BUG: block must have 1 or 2 arguments")) do - @d.server_create(:myserver, @port, proto: proto, **kwargs){|sock, conn, what_is_this| 1 } + @d.server_create(:myserver, port, proto: proto, **kwargs){|sock, conn, what_is_this| 1 } end end test 'creates udp server if specified in proto' do - @d.server_create(:myserver, @port, proto: :udp, max_bytes: 512){|x| x } + port = unused_port(protocol: :udp) + @d.server_create(:myserver, port, proto: :udp, max_bytes: 512){|x| x } created_server_info = @d._servers.first assert_equal :udp, created_server_info.proto @@ -587,7 +598,8 @@ class Dummy < Fluent::Plugin::TestBase sub_test_case '#server_create_udp' do test 'can accept all keyword arguments valid for udp server' do assert_nothing_raised do - @d.server_create_udp(:s, @port, bind: '127.0.0.1', shared: false, resolve_name: true, max_bytes: 100, flags: 1) do |data, conn| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, bind: '127.0.0.1', shared: false, resolve_name: true, max_bytes: 100, flags: 1) do |data, conn| # ... end end @@ -595,14 +607,15 @@ class Dummy < Fluent::Plugin::TestBase test 'creates a udp server just to read data' do received = "" - @d.server_create_udp(:s, @port, max_bytes: 128) do |data| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, max_bytes: 128) do |data| received << data end bind_port = unused_port(protocol: :udp, bind: "127.0.0.1") 3.times do sock = UDPSocket.new(Socket::AF_INET) sock.bind("127.0.0.1", bind_port) - sock.connect("127.0.0.1", @port) + sock.connect("127.0.0.1", port) sock.puts "yay" sock.puts "foo" sock.close @@ -614,16 +627,17 @@ class Dummy < Fluent::Plugin::TestBase test 'creates a udp server to read and write data' do received = "" responses = [] - @d.server_create_udp(:s, @port, max_bytes: 128) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, max_bytes: 128) do |data, sock| received << data sock.write "ack\n" end - bind_port = unused_port + bind_port = unused_port(protocol: :udp) 3.times do begin sock = UDPSocket.new(Socket::AF_INET) sock.bind("127.0.0.1", bind_port) - sock.connect("127.0.0.1", @port) + sock.connect("127.0.0.1", port) th = Thread.new do while true begin @@ -654,11 +668,12 @@ class Dummy < Fluent::Plugin::TestBase received = "" responses = [] - @d.server_create_udp(:s, @port, bind: "::1", max_bytes: 128) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, bind: "::1", max_bytes: 128) do |data, sock| received << data sock.write "ack\n" end - bind_port = unused_port + bind_port = unused_port(protocol: :udp) 3.times do begin sock = UDPSocket.new(Socket::AF_INET6) @@ -667,7 +682,7 @@ class Dummy < Fluent::Plugin::TestBase responses << sock.recv(16) true end - sock.connect("::1", @port) + sock.connect("::1", port) sock.write "yay\nfoo\n" th.join(5) ensure @@ -682,13 +697,14 @@ class Dummy < Fluent::Plugin::TestBase test 'does not resolve name of client address in default' do received = "" sources = [] - @d.server_create_udp(:s, @port, max_bytes: 128) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, max_bytes: 128) do |data, sock| received << data sources << sock.remote_host end 3.times do sock = UDPSocket.new(Socket::AF_INET) - sock.connect("127.0.0.1", @port) + sock.connect("127.0.0.1", port) sock.puts "yay" sock.close end @@ -702,13 +718,14 @@ class Dummy < Fluent::Plugin::TestBase received = "" sources = [] - @d.server_create_udp(:s, @port, resolve_name: true, max_bytes: 128) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, resolve_name: true, max_bytes: 128) do |data, sock| received << data sources << sock.remote_host end 3.times do sock = UDPSocket.new(Socket::AF_INET) - sock.connect("127.0.0.1", @port) + sock.connect("127.0.0.1", port) sock.puts "yay" sock.close end @@ -720,7 +737,8 @@ class Dummy < Fluent::Plugin::TestBase test 'raises error if plugin registers data callback for connection object from #server_create' do received = "" errors = [] - @d.server_create_udp(:s, @port, max_bytes: 128) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, max_bytes: 128) do |data, sock| received << data begin sock.data{|d| received << d.upcase } @@ -729,7 +747,7 @@ class Dummy < Fluent::Plugin::TestBase end end sock = UDPSocket.new(Socket::AF_INET) - sock.connect("127.0.0.1", @port) + sock.connect("127.0.0.1", port) sock.write "foo\n" sock.close @@ -742,7 +760,8 @@ class Dummy < Fluent::Plugin::TestBase test 'raise error if plugin registers write_complete callback for udp' do received = "" errors = [] - @d.server_create_udp(:s, @port, max_bytes: 128) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, max_bytes: 128) do |data, sock| received << data begin sock.on(:write_complete){|conn| "" } @@ -751,7 +770,7 @@ class Dummy < Fluent::Plugin::TestBase end end sock = UDPSocket.new(Socket::AF_INET) - sock.connect("127.0.0.1", @port) + sock.connect("127.0.0.1", port) sock.write "foo\n" sock.close @@ -764,7 +783,8 @@ class Dummy < Fluent::Plugin::TestBase test 'raises error if plugin registers close callback for udp' do received = "" errors = [] - @d.server_create_udp(:s, @port, max_bytes: 128) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:s, port, max_bytes: 128) do |data, sock| received << data begin sock.on(:close){|d| "" } @@ -773,7 +793,7 @@ class Dummy < Fluent::Plugin::TestBase end end sock = UDPSocket.new(Socket::AF_INET) - sock.connect("127.0.0.1", @port) + sock.connect("127.0.0.1", port) sock.write "foo\n" sock.close @@ -786,11 +806,12 @@ class Dummy < Fluent::Plugin::TestBase test 'can bind IPv4 / IPv6 together' do omit "IPv6 unavailable here" unless ipv6_enabled? + port = unused_port(protocol: :udp) assert_nothing_raised do - @d.server_create_udp(:s_ipv4_udp, @port, bind: '0.0.0.0', shared: false, max_bytes: 128) do |data, sock| + @d.server_create_udp(:s_ipv4_udp, port, bind: '0.0.0.0', shared: false, max_bytes: 128) do |data, sock| # ... end - @d.server_create_udp(:s_ipv6_udp, @port, bind: '::', shared: false, max_bytes: 128) do |data, sock| + @d.server_create_udp(:s_ipv6_udp, port, bind: '::', shared: false, max_bytes: 128) do |data, sock| # ... end end @@ -803,11 +824,12 @@ class Dummy < Fluent::Plugin::TestBase max_bytes, records, expected = data.values actual_records = [] - @d.server_create_udp(:myserver, @port, max_bytes: max_bytes) do |data, sock| + port = unused_port(protocol: :udp) + @d.server_create_udp(:myserver, port, max_bytes: max_bytes) do |data, sock| actual_records << data end - open_client(:udp, "127.0.0.1", @port) do |sock| + open_client(:udp, "127.0.0.1", port) do |sock| records.each do |record| sock.send(record, 0) end @@ -823,11 +845,12 @@ class Dummy < Fluent::Plugin::TestBase max_bytes, records, expected = data.values actual_records = [] - @d.server_create_udp(:myserver, @port, max_bytes: max_bytes) do |data| + port = unused_port(protocol: :udp) + @d.server_create_udp(:myserver, port, max_bytes: max_bytes) do |data| actual_records << data end - open_client(:udp, "127.0.0.1", @port) do |sock| + open_client(:udp, "127.0.0.1", port) do |sock| records.each do |record| sock.send(record, 0) end diff --git a/test/plugin_helper/test_socket.rb b/test/plugin_helper/test_socket.rb index cb2ff32de0..624169f08b 100644 --- a/test/plugin_helper/test_socket.rb +++ b/test/plugin_helper/test_socket.rb @@ -11,7 +11,7 @@ class SocketHelperTest < Test::Unit::TestCase CERT_CHAINS_DIR = File.expand_path(File.dirname(__FILE__) + '/data/cert/cert_chains') def setup - @port = unused_port + @port = unused_port(protocol: :tcp) end def teardown