Skip to content

Commit d1a58fd

Browse files
authored
Merge pull request #4675 from Watson1978/test-bind
tests: fix unused_port
2 parents a2b935a + 0634bd5 commit d1a58fd

14 files changed

+134
-82
lines changed

test/command/test_cat.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def setup
1818
@primary = create_primary
1919
metadata = @primary.buffer.new_metadata
2020
@chunk = create_chunk(@primary, metadata, @es)
21-
@port = unused_port
21+
@port = unused_port(protocol: :tcp)
2222
end
2323

2424
def teardown

test/command/test_fluentd.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ def multi_workers_ready?; true; end
11751175
end
11761176
end
11771177

1178-
sub_test_case 'sahred socket options' do
1178+
sub_test_case 'shared socket options' do
11791179
test 'enable shared socket by default' do
11801180
conf = ""
11811181
conf_path = create_conf_file('empty.conf', conf)

test/helper.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,31 @@ class Test::Unit::AssertionFailedError < StandardError
7171

7272
include Fluent::Test::Helpers
7373

74-
def unused_port(num = 1, protocol: :tcp, bind: "0.0.0.0")
74+
def unused_port(num = 1, protocol:, bind: "0.0.0.0")
7575
case protocol
76-
when :tcp
76+
when :tcp, :tls
7777
unused_port_tcp(num)
7878
when :udp
7979
unused_port_udp(num, bind: bind)
80+
when :all
81+
unused_port_tcp_udp(num)
8082
else
8183
raise ArgumentError, "unknown protocol: #{protocol}"
8284
end
8385
end
8486

87+
def unused_port_tcp_udp(num = 1)
88+
raise "not support num > 1" if num > 1
89+
90+
# The default maximum number of file descriptors in macOS is 256.
91+
# It might need to set num to a smaller value than that.
92+
tcp_ports = unused_port_tcp(200)
93+
port = unused_port_udp(1, port_list: tcp_ports)
94+
raise "can't find unused port" unless port
95+
96+
port
97+
end
98+
8599
def unused_port_tcp(num = 1)
86100
ports = []
87101
sockets = []
@@ -90,7 +104,7 @@ def unused_port_tcp(num = 1)
90104
sockets << s
91105
ports << s.addr[1]
92106
end
93-
sockets.each{|s| s.close }
107+
sockets.each(&:close)
94108
if num == 1
95109
return ports.first
96110
else
@@ -100,21 +114,27 @@ def unused_port_tcp(num = 1)
100114

101115
PORT_RANGE_AVAILABLE = (1024...65535)
102116

103-
def unused_port_udp(num = 1, bind: "0.0.0.0")
117+
def unused_port_udp(num = 1, port_list: [], bind: "0.0.0.0")
104118
family = IPAddr.new(IPSocket.getaddress(bind)).ipv4? ? ::Socket::AF_INET : ::Socket::AF_INET6
105119
ports = []
106120
sockets = []
107-
while ports.size < num
108-
port = rand(PORT_RANGE_AVAILABLE)
121+
122+
use_random_port = port_list.empty?
123+
i = 0
124+
loop do
125+
port = use_random_port ? rand(PORT_RANGE_AVAILABLE) : port_list[i]
109126
u = UDPSocket.new(family)
110127
if (u.bind(bind, port) rescue nil)
111128
ports << port
112129
sockets << u
113130
else
114131
u.close
115132
end
133+
i += 1
134+
break if ports.size >= num
135+
break if !use_random_port && i >= port_list.size
116136
end
117-
sockets.each{|s| s.close }
137+
sockets.each(&:close)
118138
if num == 1
119139
return ports.first
120140
else

test/plugin/test_in_forward.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def setup
1818
Fluent::Test.setup
1919
@responses = [] # for testing responses after sending data
2020
@d = nil
21-
@port = unused_port
21+
# forward plugin uses TCP and UDP sockets on the same port number
22+
@port = unused_port(protocol: :all)
2223
end
2324

2425
def teardown

test/plugin/test_in_http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def shutdown
1818

1919
def setup
2020
Fluent::Test.setup
21-
@port = unused_port
21+
@port = unused_port(protocol: :tcp)
2222
end
2323

