blob: cd2fed4ca0f0e20a64e19063119b9dc38d5e44f9 [file] [log] [blame]
Kevin Clark3c871292008-06-18 01:12:05 +00001require File.dirname(__FILE__) + '/spec_helper'
Kevin Clark2ddd8ed2008-06-18 01:18:35 +00002require File.dirname(__FILE__) + "/socket_spec_shared"
Kevin Clark3c871292008-06-18 01:12:05 +00003
4class ThriftSocketSpec < Spec::ExampleGroup
5 include Thrift
6
Kevin Clark3c871292008-06-18 01:12:05 +00007 describe Socket do
Kevin Clark2ddd8ed2008-06-18 01:18:35 +00008 before(:each) do
9 @socket = Socket.new
10 @handle = mock("Handle", :closed? => false)
11 @handle.stub!(:close)
Kevin Clark36376452009-03-07 00:03:15 +000012 @handle.stub!(:connect_nonblock)
13 ::Socket.stub!(:new).and_return(@handle)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000014 end
15
16 it_should_behave_like "a socket"
17
18 it "should raise a TransportException when it cannot open a socket" do
Kevin Clark36376452009-03-07 00:03:15 +000019 ::Socket.should_receive(:new).and_raise(StandardError)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000020 lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
21 end
22
Kevin Clark36376452009-03-07 00:03:15 +000023 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 Clark2ddd8ed2008-06-18 01:18:35 +000027 @socket.open
Kevin Clark3c871292008-06-18 01:12:05 +000028 end
29
30 it "should accept host/port options" do
Kevin Clark36376452009-03-07 00:03:15 +000031 ::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 Clark3c871292008-06-18 01:12:05 +000034 Socket.new('my.domain', 1234).open
35 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000036
37 it "should accept an optional timeout" do
Kevin Clark36376452009-03-07 00:03:15 +000038 ::Socket.stub!(:new)
Kevin Clark5ae0bab2008-07-18 21:49:50 +000039 Socket.new('localhost', 8080, 5).timeout.should == 5
40 end
Kevin Clark3c871292008-06-18 01:12:05 +000041 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 Clark2ddd8ed2008-06-18 01:18:35 +000049 TCPServer.should_receive(:new).with(nil, 1234)
Kevin Clark3c871292008-06-18 01:12:05 +000050 @socket.listen
51 end
52
Kevin Clarkc6758702008-06-18 01:15:45 +000053 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 Clark3c871292008-06-18 01:12:05 +000059 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 Clark6c30dbb2008-06-18 01:15:25 +000067 trans.should_receive(:handle=).with(sock)
Kevin Clark3c871292008-06-18 01:12:05 +000068 @socket.accept.should == trans
69 end
70
71 it "should close the handle when closed" do
Kevin Clark10674252008-06-18 01:17:30 +000072 handle = mock("TCPServer", :closed? => false)
Kevin Clark3c871292008-06-18 01:12:05 +000073 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
74 @socket.listen
75 handle.should_receive(:close)
76 @socket.close
77 end
Kevin Clark0d6007c2008-06-18 01:13:27 +000078
79 it "should return nil when accepting if there is no handle" do
80 @socket.accept.should be_nil
81 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +000082
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 Clark3c871292008-06-18 01:12:05 +000096 end
97end