Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 2 | require File.dirname(__FILE__) + "/socket_spec_shared" |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 3 | |
| 4 | class ThriftSocketSpec < Spec::ExampleGroup |
| 5 | include Thrift |
| 6 | |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 7 | describe Socket do |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 8 | before(:each) do |
| 9 | @socket = Socket.new |
| 10 | @handle = mock("Handle", :closed? => false) |
| 11 | @handle.stub!(:close) |
| 12 | TCPSocket.stub!(:new).and_return(@handle) |
| 13 | end |
| 14 | |
| 15 | it_should_behave_like "a socket" |
| 16 | |
| 17 | it "should raise a TransportException when it cannot open a socket" do |
| 18 | TCPSocket.should_receive(:new).and_raise(StandardError) |
| 19 | lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN } |
| 20 | end |
| 21 | |
| 22 | it "should open a TCPSocket with default args" do |
| 23 | TCPSocket.should_receive(:new).with('localhost', 9090) |
| 24 | @socket.open |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 25 | end |
| 26 | |
| 27 | it "should accept host/port options" do |
| 28 | TCPSocket.should_receive(:new).with('my.domain', 1234) |
| 29 | Socket.new('my.domain', 1234).open |
| 30 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 31 | end |
| 32 | |
| 33 | describe ServerSocket do |
| 34 | before(:each) do |
| 35 | @socket = ServerSocket.new(1234) |
| 36 | end |
| 37 | |
| 38 | it "should create a handle when calling listen" do |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 39 | TCPServer.should_receive(:new).with(nil, 1234) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 40 | @socket.listen |
| 41 | end |
| 42 | |
Kevin Clark | c675870 | 2008-06-18 01:15:45 +0000 | [diff] [blame] | 43 | it "should accept an optional host argument" do |
| 44 | @socket = ServerSocket.new('localhost', 1234) |
| 45 | TCPServer.should_receive(:new).with('localhost', 1234) |
| 46 | @socket.listen |
| 47 | end |
| 48 | |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 49 | it "should create a Thrift::Socket to wrap accepted sockets" do |
| 50 | handle = mock("TCPServer") |
| 51 | TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) |
| 52 | @socket.listen |
| 53 | sock = mock("sock") |
| 54 | handle.should_receive(:accept).and_return(sock) |
| 55 | trans = mock("Socket") |
| 56 | Socket.should_receive(:new).and_return(trans) |
Kevin Clark | 6c30dbb | 2008-06-18 01:15:25 +0000 | [diff] [blame] | 57 | trans.should_receive(:handle=).with(sock) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 58 | @socket.accept.should == trans |
| 59 | end |
| 60 | |
| 61 | it "should close the handle when closed" do |
Kevin Clark | 1067425 | 2008-06-18 01:17:30 +0000 | [diff] [blame] | 62 | handle = mock("TCPServer", :closed? => false) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 63 | TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) |
| 64 | @socket.listen |
| 65 | handle.should_receive(:close) |
| 66 | @socket.close |
| 67 | end |
Kevin Clark | 0d6007c | 2008-06-18 01:13:27 +0000 | [diff] [blame] | 68 | |
| 69 | it "should return nil when accepting if there is no handle" do |
| 70 | @socket.accept.should be_nil |
| 71 | end |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame^] | 72 | |
| 73 | it "should return true for closed? when appropriate" do |
| 74 | handle = mock("TCPServer", :closed? => false) |
| 75 | TCPServer.stub!(:new).and_return(handle) |
| 76 | @socket.listen |
| 77 | @socket.should_not be_closed |
| 78 | handle.stub!(:close) |
| 79 | @socket.close |
| 80 | @socket.should be_closed |
| 81 | @socket.listen |
| 82 | @socket.should_not be_closed |
| 83 | handle.stub!(:closed?).and_return(true) |
| 84 | @socket.should be_closed |
| 85 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 86 | end |
| 87 | end |