2424
def teardown

test/plugin/test_in_monitor_agent.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def test_enable_input_metrics(with_config)
392392
end
393393

394394
test "emit" do
395-
port = unused_port
395+
port = unused_port(protocol: :tcp)
396396
d = create_driver("
397397
@type monitor_agent
398398
bind '127.0.0.1'
@@ -451,7 +451,7 @@ def get(uri, header = {})
451451

452452
sub_test_case "servlets" do
453453
setup do
454-
@port = unused_port
454+
@port = unused_port(protocol: :tcp)
455455
# check @type and type in one configuration
456456
conf = <<-EOC
457457
<source>
@@ -759,7 +759,7 @@ def write(chunk)
759759
end
760760

761761
setup do
762-
@port = unused_port
762+
@port = unused_port(protocol: :tcp)
763763
# check @type and type in one configuration
764764
conf = <<-EOC
765765
<source>
@@ -840,7 +840,7 @@ def write(chunk)
840840

841841
sub_test_case "check the port number of http server" do
842842
test "on single worker environment" do
843-
port = unused_port
843+
port = unused_port(protocol: :tcp)
844844
d = create_driver("
845845
@type monitor_agent
846846
bind '127.0.0.1'
@@ -851,7 +851,7 @@ def write(chunk)
851851
end
852852

853853
test "worker_id = 2 on multi worker environment" do
854-
port = unused_port
854+
port = unused_port(protocol: :tcp)
855855
Fluent::SystemConfig.overwrite_system_config('workers' => 4) do
856856
d = Fluent::Test::Driver::Input.new(Fluent::Plugin::MonitorAgentInput)
857857
d.instance.instance_eval{ @_fluentd_worker_id = 2 }
@@ -905,7 +905,7 @@ def filter(tag, time, record)
905905
end
906906

907907
test "plugins have a variable named buffer does not throws NoMethodError" do
908-
port = unused_port
908+
port = unused_port(protocol: :tcp)
909909
d = create_driver("
910910
@type monitor_agent
911911
bind '127.0.0.1'

test/plugin/test_in_syslog.rb

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
class SyslogInputTest < Test::Unit::TestCase
66
def setup
77
Fluent::Test.setup
8-
@port = unused_port
8+
@port = unused_port(protocol: :udp)
99
end
1010

1111
def teardown
1212
@port = nil
1313
end
1414

15-
def ipv4_config
15+
def ipv4_config(port = @port)
1616
%[
17-
port #{@port}
17+
port #{port}
1818
bind 127.0.0.1
1919
tag syslog
2020
]
2121
end
2222

23-
def ipv6_config
23+
def ipv6_config(port = @port)
2424
%[
25-
port #{@port}
25+
port #{port}
2626
bind ::1
2727
tag syslog
2828
]
@@ -69,7 +69,8 @@ def test_configure_resolve_hostname(param)
6969
'Use transport and protocol' => ["protocol_type udp\n<transport tcp>\n </transport>", :udp, :tcp])
7070
def test_configure_protocol(param)
7171
conf, proto_type, transport_proto_type = *param
72-
d = create_driver([ipv4_config, conf].join("\n"))
72+
port = unused_port(protocol: proto_type ? proto_type : transport_proto_type)
73+
d = create_driver([ipv4_config(port), conf].join("\n"))
7374

7475
assert_equal(d.instance.protocol_type, proto_type)
7576
assert_equal(d.instance.transport_config.protocol, transport_proto_type)
@@ -158,12 +159,13 @@ def test_msg_size_udp_for_large_msg
158159
end
159160

160161
def test_msg_size_with_tcp
161-
d = create_driver([ipv4_config, "<transport tcp> \n</transport>"].join("\n"))
162+
port = unused_port(protocol: :tcp)
163+
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>"].join("\n"))
162164
tests = create_test_case
163165

164166
d.run(expect_emits: 2) do
165167
tests.each {|test|
166-
TCPSocket.open('127.0.0.1', @port) do |s|
168+
TCPSocket.open('127.0.0.1', port) do |s|
167169
s.send(test['msg'], 0)
168170
end
169171
}
@@ -189,11 +191,12 @@ def test_emit_rfc5452
189191
end
190192

191193
def test_msg_size_with_same_tcp_connection
192-
d = create_driver([ipv4_config, "<transport tcp> \n</transport>"].join("\n"))
194+
port = unused_port(protocol: :tcp)
195+
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>"].join("\n"))
193196
tests = create_test_case
194197

195198
d.run(expect_emits: 2) do
196-
TCPSocket.open('127.0.0.1', @port) do |s|
199+
TCPSocket.open('127.0.0.1', port) do |s|
197200
tests.each {|test|
198201
s.send(test['msg'], 0)
199202
}
@@ -347,12 +350,13 @@ def compare_test_result(events, tests, options = {})
347350

348351
sub_test_case 'octet counting frame' do
349352
def test_msg_size_with_tcp
350-
d = create_driver([ipv4_config, "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
353+
port = unused_port(protocol: :tcp)
354+
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
351355
tests = create_test_case
352356

353357
d.run(expect_emits: 2) do
354358
tests.each {|test|
355-
TCPSocket.open('127.0.0.1', @port) do |s|
359+
TCPSocket.open('127.0.0.1', port) do |s|
356360
s.send(test['msg'], 0)
357361
end
358362
}
@@ -363,11 +367,12 @@ def test_msg_size_with_tcp
363367
end
364368

365369
def test_msg_size_with_same_tcp_connection
366-
d = create_driver([ipv4_config, "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
370+
port = unused_port(protocol: :tcp)
371+
d = create_driver([ipv4_config(port), "<transport tcp> \n</transport>", 'frame_type octet_count'].join("\n"))
367372
tests = create_test_case
368373

369374
d.run(expect_emits: 2) do
370-
TCPSocket.open('127.0.0.1', @port) do |s|
375+
TCPSocket.open('127.0.0.1', port) do |s|
371376
tests.each {|test|
372377
s.send(test['msg'], 0)
373378
}
@@ -469,7 +474,8 @@ def test_emit_unmatched_lines_with_address
469474
end
470475

471476
def test_send_keepalive_packet_is_disabled_by_default
472-
d = create_driver(ipv4_config + %[
477+
port = unused_port(protocol: :tcp)
478+
d = create_driver(ipv4_config(port) + %[
473479
<transport tcp>
474480
</transport>
475481
protocol tcp
@@ -479,19 +485,20 @@ def test_send_keepalive_packet_is_disabled_by_default
479485

480486
def test_send_keepalive_packet_can_be_enabled
481487
addr = "127.0.0.1"
482-
d = create_driver(ipv4_config + %[
488+
port = unused_port(protocol: :tcp)
489+
d = create_driver(ipv4_config(port) + %[
483490
<transport tcp>
484491
</transport>
485492
send_keepalive_packet true
486493
])
487494
assert_true d.instance.send_keepalive_packet
488495
mock.proxy(d.instance).server_create_connection(
489-
:in_syslog_tcp_server, @port,
496+
:in_syslog_tcp_server, port,
490497
bind: addr,
491498
resolve_name: nil,
492499
send_keepalive_packet: true)
493500
d.run do
494-
TCPSocket.open(addr, @port)
501+
TCPSocket.open(addr, port)
495502
end
496503
end
497504

test/plugin/test_in_tcp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class TcpInputTest < Test::Unit::TestCase
66
def setup
77
Fluent::Test.setup
8-
@port = unused_port
8+
@port = unused_port(protocol: :tcp)
99
end
1010

1111
def teardown

test/plugin/test_in_udp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class UdpInputTest < Test::Unit::TestCase
66
def setup
77
Fluent::Test.setup
8-
@port = unused_port
8+
@port = unused_port(protocol: :udp)
99
end
1010

1111
def teardown

test/plugin/test_out_forward.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def setup
1212
FileUtils.rm_rf(TMP_DIR)
1313
FileUtils.mkdir_p(TMP_DIR)
1414
@d = nil
15-
@target_port = unused_port
15+
# forward plugin uses TCP and UDP sockets on the same port number
16+
@target_port = unused_port(protocol: :all)
1617
end
1718

1819
def teardown

0 commit comments

Comments
 (0)