Refactor FakeSource from a module to a class

This commit is contained in:
Alex Young
2012-07-03 14:39:05 +01:00
parent ded4914c84
commit 64ebbe7688
7 changed files with 70 additions and 54 deletions

View File

@@ -7,18 +7,17 @@
# listening.
require 'flexnbd/fake_source'
include FlexNBD::FakeSource
include FlexNBD
addr, port = *ARGV
connect( addr, port, "Failed to connect" ).close
FakeSource.new( 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 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
FakeSource.new( addr, port, "Failed to reconnect" ).close
exit 0

View File

@@ -8,15 +8,17 @@
# is still alive.
require 'flexnbd/fake_source'
include FlexNBD::FakeSource
include FlexNBD
addr, port = *ARGV
client_sock = connect( addr, port, "Timed out connecting." )
read_hello( client_sock )
client_sock.close
client = FakeSource.new( addr, port, "Timed out connecting." )
client.read_hello
client.close
sleep(0.2)
connect( addr, port, "Timed out reconnecting." )
FakeSource.new( addr, port, "Timed out reconnecting." )
exit(0)

View File

@@ -1,19 +1,22 @@
#!/usr/bin/env ruby
# 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'
include FlexNBD::FakeSource
include FlexNBD
addr, port = *ARGV
# client_sock1 is a connection the destination is expecting.
client_sock1 = connect( addr, port, "Timed out connecting" )
client1 = FakeSource.new( addr, port, "Timed out connecting" )
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
client_sock1.close
client1.close
# And this is just a tidy-up.
client_sock2.close
client2.close
exit(0)

View File

@@ -7,16 +7,15 @@
require 'timeout'
require 'flexnbd/fake_source'
include FlexNBD::FakeSource
include FlexNBD
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)

View File

@@ -8,10 +8,10 @@
addr, port = *ARGV
require "flexnbd/fake_source"
include FlexNBD::FakeSource
include FlexNBD
client_sock = connect( addr, port, "Timed out connecting" )
read_hello( client_sock )
client = FakeSource.new( addr, port, "Timed out connecting" )
client.read_hello
# Now we do two things:
@@ -22,18 +22,18 @@ read_hello( client_sock )
# closed its end yet.
kidpid = fork do
client_sock.close
new_sock = nil
client.close
new_client = nil
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 )
new_sock = connect( addr, port, "Timed out reconnecting." )
read_hello( new_sock )
new_client = FakeSource.new( addr, port, "Timed out reconnecting." )
new_client.read_hello
exit 0
end
# Sleep for longer than the child, to give the flexnbd process a bit
# of slop
sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 3 )
client_sock.close
client.close
_,status = Process.waitpid2( kidpid )
exit status.exitstatus

View File

@@ -6,16 +6,17 @@
# disconnected.
require 'flexnbd/fake_source'
include FlexNBD::FakeSource
include FlexNBD
addr, port = *ARGV
client_sock = connect( addr, port, "Timed out connecting" )
read_hello( client_sock )
write_write_request( client_sock, 1 << 31, 1 << 31, "myhandle" )
response = read_response( client_sock )
client = FakeSource.new( addr, port, "Timed out connecting" )
client.read_hello
client.write_write_request( 1 << 31, 1 << 31, "myhandle" )
response = client.read_response
fail "Not an error" if response[:error] == 0
fail "Wrong handle" unless "myhandle" == response[:handle]
client_sock.close
client.close
exit(0)

View File

@@ -5,41 +5,46 @@ require "timeout"
require 'flexnbd/constants'
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
TCPSocket.new( addr, port, source_addr, source_port )
@sock = TCPSocket.new( addr, port, source_addr, source_port )
end
end
def read_hello( client_sock )
def close
@sock.close
end
def read_hello()
timing_out( FlexNBD::MS_HELLO_TIME_SECS,
"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
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
client_sock.write( "\x25\x60\x95\x13" )
client_sock.write( "\x00\x00\x00\x01" )
client_sock.write( handle )
client_sock.write( "\x0"*4 )
client_sock.write( [from].pack( 'N' ) )
client_sock.write( [len ].pack( 'N' ) )
@sock.write( "\x25\x60\x95\x13" )
@sock.write( "\x00\x00\x00\x01" )
@sock.write( handle )
@sock.write( "\x0"*4 )
@sock.write( [from].pack( 'N' ) )
@sock.write( [len ].pack( 'N' ) )
end
def read_response( client_sock )
magic = client_sock.read(4)
error_s = client_sock.read(4)
handle = client_sock.read(8)
def read_response
magic = @sock.read(4)
error_s = @sock.read(4)
handle = @sock.read(8)
{
:magic => magic,
@@ -49,6 +54,13 @@ module FlexNBD
end
def ensure_disconnected
Timeout.timeout( 2 ) do
@sock.read(1)
end
end
def timing_out( time, msg )
begin
Timeout.timeout( time ) do
@@ -61,5 +73,5 @@ module FlexNBD
end
end # module FakeSource
end # class FakeSource
end # module FlexNBD