<commit_before>require 'support/config'
require 'passenger/spawn_manager'
require 'passenger/message_channel'
require 'passenger/utils'
require 'abstract_server_spec'
require 'minimal_spawner_spec'
require 'spawner_privilege_lowering_spec'
require 'spawner_error_handling_spec'
include Passenger
include Passenger::Utils

describe SpawnManager do
	before :each do
		@manager = SpawnManager.new
	end
	
	after :each do
		@manager.cleanup
	end
	
	describe "AbstractServer-like behavior" do
		before :each do
			@server = @manager
			@server.start
		end
		
		it_should_behave_like "AbstractServer"
	end
	
	it_should_behave_like "a minimal spawner"
	
	it "should not crash on spawning when running asynchronously" do
		app = @manager.spawn_application('stub/railsapp')
		app.close
	end
	
	it "should not crash on spawning when running synchronously" do
		a, b = UNIXSocket.pair
		pid = fork do
			begin
				a.close
				sleep(1) # Give @manager the chance to start.
				channel = MessageChannel.new(b)
				channel.write("spawn_application", "stub/minimal-railsapp", "true", "nobody")
				channel.read
				pid, listen_socket = channel.read
				channel.recv_io.close
				channel.close
			rescue Exception => e
				print_exception("child", e)
			ensure
				exit!
			end
		end
		b.close
		@manager.start_synchronously(a)
		a.close
		Process.waitpid(pid) rescue nil
	end
	
	def spawn_application
		@manager.spawn_application('stub/minimal-railsapp')
	end
end

describe SpawnManager do
	it_should_behave_like "handling errors in application initialization"
	it_should_behave_like "handling errors in framework initialization"
	
	def spawn_application(app_root)
		spawner = SpawnManager.new
		begin
			return spawner.spawn_application(app_root)
		ensure
			spawner.cleanup
		end
	end
	
	def load_nonexistant_framework
		Application.instance_eval do<commit_msg>Add tests for the new reload() behavior.<commit_after>require 'support/config'
require 'passenger/spawn_manager'
require 'passenger/message_channel'
require 'passenger/utils'
require 'abstract_server_spec'
require 'minimal_spawner_spec'
require 'spawner_privilege_lowering_spec'
require 'spawner_error_handling_spec'
include Passenger
include Passenger::Utils

describe SpawnManager do
	before :each do
		@manager = SpawnManager.new
	end
	
	after :each do
		@manager.cleanup
	end
	
	describe "AbstractServer-like behavior" do
		before :each do
			@server = @manager
			@server.start
		end
		
		it_should_behave_like "AbstractServer"
	end
	
	it_should_behave_like "a minimal spawner"
	
	it "should not crash on spawning when running asynchronously" do
		app = @manager.spawn_application('stub/railsapp')
		app.close
	end
	
	it "should not crash on spawning when running synchronously" do
		a, b = UNIXSocket.pair
		pid = fork do
			begin
				a.close
				sleep(1) # Give @manager the chance to start.
				channel = MessageChannel.new(b)
				channel.write("spawn_application", "stub/minimal-railsapp", "true", "nobody")
				channel.read
				pid, listen_socket = channel.read
				channel.recv_io.close
				channel.close
			rescue Exception => e
				print_exception("child", e)
			ensure
				exit!
			end
		end
		b.close
		@manager.start_synchronously(a)
		a.close
		Process.waitpid(pid) rescue nil
	end
	
	it "should not crash upon spawning an application that doesn't specify its Rails version" do
		@manager.spawn_application('stub/railsapp-without-version-spec').close
	end
	
	it "should properly reload applications that do not specify a Rails version" do
		@manager.spawn_application('stub/railsapp-without-version-spec').close
		@manager.reload('stub/railsapp-without-version-spec')
		spawners = @manager.instance_eval { @spawners }
		spawners.should be_empty
	end
	
	def spawn_application
		@manager.spawn_application('stub/minimal-railsapp')
	end
end

describe SpawnManager do
	it_should_behave_like "handling errors in application initialization"
	it_should_behave_like "handling errors in framework initialization"
	
	def spawn_application(app_root)
		spawner = SpawnManager.new
		begin
			return spawner.spawn_application(app_root)
		ensure
			spawner.cleanup
		end
	end
	
	def load_nonexistant_framework
		Application.instance_eval do
{'fraction_non_alphanumeric': 0.07471950346144665, 'fraction_numerical': 0.00047744091668656003, 'mean_word_length': 4.469973890339426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5079609', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1316, 'n_words': 351}