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

@@ -4,40 +4,20 @@
# a valid NBD hello with a random size, then check that we have see an
# EOF on read.
addr, port = *ARGV
require 'flexnbd/fake_dest'
include FlexNBD::FakeDest
require 'socket'
require 'timeout'
require 'flexnbd/constants'
sock = serve( *ARGV )
client_sock = accept( sock, "Timed out waiting for a connection" )
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
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" )
client_sock.write( "\x00\x00\x42\x02\x81\x86\x12\x53" )
8.times do client_sock.write rand(256).chr end
client_sock.write( "\x00" * 128 )
write_hello( client_sock, :size => :wrong )
t.join