From 192471ee826912603e60d97c66a54c29d0b52557 Mon Sep 17 00:00:00 2001 From: Alex Young Date: Wed, 27 Jun 2012 17:28:24 +0100 Subject: [PATCH] Factor common code out of the test fake sources * * * More fake source refacoring --- tests/fakes/source/close_after_connect.rb | 26 +++++--------- tests/fakes/source/close_after_hello.rb | 37 +++---------------- tests/fakes/source/connect_during_hello.rb | 29 ++++----------- tests/fakes/source/hang_after_hello.rb | 42 ++++------------------ tests/flexnbd/fake_source.rb | 40 +++++++++++++++++++++ 5 files changed, 66 insertions(+), 108 deletions(-) create mode 100644 tests/flexnbd/fake_source.rb diff --git a/tests/fakes/source/close_after_connect.rb b/tests/fakes/source/close_after_connect.rb index 4ed2966..0317c42 100755 --- a/tests/fakes/source/close_after_connect.rb +++ b/tests/fakes/source/close_after_connect.rb @@ -6,27 +6,19 @@ # It then connects again, to check that the destination is still # listening. +require 'flexnbd/fake_source' +include FlexNBD::FakeSource + addr, port = *ARGV -require 'socket' -require 'timeout' -begin - Timeout.timeout( 2 ) do - sock = TCPSocket.open( addr, port.to_i ) - sock.close - end -rescue Timeout::Error - $stderr.puts "Failed to connect" - exit 1 -end -Timeout.timeout( 3 ) do +connect( 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. - sleep 0.5 - sock = TCPSocket.open( addr, port.to_i ) - sock.close -end + # 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 exit 0 diff --git a/tests/fakes/source/close_after_hello.rb b/tests/fakes/source/close_after_hello.rb index dc0acd1..e5b064e 100755 --- a/tests/fakes/source/close_after_hello.rb +++ b/tests/fakes/source/close_after_hello.rb @@ -7,43 +7,16 @@ # After the disconnect, we reconnect to be sure that the destination # is still alive. - -require 'socket' -require "timeout" -require 'flexnbd/constants' +require 'flexnbd/fake_source' +include FlexNBD::FakeSource addr, port = *ARGV -client_sock = nil -begin - Timeout.timeout(2) do - client_sock = TCPSocket.open( addr, port ) - end -rescue Timeout::Error - $stderr.puts "Timed out connecting." - exit 1 -end - -begin - Timeout.timeout( FlexNBD::MS_HELLO_TIME_SECS ) do - fail "No hello." unless (hello = client_sock.read( 152 )) && - hello.length==152 - end -rescue Timeout::Error - $stderr.puts "Timed out waiting for hello." - exit 1 -end +client_sock = connect( addr, port, "Timed out connecting." ) +read_hello( client_sock ) client_sock.close - sleep(0.2) -begin - Timeout.timeout(2) do - client_sock = TCPSocket.open( addr, port ) - end -rescue Timeout::Error - $stderr.puts "Timed out reconnecting." - exit 1 -end +connect( addr, port, "Timed out reconnecting." ) exit(0) diff --git a/tests/fakes/source/connect_during_hello.rb b/tests/fakes/source/connect_during_hello.rb index 756fa69..7ceba92 100755 --- a/tests/fakes/source/connect_during_hello.rb +++ b/tests/fakes/source/connect_during_hello.rb @@ -1,34 +1,17 @@ #!/usr/bin/env ruby # Connect to the destination, then hang. Connect a second time to the -# destinatioin. This will trigger the destination's thread clearer. +# destination. This will trigger the destination's thread clearer. -require 'socket' -require 'timeout' +require 'flexnbd/fake_source' +include FlexNBD::FakeSource addr, port = *ARGV # client_sock1 is a connection the destination is expecting. -client_sock1 = nil -begin - Timeout.timeout(2) do - client_sock1 = TCPSocket.open( addr, port ) - end -rescue Timeout::Error - $stderr.puts "Timed out connecting." - exit 1 -end - -#client_sock2 is the interloper. -client_sock2 = nil -begin - Timeout.timeout(2) do - client_sock2 = TCPSocket.open( addr, port ) - end -rescue Timeout::Error - $stderr.puts "Timed out connecting a second time." - exit 1 -end +client_sock1 = connect( addr, port, "Timed out connecting" ) +sleep(0.25) +client_sock2 = connect( addr, port, "Timed out connecting a second time" ) # This is the expected source crashing after connect client_sock1.close diff --git a/tests/fakes/source/hang_after_hello.rb b/tests/fakes/source/hang_after_hello.rb index 9070d9c..185c6ca 100755 --- a/tests/fakes/source/hang_after_hello.rb +++ b/tests/fakes/source/hang_after_hello.rb @@ -7,30 +7,11 @@ # listening for an incoming migration. addr, port = *ARGV -require 'socket' -require 'timeout' +require "flexnbd/fake_source" +include FlexNBD::FakeSource -require "flexnbd/constants" - - -client_sock=nil -begin - Timeout.timeout(2) do - client_sock = TCPSocket.open( addr, port ) - end -rescue Timeout::Error - $stderr.puts "Timed out connecting" - exit 1 -end - -begin - Timeout.timeout(FlexNBD::MS_HELLO_TIME_SECS) do - client_sock.read(152) - end -rescue Timeout::Error - $stderr.puts "Timed out reading hello" - exit 1 -end +client_sock = connect( addr, port, "Timed out connecting" ) +read_hello( client_sock ) # Now we do two things: @@ -44,19 +25,8 @@ kidpid = fork do client_sock.close new_sock = nil sleep( FlexNBD::CLIENT_MAX_WAIT_SECS + 1 ) - begin - Timeout.timeout( 2 ) do - new_sock = TCPSocket.open( addr, port ) - end - Timeout.timeout( FlexNBD::MS_HELLO_TIME_SECS ) do - fail "No hello." unless (hello = new_sock.read( 152 )) && - hello.length==152 - end - new_sock.close - rescue Timeout::Error - $stderr.puts "Timed out reconnecting" - exit 1 - end + new_sock = connect( addr, port, "Timed out reconnecting." ) + read_hello( new_sock ) exit 0 end diff --git a/tests/flexnbd/fake_source.rb b/tests/flexnbd/fake_source.rb new file mode 100644 index 0000000..40ae021 --- /dev/null +++ b/tests/flexnbd/fake_source.rb @@ -0,0 +1,40 @@ +# encoding: utf-8 + +require 'socket' +require "timeout" +require 'flexnbd/constants' + +module FlexNBD + module FakeSource + + def connect( addr, port, err_msg ) + timing_out( 2, err_msg ) do + TCPSocket.open( addr, port ) + end + end + + + def read_hello( client_sock ) + timing_out( FlexNBD::MS_HELLO_TIME_SECS, + "Timed out waiting for hello." ) do + fail "No hello." unless (hello = client_sock.read( 152 )) && + hello.length==152 + hello + end + end + + + def timing_out( time, msg ) + begin + Timeout.timeout( time ) do + yield + end + rescue Timeout::Error + $stderr.puts msg + exit 1 + end + end + + + end # module FakeSource +end # module FlexNBD