Factor common code out of the test fake sources
* * * More fake source refacoring
This commit is contained in:
@@ -6,27 +6,19 @@
|
||||
# It then connects again, to check that the destination is still
|
||||
# listening.
|
||||
|
||||
require 'flexnbd/fake_source'
|
||||
include FlexNBD::FakeSource
|
||||
|
||||
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
|
||||
# be a problem for the destination, but it would prevent us from
|
||||
# determining success or failure here.
|
||||
sleep 0.5
|
||||
sock = TCPSocket.open( addr, port.to_i )
|
||||
sock.close
|
||||
end
|
||||
# determining success or failure here in the case where we try to
|
||||
# reconnect before the destination has tidied up after the first
|
||||
# thread went away.
|
||||
sleep(0.5)
|
||||
connect( addr, port, "Failed to reconnect" ).close
|
||||
|
||||
exit 0
|
||||
|
@@ -7,43 +7,16 @@
|
||||
# After the disconnect, we reconnect to be sure that the destination
|
||||
# is still alive.
|
||||
|
||||
|
||||
require 'socket'
|
||||
require "timeout"
|
||||
require 'flexnbd/constants'
|
||||
require 'flexnbd/fake_source'
|
||||
include FlexNBD::FakeSource
|
||||
|
||||
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
|
||||
|
||||
sleep(0.2)
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
client_sock = TCPSocket.open( addr, port )
|
||||
end
|
||||
rescue Timeout::Error
|
||||
$stderr.puts "Timed out reconnecting."
|
||||
exit 1
|
||||
end
|
||||
connect( addr, port, "Timed out reconnecting." )
|
||||
|
||||
exit(0)
|
||||
|
@@ -1,34 +1,17 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# 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 'timeout'
|
||||
require 'flexnbd/fake_source'
|
||||
include FlexNBD::FakeSource
|
||||
|
||||
addr, port = *ARGV
|
||||
|
||||
# client_sock1 is a connection the destination is expecting.
|
||||
client_sock1 = nil
|
||||
begin
|
||||
Timeout.timeout(2) do
|
||||
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
|
||||
client_sock1 = connect( addr, port, "Timed out connecting" )
|
||||
sleep(0.25)
|
||||
client_sock2 = connect( addr, port, "Timed out connecting a second time" )
|
||||
|
||||
# This is the expected source crashing after connect
|
||||
client_sock1.close
|
||||
|
@@ -7,30 +7,11 @@
|
||||
# listening for an incoming migration.
|
||||
|
||||
addr, port = *ARGV
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
require "flexnbd/fake_source"
|
||||
include FlexNBD::FakeSource
|
||||
|
||||
require "flexnbd/constants"
|
||||
|
||||
|
||||
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
|
||||
client_sock = connect( addr, port, "Timed out connecting" )
|
||||
read_hello( client_sock )
|
||||
|
||||
# Now we do two things:
|
||||
|
||||
@@ -44,19 +25,8 @@ kidpid = fork do
|
||||
client_sock.close
|
||||
new_sock = nil
|
||||
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 )
|
||||
begin
|
||||
Timeout.timeout( 2 ) do
|
||||
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
|
||||
new_sock = connect( addr, port, "Timed out reconnecting." )
|
||||
read_hello( new_sock )
|
||||
exit 0
|
||||
end
|
||||
|
||||
|
40
tests/flexnbd/fake_source.rb
Normal file
40
tests/flexnbd/fake_source.rb
Normal 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
|
Reference in New Issue
Block a user