Factor common code out of fake destinations

This commit is contained in:
Alex Young
2012-06-28 11:34:36 +01:00
parent 192471ee82
commit 9b717d6391
6 changed files with 85 additions and 117 deletions

View File

@@ -3,43 +3,22 @@
# Simulate a destination which sends the wrong magic.
# We expect the sender to disconnect and reconnect.
addr, port = *ARGV
require 'socket'
require 'timeout'
require 'flexnbd/constants'
sock = TCPServer.new( addr, port )
client_sock = nil
begin
Timeout.timeout(2) do
client_sock = sock.accept
end
rescue Timeout::Error
$stderr.puts "Timed out waiting for a connection"
exit 1
end
require 'flexnbd/fake_dest'
include FlexNBD::FakeDest
sock = serve( *ARGV )
client_sock = accept( sock, "Timed out waiting for a connection" )
# Launch a second thread so that we can spot the reconnection attempt
# as soon as it happens, or alternatively die a flaming death on
# timeout.
t = Thread.new do
begin
Timeout.timeout(FlexNBD::MS_RETRY_DELAY_SECS + 1) do
client_sock2 = sock.accept
end
rescue Timeout::Error
$stderr.puts "Timed out waiting for a reconnection"
exit 1
end
client_sock2 = accept( sock, "Timed out waiting for a reconnection",
FlexNBD::MS_RETRY_DELAY_SECS + 1 )
client_sock2.close
end
client_sock.write( "NBDMAGIC" )
# We're off in the last byte, should be \x53
client_sock.write( "\x00\x00\x42\x02\x81\x86\x12\x52" )
# 4096 is the right size; this is defined in nbd_scenarios
client_sock.write( "\x00\x00\x00\x00\x00\x00\x10\x00" )
client_sock.write( "\x00" * 128 )
write_hello( client_sock, :magic => :wrong )
t.join