Test that closing the socket immediately after sending write data causes an error

This commit is contained in:
Alex Young
2012-07-03 15:33:00 +01:00
parent d16aebf36e
commit 5c66d35677
3 changed files with 46 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# encoding: utf-8
# We connect, pause the server, issue a write request, send data,
# disconnect, then cont the server. This ensures that our disconnect
# happens before the server can try to write the reply.
require 'flexnbd/fake_source'
include FlexNBD
addr, port, srv_pid = *ARGV
client = FakeSource.new( addr, port, "Timed out connecting" )
client.read_hello
Process.kill( "STOP", srv_pid.to_i )
client.write_write_request( 0, 8 )
client.write_data( "12345678" )
client.close
Process.kill( "CONT", srv_pid.to_i )
# This sleep ensures that we don't return control to the test runner
# too soon, giving the flexnbd process time to fall over if it's going
# to.
sleep(0.25)
# ...and can we reconnect?
client2 = FakeSource.new( addr, port, "Timed out connecting" )
client2.close
exit(0)

View File

@@ -40,6 +40,10 @@ module FlexNBD
@sock.write( [len ].pack( 'N' ) )
end
def write_data( data )
@sock.write( data )
end
def read_response
magic = @sock.read(4)

View File

@@ -50,10 +50,18 @@ class TestDestErrorHandling < Test::Unit::TestCase
end
def test_disconnect_before_write_reply_causes_error
def test_disconnect_before_write_data_causes_error
run_fake( "source/close_after_write" )
end
def test_disconnect_before_write_reply_causes_error
# Note that this is an odd case: writing the reply doesn't fail.
# The test passes because the next attempt by flexnbd to read a
# request returns EOF.
run_fake( "source/close_after_write_data" )
end
private
def run_fake( name )
@env.run_fake( name, @env.ip, @env.port1 )