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