File: unseekable_socket_spec.rb

package info (click to toggle)
ruby-passenger 3.0.13debian-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,920 kB
  • sloc: cpp: 99,104; ruby: 18,098; ansic: 9,846; sh: 8,632; python: 141; makefile: 30
file content (66 lines) | stat: -rw-r--r-- 1,751 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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'phusion_passenger/utils/unseekable_socket'

module PhusionPassenger

describe Utils::UnseekableSocket do
	class MyException < StandardError
	end
	
	class MySocket
		def write(data)
		end
		
		def gets
		end
		
		def sync=(value)
		end
	end
	
	before :each do
		@socket  = MySocket.new
		@wrapper = Utils::UnseekableSocket.wrap(@socket)
	end
	
	def catch_exception
		yield
		return nil
	rescue MyException => e
		return e
	end
	
	it "delegates method calls to the wrapped socket" do
		@socket.should_receive(:write).with("some data")
		@socket.should_receive(:gets)
		@wrapper.write("some data")
		@wrapper.gets
	end
	
	it "annotates exceptions so that we can identify its source" do
		@wrapper.source_of_exception?(MyException.new("foo")).should be_false
		
		@socket.should_receive(:write).at_least(:once).and_raise(MyException.new("an error"))
		@wrapper2 = Utils::UnseekableSocket.wrap(@socket)
		e1 = catch_exception { @wrapper.write("hello") }
		
		@wrapper.source_of_exception?(e1).should be_true
		@wrapper2.source_of_exception?(e1).should be_true
		
		@socket2 = MySocket.new
		@socket2.should_receive(:write).at_least(:once).and_raise(MyException.new("an error"))
		@wrapper.wrap(@socket2)
		@wrapper2.wrap(@socket2)
		e2 = catch_exception { @wrapper.write("hello") }
		
		@wrapper.source_of_exception?(e1).should be_false
		@wrapper2.source_of_exception?(e1).should be_false
		@wrapper.source_of_exception?(e2).should be_true
		@wrapper2.source_of_exception?(e2).should be_true
		
		Utils::UnseekableSocket.new.source_of_exception?(e1).should be_false
		Utils::UnseekableSocket.new.source_of_exception?(e2).should be_false
	end
end

end # module PhusionPassenger