Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit db3e25c

Browse files
committed
Test Improvement: Only attempt to bind to non-bound ports
Fixes logstash-plugins#84
1 parent 90d2782 commit db3e25c

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

spec/inputs/tcp_spec.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
#Cabin::Channel.get(LogStash).level = :debug
1616
describe LogStash::Inputs::Tcp do
1717

18+
def get_port
19+
begin
20+
# Start high to better avoid common services
21+
port = rand(10000..65535)
22+
s = TCPServer.new("127.0.0.1", port)
23+
s.close
24+
return port
25+
rescue Errno::EADDRINUSE
26+
retry
27+
end
28+
end
29+
30+
let(:port) { get_port }
31+
1832
context "codec (PR #1372)" do
1933
it "switches from plain to line" do
2034
require "logstash/codecs/plain"
@@ -36,7 +50,6 @@
3650

3751
it "should read plain with unicode" do
3852
event_count = 10
39-
port = rand(1024..65535)
4053
conf = <<-CONFIG
4154
input {
4255
tcp {
@@ -69,12 +82,11 @@
6982

7083
it "should handle PROXY protocol v1 connections" do
7184
event_count = 10
72-
port = rand(1024..65535)
7385
conf = <<-CONFIG
7486
input {
7587
tcp {
76-
port => #{port}
7788
proxy_protocol => true
89+
port => '#{port}'
7890
}
7991
}
8092
CONFIG
@@ -104,7 +116,6 @@
104116
end
105117

106118
it "should read events with plain codec and ISO-8859-1 charset" do
107-
port = rand(1024..65535)
108119
charset = "ISO-8859-1"
109120
conf = <<-CONFIG
110121
input {
@@ -132,7 +143,6 @@
132143
end
133144

134145
it "should read events with json codec" do
135-
port = rand(1024..65535)
136146
conf = <<-CONFIG
137147
input {
138148
tcp {
@@ -167,7 +177,6 @@
167177
end
168178

169179
it "should read events with json codec (testing 'host' handling)" do
170-
port = rand(1024..65535)
171180
conf = <<-CONFIG
172181
input {
173182
tcp {
@@ -194,7 +203,6 @@
194203
end
195204

196205
it "should read events with json_lines codec" do
197-
port = rand(1024..65535)
198206
conf = <<-CONFIG
199207
input {
200208
tcp {
@@ -233,7 +241,6 @@
233241

234242
it "should one message per connection" do
235243
event_count = 10
236-
port = rand(10000..65535)
237244
conf = <<-CONFIG
238245
input {
239246
tcp {
@@ -271,7 +278,6 @@
271278
srand(RSpec.configuration.seed)
272279
end
273280

274-
let(:port) { rand(1024..65535) }
275281
subject { LogStash::Plugin.lookup("input", "tcp").new({ "port" => port }) }
276282
let!(:helper) { TcpHelpers.new }
277283

0 commit comments

Comments
 (0)