Refactor FakeSource from a module to a class
This commit is contained in:
@@ -7,18 +7,17 @@
|
|||||||
# listening.
|
# listening.
|
||||||
|
|
||||||
require 'flexnbd/fake_source'
|
require 'flexnbd/fake_source'
|
||||||
include FlexNBD::FakeSource
|
include FlexNBD
|
||||||
|
|
||||||
addr, port = *ARGV
|
addr, port = *ARGV
|
||||||
|
|
||||||
|
FakeSource.new( addr, port, "Failed to connect" ).close
|
||||||
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 in the case where we try to
|
# determining success or failure here in the case where we try to
|
||||||
# reconnect before the destination has tidied up after the first
|
# reconnect before the destination has tidied up after the first
|
||||||
# thread went away.
|
# thread went away.
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
connect( addr, port, "Failed to reconnect" ).close
|
FakeSource.new( addr, port, "Failed to reconnect" ).close
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
@@ -8,15 +8,17 @@
|
|||||||
# is still alive.
|
# is still alive.
|
||||||
|
|
||||||
require 'flexnbd/fake_source'
|
require 'flexnbd/fake_source'
|
||||||
include FlexNBD::FakeSource
|
include FlexNBD
|
||||||
|
|
||||||
addr, port = *ARGV
|
addr, port = *ARGV
|
||||||
|
|
||||||
|
|
||||||
client_sock = connect( addr, port, "Timed out connecting." )
|
client = FakeSource.new( addr, port, "Timed out connecting." )
|
||||||
read_hello( client_sock )
|
client.read_hello
|
||||||
client_sock.close
|
client.close
|
||||||
|
|
||||||
sleep(0.2)
|
sleep(0.2)
|
||||||
connect( addr, port, "Timed out reconnecting." )
|
|
||||||
|
FakeSource.new( addr, port, "Timed out reconnecting." )
|
||||||
|
|
||||||
exit(0)
|
exit(0)
|
||||||
|
@@ -1,19 +1,22 @@
|
|||||||
#!/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
|
||||||
# destination. This will trigger the destination's thread clearer.
|
# destination. This will trigger the destination's thread clearer. We
|
||||||
|
# can't really see any error state from here, we just try to trigger
|
||||||
|
# something the test runner can detect.
|
||||||
|
|
||||||
require 'flexnbd/fake_source'
|
require 'flexnbd/fake_source'
|
||||||
include FlexNBD::FakeSource
|
include FlexNBD
|
||||||
|
|
||||||
addr, port = *ARGV
|
addr, port = *ARGV
|
||||||
|
|
||||||
# client_sock1 is a connection the destination is expecting.
|
client1 = FakeSource.new( addr, port, "Timed out connecting" )
|
||||||
client_sock1 = connect( addr, port, "Timed out connecting" )
|
|
||||||
sleep(0.25)
|
sleep(0.25)
|
||||||
client_sock2 = connect( addr, port, "Timed out connecting a second time" )
|
client2 = FakeSource.new( addr, port, "Timed out connecting a second time" )
|
||||||
|
|
||||||
# This is the expected source crashing after connect
|
# This is the expected source crashing after connect
|
||||||
client_sock1.close
|
client1.close
|
||||||
# And this is just a tidy-up.
|
# And this is just a tidy-up.
|
||||||
client_sock2.close
|
client2.close
|
||||||
|
|
||||||
|
exit(0)
|
||||||
|
@@ -7,16 +7,15 @@
|
|||||||
|
|
||||||
require 'timeout'
|
require 'timeout'
|
||||||
require 'flexnbd/fake_source'
|
require 'flexnbd/fake_source'
|
||||||
include FlexNBD::FakeSource
|
include FlexNBD
|
||||||
|
|
||||||
addr, port = *ARGV
|
addr, port = *ARGV
|
||||||
sock = connect( addr, port, "Timed out connecting", "127.0.0.6" )
|
|
||||||
sleep( 0.25 )
|
|
||||||
Timeout.timeout( 2 ) do
|
|
||||||
fail "Not disconnected" if sock.read(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
sock.close
|
client = FakeSource.new( addr, port, "Timed out connecting", "127.0.0.6" )
|
||||||
|
sleep( 0.25 )
|
||||||
|
client.ensure_disconnected
|
||||||
|
|
||||||
|
client.close
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
@@ -8,10 +8,10 @@
|
|||||||
|
|
||||||
addr, port = *ARGV
|
addr, port = *ARGV
|
||||||
require "flexnbd/fake_source"
|
require "flexnbd/fake_source"
|
||||||
include FlexNBD::FakeSource
|
include FlexNBD
|
||||||
|
|
||||||
client_sock = connect( addr, port, "Timed out connecting" )
|
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||||
read_hello( client_sock )
|
client.read_hello
|
||||||
|
|
||||||
# Now we do two things:
|
# Now we do two things:
|
||||||
|
|
||||||
@@ -22,18 +22,18 @@ read_hello( client_sock )
|
|||||||
# closed its end yet.
|
# closed its end yet.
|
||||||
|
|
||||||
kidpid = fork do
|
kidpid = fork do
|
||||||
client_sock.close
|
client.close
|
||||||
new_sock = nil
|
new_client = nil
|
||||||
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 )
|
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 )
|
||||||
new_sock = connect( addr, port, "Timed out reconnecting." )
|
new_client = FakeSource.new( addr, port, "Timed out reconnecting." )
|
||||||
read_hello( new_sock )
|
new_client.read_hello
|
||||||
exit 0
|
exit 0
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sleep for longer than the child, to give the flexnbd process a bit
|
# Sleep for longer than the child, to give the flexnbd process a bit
|
||||||
# of slop
|
# of slop
|
||||||
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 3 )
|
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 3 )
|
||||||
client_sock.close
|
client.close
|
||||||
|
|
||||||
_,status = Process.waitpid2( kidpid )
|
_,status = Process.waitpid2( kidpid )
|
||||||
exit status.exitstatus
|
exit status.exitstatus
|
||||||
|
@@ -6,16 +6,17 @@
|
|||||||
# disconnected.
|
# disconnected.
|
||||||
|
|
||||||
require 'flexnbd/fake_source'
|
require 'flexnbd/fake_source'
|
||||||
include FlexNBD::FakeSource
|
include FlexNBD
|
||||||
|
|
||||||
addr, port = *ARGV
|
addr, port = *ARGV
|
||||||
client_sock = connect( addr, port, "Timed out connecting" )
|
|
||||||
read_hello( client_sock )
|
client = FakeSource.new( addr, port, "Timed out connecting" )
|
||||||
write_write_request( client_sock, 1 << 31, 1 << 31, "myhandle" )
|
client.read_hello
|
||||||
response = read_response( client_sock )
|
client.write_write_request( 1 << 31, 1 << 31, "myhandle" )
|
||||||
|
response = client.read_response
|
||||||
|
|
||||||
fail "Not an error" if response[:error] == 0
|
fail "Not an error" if response[:error] == 0
|
||||||
fail "Wrong handle" unless "myhandle" == response[:handle]
|
fail "Wrong handle" unless "myhandle" == response[:handle]
|
||||||
|
|
||||||
client_sock.close
|
client.close
|
||||||
exit(0)
|
exit(0)
|
||||||
|
@@ -5,41 +5,46 @@ require "timeout"
|
|||||||
require 'flexnbd/constants'
|
require 'flexnbd/constants'
|
||||||
|
|
||||||
module FlexNBD
|
module FlexNBD
|
||||||
module FakeSource
|
class FakeSource
|
||||||
|
|
||||||
def connect( addr, port, err_msg, source_addr=nil, source_port=0 )
|
def initialize( addr, port, err_msg, source_addr=nil, source_port=0 )
|
||||||
timing_out( 2, err_msg ) do
|
timing_out( 2, err_msg ) do
|
||||||
TCPSocket.new( addr, port, source_addr, source_port )
|
@sock = TCPSocket.new( addr, port, source_addr, source_port )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def read_hello( client_sock )
|
def close
|
||||||
|
@sock.close
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def read_hello()
|
||||||
timing_out( FlexNBD::MS_HELLO_TIME_SECS,
|
timing_out( FlexNBD::MS_HELLO_TIME_SECS,
|
||||||
"Timed out waiting for hello." ) do
|
"Timed out waiting for hello." ) do
|
||||||
fail "No hello." unless (hello = client_sock.read( 152 )) &&
|
fail "No hello." unless (hello = @sock.read( 152 )) &&
|
||||||
hello.length==152
|
hello.length==152
|
||||||
hello
|
hello
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def write_write_request( client_sock, from, len, handle )
|
def write_write_request( from, len, handle )
|
||||||
fail "Bad handle" unless handle.length == 8
|
fail "Bad handle" unless handle.length == 8
|
||||||
|
|
||||||
client_sock.write( "\x25\x60\x95\x13" )
|
@sock.write( "\x25\x60\x95\x13" )
|
||||||
client_sock.write( "\x00\x00\x00\x01" )
|
@sock.write( "\x00\x00\x00\x01" )
|
||||||
client_sock.write( handle )
|
@sock.write( handle )
|
||||||
client_sock.write( "\x0"*4 )
|
@sock.write( "\x0"*4 )
|
||||||
client_sock.write( [from].pack( 'N' ) )
|
@sock.write( [from].pack( 'N' ) )
|
||||||
client_sock.write( [len ].pack( 'N' ) )
|
@sock.write( [len ].pack( 'N' ) )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def read_response( client_sock )
|
def read_response
|
||||||
magic = client_sock.read(4)
|
magic = @sock.read(4)
|
||||||
error_s = client_sock.read(4)
|
error_s = @sock.read(4)
|
||||||
handle = client_sock.read(8)
|
handle = @sock.read(8)
|
||||||
|
|
||||||
{
|
{
|
||||||
:magic => magic,
|
:magic => magic,
|
||||||
@@ -49,6 +54,13 @@ module FlexNBD
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_disconnected
|
||||||
|
Timeout.timeout( 2 ) do
|
||||||
|
@sock.read(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def timing_out( time, msg )
|
def timing_out( time, msg )
|
||||||
begin
|
begin
|
||||||
Timeout.timeout( time ) do
|
Timeout.timeout( time ) do
|
||||||
@@ -61,5 +73,5 @@ module FlexNBD
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end # module FakeSource
|
end # class FakeSource
|
||||||
end # module FlexNBD
|
end # module FlexNBD
|
||||||
|
Reference in New Issue
Block a user