File: spawner_error_handling_spec.rb

package info (click to toggle)
passenger 2.2.11debian-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,576 kB
  • ctags: 28,138
  • sloc: cpp: 66,323; ruby: 9,646; ansic: 2,425; python: 141; sh: 56; makefile: 29
file content (107 lines) | stat: -rw-r--r-- 3,262 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require 'stringio'

shared_examples_for "handling errors in application initialization" do
	before :each do
		@stub = setup_rails_stub('foobar')
		@stub.use_vendor_rails('minimal')
	end
	
	after :each do
		teardown_rails_stub
	end

	it "raises an AppInitError if the spawned app raises a standard exception during startup" do
		File.prepend(@stub.environment_rb, "raise 'This is a dummy exception.'\n")
		begin
			spawn_stub_application(@stub, "print_exceptions" => false).close
			violated "Spawning the application should have raised an AppInitError."
		rescue AppInitError => e
			e.child_exception.message.should == "This is a dummy exception."
		end
	end
	
	it "raises an AppInitError if the spawned app raises a custom-defined exception during startup" do
		File.prepend(@stub.environment_rb, %{
			class MyError < StandardError
			end
			
			raise MyError, "This is a custom exception."
		})
		begin
			spawn_stub_application(@stub, "print_exceptions" => false).close
			violated "Spawning the application should have raised an AppInitError."
		rescue AppInitError => e
			e.child_exception.message.should == "This is a custom exception. (MyError)"
		end
	end
	
	it "raises an AppInitError if the spawned app calls exit() during startup" do
		File.prepend(@stub.environment_rb, "exit\n")
		begin
			spawn_stub_application(@stub, "print_exceptions" => false).close
			violated "Spawning the application should have raised an AppInitError."
		rescue AppInitError => e
			e.child_exception.class.should == SystemExit
		end
	end
	
	it "prints the exception to STDERR if the spawned app raised an error" do
		old_stderr = STDERR
		file = File.new('output.tmp', 'w+')
		begin
			Object.send(:remove_const, "STDERR") rescue nil
			Object.const_set("STDERR", file)
			
			File.prepend(@stub.environment_rb, "raise 'This is a dummy exception.'\n")
			block = lambda do
				spawn_stub_application(@stub).close
			end
			block.should raise_error(AppInitError)
			
			file.rewind
			data = file.read
			data.should =~ /spawn_stub_application/
			data.should =~ /spawner_error_handling_spec\.rb/
		ensure
			Object.send(:remove_const, "STDERR") rescue nil
			Object.const_set("STDERR", old_stderr)
			file.close rescue nil
			File.unlink('output.tmp') rescue nil
		end
	end
end

shared_examples_for "handling errors in framework initialization" do
	include Utils
	
	it "raises FrameworkInitError if the framework could not be loaded" do
		block = lambda do
			load_nonexistant_framework(:print_framework_loading_exceptions => false).close
		end
		block.should raise_error(FrameworkInitError)
	end
	
	it "prints the exception to STDERR if the framework could not be loaded" do
		old_stderr = STDERR
		file = File.new('output.tmp', 'w+')
		begin
			Object.send(:remove_const, "STDERR") rescue nil
			Object.const_set("STDERR", file)
			
			block = lambda do
				load_nonexistant_framework.close
			end
			block.should raise_error(FrameworkInitError)
			
			file.rewind
			data = file.read
			data.should =~ /load_nonexistant_framework/
			data.should =~ /spawner_error_handling_spec\.rb/
		ensure
			Object.send(:remove_const, "STDERR") rescue nil
			Object.const_set("STDERR", old_stderr)
			file.close rescue nil
			File.unlink('output.tmp') rescue nil
		end
	end
end