From 64ebbe7688ed3f01a50db3a17800c753ee2076fe Mon Sep 17 00:00:00 2001 From: Alex Young Date: Tue, 3 Jul 2012 14:39:05 +0100 Subject: [PATCH] Refactor FakeSource from a module to a class --- .../fakes/source/close_after_connect.rb | 7 ++- .../fakes/source/close_after_hello.rb | 12 +++-- .../fakes/source/connect_during_hello.rb | 17 ++++--- .../fakes/source/connect_from_banned_ip.rb | 13 +++--- .../fakes/source/hang_after_hello.rb | 16 +++---- .../fakes/source/write_out_of_range.rb | 13 +++--- tests/acceptance/flexnbd/fake_source.rb | 46 ++++++++++++------- 7 files changed, 70 insertions(+), 54 deletions(-) diff --git a/tests/acceptance/fakes/source/close_after_connect.rb b/tests/acceptance/fakes/source/close_after_connect.rb index 0317c42..5b05db8 100755 --- a/tests/acceptance/fakes/source/close_after_connect.rb +++ b/tests/acceptance/fakes/source/close_after_connect.rb @@ -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 diff --git a/tests/acceptance/fakes/source/close_after_hello.rb b/tests/acceptance/fakes/source/close_after_hello.rb index e5b064e..952fdf4 100755 --- a/tests/acceptance/fakes/source/close_after_hello.rb +++ b/tests/acceptance/fakes/source/close_after_hello.rb @@ -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) diff --git a/tests/acceptance/fakes/source/connect_during_hello.rb b/tests/acceptance/fakes/source/connect_during_hello.rb index 7ceba92..fac5ab7 100755 --- a/tests/acceptance/fakes/source/connect_during_hello.rb +++ b/tests/acceptance/fakes/source/connect_during_hello.rb @@ -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) diff --git a/tests/acceptance/fakes/source/connect_from_banned_ip.rb b/tests/acceptance/fakes/source/connect_from_banned_ip.rb index 98221fd..f87b099 100755 --- a/tests/acceptance/fakes/source/connect_from_banned_ip.rb +++ b/tests/acceptance/fakes/source/connect_from_banned_ip.rb @@ -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) diff --git a/tests/acceptance/fakes/source/hang_after_hello.rb b/tests/acceptance/fakes/source/hang_after_hello.rb index 185c6ca..6deb6d7 100755 --- a/tests/acceptance/fakes/source/hang_after_hello.rb +++ b/tests/acceptance/fakes/source/hang_after_hello.rb @@ -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 diff --git a/tests/acceptance/fakes/source/write_out_of_range.rb b/tests/acceptance/fakes/source/write_out_of_range.rb index 529de7e..cc093a7 100755 --- a/tests/acceptance/fakes/source/write_out_of_range.rb +++ b/tests/acceptance/fakes/source/write_out_of_range.rb @@ -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) diff --git a/tests/acceptance/flexnbd/fake_source.rb b/tests/acceptance/flexnbd/fake_source.rb index 798e70e..1815ad8 100644 --- a/tests/acceptance/flexnbd/fake_source.rb +++ b/tests/acceptance/flexnbd/fake_source.rb @@ -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