You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2017/10/30 23:41:53 UTC
qpid-proton git commit: PROTON-1658: [ruby] simpler,
more portable port allocation for tests
Repository: qpid-proton
Updated Branches:
refs/heads/master 91195b580 -> 738c1980e
PROTON-1658: [ruby] simpler, more portable port allocation for tests
The SO_REUSEADDR trick that works on Linux does not work on OSX or Windows.
Use a simpler approach of listen(0), close socket, do real listen. This is
potentially race-prone but works well in practice
The correct solution is to allow the container to listen using a pre-existing socket,
this is coming with PROTON-1064.
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/738c1980
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/738c1980
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/738c1980
Branch: refs/heads/master
Commit: 738c1980eff65051163e53ba2f1a64a0ce2672dd
Parents: 91195b5
Author: Alan Conway <ac...@redhat.com>
Authored: Mon Oct 30 19:27:18 2017 -0400
Committer: Alan Conway <ac...@redhat.com>
Committed: Mon Oct 30 19:27:18 2017 -0400
----------------------------------------------------------------------
examples/ruby/example_test.rb | 18 ++++++++-----
proton-c/bindings/ruby/tests/test_tools.rb | 35 ++++---------------------
2 files changed, 17 insertions(+), 36 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/738c1980/examples/ruby/example_test.rb
----------------------------------------------------------------------
diff --git a/examples/ruby/example_test.rb b/examples/ruby/example_test.rb
index e14aef7..86890ce 100755
--- a/examples/ruby/example_test.rb
+++ b/examples/ruby/example_test.rb
@@ -18,9 +18,9 @@
# under the License.
#
+require 'minitest/autorun'
require 'qpid_proton'
require 'socket'
-require 'test_tools'
class ExampleTest < MiniTest::Test
@@ -66,11 +66,17 @@ EOS
end
end
-# Start the broker before all tests
-TestPort.new do |tp|
- $port = tp.port
- $broker = spawn("#{RbConfig.ruby} reactor/broker.rb -a :#{$port}")
- wait_port($port)
+# Start the broker before all tests.
+$port = TCPServer.open(0) do |s| s.addr[1]; end # find an unused port
+$broker = spawn("#{RbConfig.ruby} reactor/broker.rb -a :#{$port}")
+
+# Wait for the broker to be listening
+deadline = Time.now + 5
+begin
+ TCPSocket.open("", $port).close
+rescue Errno::ECONNREFUSED
+ retry if Time.now < deadline
+ raise
end
# Kill the broker after all tests
http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/738c1980/proton-c/bindings/ruby/tests/test_tools.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/tests/test_tools.rb b/proton-c/bindings/ruby/tests/test_tools.rb
index e9045ee..22a9040 100644
--- a/proton-c/bindings/ruby/tests/test_tools.rb
+++ b/proton-c/bindings/ruby/tests/test_tools.rb
@@ -27,31 +27,6 @@ require 'socket'
Container = Qpid::Proton::Reactor::Container
MessagingHandler = Qpid::Proton::Handler::MessagingHandler
-# Bind an unused local port using bind(0) and SO_REUSEADDR and hold it till close()
-# Provides #host, #port and #addr ("host:port") as strings
-class TestPort
- attr_reader :host, :port, :addr
-
- # With block, execute block passing self then close
- # Note host must be the local host, but you can pass '::1' instead for ipv6
- def initialize(host='127.0.0.1')
- @sock = Socket.new(:INET, :STREAM)
- @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
- @sock.bind(Socket.sockaddr_in(0, host))
- @host, @port = @sock.connect_address.ip_unpack
- @addr = "#{@host}:#{@port}"
- if block_given?
- begin
- yield self
- ensure
- close
- end
- end
- end
-
- def close() @sock.close(); end
-end
-
class TestError < Exception; end
def wait_port(port, timeout=5)
@@ -134,16 +109,16 @@ class TestHandler < MessagingHandler
end
end
-# A TestHandler that listens on a TestPort
+# A TestHandler that listens on a random port
class TestServer < TestHandler
def initialize
super
- @tp = TestPort.new
+ @port = TCPServer.open(0) do |s| s.addr[1]; end # find an unused port
end
- def host() @tp.host; end
- def port() @tp.port; end
- def addr() @tp.addr; end
+ def host() ""; end
+ def port() @port; end
+ def addr() "#{host}:#{port}"; end
def on_start(e)
super
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org