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 | 5ae0bab | 2008-07-18 21:49:50 +0000 | [diff] [blame] | 31 | |
| 32 | it "should accept an optional timeout" do |
| 33 | TCPSocket.stub!(:new) |
| 34 | Socket.new('localhost', 8080, 5).timeout.should == 5 |
| 35 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 36 | end |
| 37 | |
| 38 | describe ServerSocket do |
| 39 | before(:each) do |
| 40 | @socket = ServerSocket.new(1234) |
| 41 | end |
| 42 | |
| 43 | it "should create a handle when calling listen" do |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 44 | TCPServer.should_receive(:new).with(nil, 1234) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 45 | @socket.listen |
| 46 | end |
| 47 | |
Kevin Clark | c675870 | 2008-06-18 01:15:45 +0000 | [diff] [blame] | 48 | it "should accept an optional host argument" do |
| 49 | @socket = ServerSocket.new('localhost', 1234) |
| 50 | TCPServer.should_receive(:new).with('localhost', 1234) |
| 51 | @socket.listen |
| 52 | end |
| 53 | |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 54 | it "should create a Thrift::Socket to wrap accepted sockets" do |
| 55 | handle = mock("TCPServer") |
| 56 | TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) |
| 57 | @socket.listen |
| 58 | sock = mock("sock") |
| 59 | handle.should_receive(:accept).and_return(sock) |
| 60 | trans = mock("Socket") |
| 61 | Socket.should_receive(:new).and_return(trans) |
Kevin Clark | 6c30dbb | 2008-06-18 01:15:25 +0000 | [diff] [blame] | 62 | trans.should_receive(:handle=).with(sock) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 63 | @socket.accept.should == trans |
| 64 | end |
| 65 | |
| 66 | it "should close the handle when closed" do |
Kevin Clark | 1067425 | 2008-06-18 01:17:30 +0000 | [diff] [blame] | 67 | handle = mock("TCPServer", :closed? => false) |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 68 | TCPServer.should_receive(:new).with(nil, 1234).and_return(handle) |
| 69 | @socket.listen |
| 70 | handle.should_receive(:close) |
| 71 | @socket.close |
| 72 | end |
Kevin Clark | 0d6007c | 2008-06-18 01:13:27 +0000 | [diff] [blame] | 73 | |
| 74 | it "should return nil when accepting if there is no handle" do |
| 75 | @socket.accept.should be_nil |
| 76 | end |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 77 | |
| 78 | it "should return true for closed? when appropriate" do |
| 79 | handle = mock("TCPServer", :closed? => false) |
| 80 | TCPServer.stub!(:new).and_return(handle) |
| 81 | @socket.listen |
| 82 | @socket.should_not be_closed |
| 83 | handle.stub!(:close) |
| 84 | @socket.close |
| 85 | @socket.should be_closed |
| 86 | @socket.listen |
| 87 | @socket.should_not be_closed |
| 88 | handle.stub!(:closed?).and_return(true) |
| 89 | @socket.should be_closed |
| 90 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 91 | end |
| 92 | end |