1. Michael Granger
  2. ruby-pg

Commits

Lars Kanis  committed 4a6fe37

Avoid fork() in specs.

fork() is not implemented on windows and jruby.

On Rubinius fork() blocks in these tests or raises 'server closed
the connection unexpectedly' after the test.

  • Participants
  • Parent commits ccc3e80
  • Branches default

Comments (0)

Files changed (2)

File spec/lib/helpers.rb

View file
  • Ignore whitespace
 	end
 
 
-	NOFORK_PLATFORMS = %w{java}
-
 	### Run the specified command +cmd+ after redirecting stdout and stderr to the specified
 	### +logpath+, failing if the execution fails.
 	def log_and_run( logpath, *cmd )
 		# Eliminate the noise of creating/tearing down the database by
 		# redirecting STDERR/STDOUT to a logfile if the Ruby interpreter
 		# supports fork()
-		if NOFORK_PLATFORMS.include?( RUBY_PLATFORM )
+		logfh = File.open( logpath, File::WRONLY|File::CREAT|File::APPEND )
+		begin
+			pid = fork
+		rescue NotImplementedError
+			logfh.close
 			system( *cmd )
 		else
-			logfh = File.open( logpath, File::WRONLY|File::CREAT|File::APPEND )
-			if pid = fork
+			if pid
 				logfh.close
 			else
 				$stdout.reopen( logfh )

File spec/pg/connection_spec.rb

View file
  • Ignore whitespace
 		@conn.exec( 'ROLLBACK' )
 		@conn.exec( 'LISTEN woo' )
 
-		pid = fork do
+		t = Thread.new do
 			begin
 				conn = described_class.connect( @conninfo )
 				sleep 1
-				conn.exec( 'NOTIFY woo' )
+				conn.async_exec( 'NOTIFY woo' )
 			ensure
 				conn.finish
-				exit!
 			end
 		end
 
 		@conn.wait_for_notify( 10 ).should == 'woo'
 		@conn.exec( 'UNLISTEN woo' )
 
-		Process.wait( pid )
+		t.join
 	end
 
 	it "calls a block for NOTIFY events if one is given" do
 		@conn.exec( 'ROLLBACK' )
 		@conn.exec( 'LISTEN woo' )
 
-		pid = fork do
+		t = Thread.new do
 			begin
 				conn = described_class.connect( @conninfo )
 				sleep 1
-				conn.exec( 'NOTIFY woo' )
+				conn.async_exec( 'NOTIFY woo' )
 			ensure
 				conn.finish
-				exit!
 			end
 		end
 
 
 		@conn.exec( 'UNLISTEN woo' )
 
-		Process.wait( pid )
+		t.join
 	end
 
 	it "doesn't collapse sequential notifications" do
 		@conn.exec( 'LISTEN war' )
 		@conn.exec( 'LISTEN woz' )
 
-		pid = fork do
-			begin
-				conn = described_class.connect( @conninfo )
-				conn.exec( 'NOTIFY woo' )
-				conn.exec( 'NOTIFY war' )
-				conn.exec( 'NOTIFY woz' )
-			ensure
-				conn.finish
-				exit!
-			end
+		begin
+			conn = described_class.connect( @conninfo )
+			conn.exec( 'NOTIFY woo' )
+			conn.exec( 'NOTIFY war' )
+			conn.exec( 'NOTIFY woz' )
+		ensure
+			conn.finish
 		end
 
-		Process.wait( pid )
-
 		channels = []
 		3.times do
 			channels << @conn.wait_for_notify( 2 )
 		@conn.exec( 'ROLLBACK' )
 		@conn.exec( 'LISTEN woo' )
 
-		pid = fork do
-			begin
-				conn = described_class.connect( @conninfo )
-				conn.exec( 'NOTIFY woo' )
-			ensure
-				conn.finish
-				exit!
-			end
+		begin
+			conn = described_class.connect( @conninfo )
+			conn.exec( 'NOTIFY woo' )
+		ensure
+			conn.finish
 		end
 
-		# Wait for the forked child to send the notification
-		Process.wait( pid )
-
 		# Cause the notification to buffer, but not be read yet
 		@conn.exec( 'SELECT 1' )
 
 			@conn.exec( 'ROLLBACK' )
 			@conn.exec( 'LISTEN knees' )
 
-			pid = fork do
-				conn = described_class.connect( @conninfo )
-				conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
-				conn.finish
-				exit!
-			end
-
-			Process.wait( pid )
+			conn = described_class.connect( @conninfo )
+			conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
+			conn.finish
 
 			event, pid, msg = nil
 			@conn.wait_for_notify( 10 ) do |*args|
 			@conn.exec( 'ROLLBACK' )
 			@conn.exec( 'LISTEN knees' )
 
-			pid = fork do
-				conn = described_class.connect( @conninfo )
-				conn.exec( %Q{NOTIFY knees} )
-				conn.finish
-				exit!
-			end
-
-			Process.wait( pid )
+			conn = described_class.connect( @conninfo )
+			conn.exec( %Q{NOTIFY knees} )
+			conn.finish
 
 			event, pid = nil
 			@conn.wait_for_notify( nil ) do |*args|
 			@conn.exec( 'ROLLBACK' )
 			@conn.exec( 'LISTEN knees' )
 
-			pid = fork do
-				conn = described_class.connect( @conninfo )
-				conn.exec( %Q{NOTIFY knees} )
-				conn.finish
-				exit!
-			end
-
-			Process.wait( pid )
+			conn = described_class.connect( @conninfo )
+			conn.exec( %Q{NOTIFY knees} )
+			conn.finish
 
 			payload = :notnil
 			@conn.wait_for_notify( nil ) do |*args|
 			@conn.exec( 'ROLLBACK' )
 			@conn.exec( 'LISTEN knees' )
 
-			pid = fork do
-				conn = described_class.connect( @conninfo )
-				conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
-				conn.finish
-				exit!
-			end
-
-			Process.wait( pid )
+			conn = described_class.connect( @conninfo )
+			conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
+			conn.finish
 
 			event, pid, msg = nil
 			@conn.wait_for_notify( 10 ) do |arg1, arg2|
 			@conn.exec( 'ROLLBACK' )
 			@conn.exec( 'LISTEN knees' )
 
-			pid = fork do
-				conn = described_class.connect( @conninfo )
-				conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
-				conn.finish
-				exit!
-			end
-
-			Process.wait( pid )
+			conn = described_class.connect( @conninfo )
+			conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
+			conn.finish
 
 			notification_received = false
 			@conn.wait_for_notify( 10 ) do
 			@conn.exec( 'ROLLBACK' )
 			@conn.exec( 'LISTEN knees' )
 
-			pid = fork do
-				conn = described_class.connect( @conninfo )
-				conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
-				conn.finish
-				exit!
-			end
-
-			Process.wait( pid )
+			conn = described_class.connect( @conninfo )
+			conn.exec( %Q{NOTIFY knees, 'skirt and boots'} )
+			conn.finish
 
 			event, pid, msg = nil
 			@conn.wait_for_notify( 10 ) do |arg1, arg2, arg3|