Factor common code out of the test fake sources

* * *
More fake source refacoring
This commit is contained in:
Alex Young
2012-06-27 17:28:24 +01:00
parent 137a764cc7
commit 192471ee82
5 changed files with 66 additions and 108 deletions

View File

@@ -6,27 +6,19 @@
# It then connects again, to check that the destination is still # It then connects again, to check that the destination is still
# listening. # listening.
require 'flexnbd/fake_source'
include FlexNBD::FakeSource
addr, port = *ARGV addr, port = *ARGV
require 'socket'
require 'timeout'
begin
Timeout.timeout( 2 ) do
sock = TCPSocket.open( addr, port.to_i )
sock.close
end
rescue Timeout::Error
$stderr.puts "Failed to connect"
exit 1
end
Timeout.timeout( 3 ) do connect( addr, port, "Failed to connect" ).close
# Sleep to be sure we don't try to connect too soon. That wouldn't # Sleep to be sure we don't try to connect too soon. That wouldn't
# be a problem for the destination, but it would prevent us from # be a problem for the destination, but it would prevent us from
# determining success or failure here. # determining success or failure here in the case where we try to
sleep 0.5 # reconnect before the destination has tidied up after the first
sock = TCPSocket.open( addr, port.to_i ) # thread went away.
sock.close sleep(0.5)
end connect( addr, port, "Failed to reconnect" ).close
exit 0 exit 0

View File

@@ -7,43 +7,16 @@
# After the disconnect, we reconnect to be sure that the destination # After the disconnect, we reconnect to be sure that the destination
# is still alive. # is still alive.
require 'flexnbd/fake_source'
require 'socket' include FlexNBD::FakeSource
require "timeout"
require 'flexnbd/constants'
addr, port = *ARGV addr, port = *ARGV
client_sock = nil
begin
Timeout.timeout(2) do
client_sock = TCPSocket.open( addr, port )
end
rescue Timeout::Error
$stderr.puts "Timed out connecting."
exit 1
end
begin
Timeout.timeout( FlexNBD::MS_HELLO_TIME_SECS ) do
fail "No hello." unless (hello = client_sock.read( 152 )) &&
hello.length==152
end
rescue Timeout::Error
$stderr.puts "Timed out waiting for hello."
exit 1
end
client_sock = connect( addr, port, "Timed out connecting." )
read_hello( client_sock )
client_sock.close client_sock.close
sleep(0.2) sleep(0.2)
begin connect( addr, port, "Timed out reconnecting." )
Timeout.timeout(2) do
client_sock = TCPSocket.open( addr, port )
end
rescue Timeout::Error
$stderr.puts "Timed out reconnecting."
exit 1
end
exit(0) exit(0)

View File

@@ -1,34 +1,17 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# Connect to the destination, then hang. Connect a second time to the # Connect to the destination, then hang. Connect a second time to the
# destinatioin. This will trigger the destination's thread clearer. # destination. This will trigger the destination's thread clearer.
require 'socket' require 'flexnbd/fake_source'
require 'timeout' include FlexNBD::FakeSource
addr, port = *ARGV addr, port = *ARGV
# client_sock1 is a connection the destination is expecting. # client_sock1 is a connection the destination is expecting.
client_sock1 = nil client_sock1 = connect( addr, port, "Timed out connecting" )
begin sleep(0.25)
Timeout.timeout(2) do client_sock2 = connect( addr, port, "Timed out connecting a second time" )
client_sock1 = TCPSocket.open( addr, port )
end
rescue Timeout::Error
$stderr.puts "Timed out connecting."
exit 1
end
#client_sock2 is the interloper.
client_sock2 = nil
begin
Timeout.timeout(2) do
client_sock2 = TCPSocket.open( addr, port )
end
rescue Timeout::Error
$stderr.puts "Timed out connecting a second time."
exit 1
end
# This is the expected source crashing after connect # This is the expected source crashing after connect
client_sock1.close client_sock1.close

View File

@@ -7,30 +7,11 @@
# listening for an incoming migration. # listening for an incoming migration.
addr, port = *ARGV addr, port = *ARGV
require 'socket' require "flexnbd/fake_source"
require 'timeout' include FlexNBD::FakeSource
require "flexnbd/constants" client_sock = connect( addr, port, "Timed out connecting" )
read_hello( client_sock )
client_sock=nil
begin
Timeout.timeout(2) do
client_sock = TCPSocket.open( addr, port )
end
rescue Timeout::Error
$stderr.puts "Timed out connecting"
exit 1
end
begin
Timeout.timeout(FlexNBD::MS_HELLO_TIME_SECS) do
client_sock.read(152)
end
rescue Timeout::Error
$stderr.puts "Timed out reading hello"
exit 1
end
# Now we do two things: # Now we do two things:
@@ -44,19 +25,8 @@ kidpid = fork do
client_sock.close client_sock.close
new_sock = nil new_sock = nil
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 ) sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 )
begin new_sock = connect( addr, port, "Timed out reconnecting." )
Timeout.timeout( 2 ) do read_hello( new_sock )
new_sock = TCPSocket.open( addr, port )
end
Timeout.timeout( FlexNBD::MS_HELLO_TIME_SECS ) do
fail "No hello." unless (hello = new_sock.read( 152 )) &&
hello.length==152
end
new_sock.close
rescue Timeout::Error
$stderr.puts "Timed out reconnecting"
exit 1
end
exit 0 exit 0
end end

View File

@@ -0,0 +1,40 @@
# encoding: utf-8
require 'socket'
require "timeout"
require 'flexnbd/constants'
module FlexNBD
module FakeSource
def connect( addr, port, err_msg )
timing_out( 2, err_msg ) do
TCPSocket.open( addr, port )
end
end
def read_hello( client_sock )
timing_out( FlexNBD::MS_HELLO_TIME_SECS,
"Timed out waiting for hello." ) do
fail "No hello." unless (hello = client_sock.read( 152 )) &&
hello.length==152
hello
end
end
def timing_out( time, msg )
begin
Timeout.timeout( time ) do
yield
end
rescue Timeout::Error
$stderr.puts msg
exit 1
end
end
end # module FakeSource
end # module FlexNBD