flexnbd: Make a test a bit stricter

This commit is contained in:
nick
2013-09-20 16:00:56 +01:00
parent eb80c0d235
commit 6ffa10bf89

View File

@@ -35,22 +35,26 @@ class TestWriteDuringMigration < Test::Unit::TestCase
end end
end end
def debug_arg
ENV['DEBUG'] ? "--verbose" : ""
end
def launch_servers def launch_servers
@dst_proc = fork() { @dst_proc = fork() {
cmd = "#{@flexnbd} listen -l 127.0.0.1 -p #{@dest_port} -f #{@dest_file} -s #{@dest_sock}" cmd = "#{@flexnbd} listen -l 127.0.0.1 -p #{@dest_port} -f #{@dest_file} -s #{@dest_sock} #{debug_arg}"
exec cmd exec cmd
} }
@src_proc = fork() { @src_proc = fork() {
cmd = "#{@flexnbd} serve -l 127.0.0.1 -p #{@source_port} -f #{@source_file} -s #{@source_sock}" cmd = "#{@flexnbd} serve -l 127.0.0.1 -p #{@source_port} -f #{@source_file} -s #{@source_sock} #{debug_arg}"
exec cmd exec cmd
} }
begin begin
awaiting = nil awaiting = nil
Timeout.timeout(10) do Timeout.timeout(10) do
awaiting = :source awaiting = :source
sleep 0.1 while !File.exists?( @source_sock ) sleep 0.1 while !File.exists?( @source_sock )
awaiting = :dest awaiting = :dest
sleep 0.1 while !File.exists?( @dest_sock ) sleep 0.1 while !File.exists?( @dest_sock )
end end
@@ -79,7 +83,7 @@ class TestWriteDuringMigration < Test::Unit::TestCase
def start_mirror def start_mirror
UNIXSocket.open(@source_sock) {|sock| UNIXSocket.open(@source_sock) {|sock|
sock.write(["mirror", "127.0.0.1", @dest_port.to_s, "unlink"].join("\x0A") + "\x0A\x0A") sock.write(["mirror", "127.0.0.1", @dest_port.to_s, "exit"].join("\x0A") + "\x0A\x0A")
sock.flush sock.flush
rsp = sock.readline rsp = sock.readline
} }
@@ -101,14 +105,15 @@ class TestWriteDuringMigration < Test::Unit::TestCase
Dir.mktmpdir() do |tmpdir| Dir.mktmpdir() do |tmpdir|
Dir.chdir( tmpdir ) do Dir.chdir( tmpdir ) do
make_files() make_files()
launch_servers() launch_servers()
src_writer = Thread.new do src_writer = Thread.new do
client = FlexNBD::FakeSource.new( "127.0.0.1", @source_port, "Timed out connecting" ) client = FlexNBD::FakeSource.new( "127.0.0.1", @source_port, "Timed out connecting" )
offsets = Range.new(0, (@size - @write_data.size) / 4096 ).to_a
loop do loop do
begin begin
client.write(0, @write_data) client.write(offsets[rand(offsets.size)] * 4096, @write_data)
rescue => err rescue => err
# We expect a broken write at some point, so ignore it # We expect a broken write at some point, so ignore it
break break
@@ -119,9 +124,25 @@ class TestWriteDuringMigration < Test::Unit::TestCase
start_mirror() start_mirror()
wait_for_quit() wait_for_quit()
src_writer.join src_writer.join
# Ensure each block matches
File.open(@source_file, "r") do |source|
File.open(@dest_file, "r") do |dest|
0.upto( @size / 4096 ) do |block_num|
s_data = source.read( 4096 )
d_data = source.read( 4096 )
assert s_data == d_data, "Block #{block_num} mismatch!"
source.seek( 4096, IO::SEEK_CUR )
dest.seek( 4096, IO::SEEK_CUR )
end
end
end
end end
end end
end end
end end