blob: 3fede9663a48dee6d0c177cd44cccba0e8ae6511 [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)
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 Clark3c871292008-06-18 01:12:05 +000025 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 Clark3c871292008-06-18 01:12:05 +000031 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 Clark2ddd8ed2008-06-18 01:18:35 +000039 TCPServer.should_receive(:new).with(nil, 1234)
Kevin Clark3c871292008-06-18 01:12:05 +000040 @socket.listen
41 end
42
Kevin Clarkc6758702008-06-18 01:15:45 +000043 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 Clark3c871292008-06-18 01:12:05 +000049 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 Clark6c30dbb2008-06-18 01:15:25 +000057 trans.should_receive(:handle=).with(sock)
Kevin Clark3c871292008-06-18 01:12:05 +000058 @socket.accept.should == trans
59 end
60
61 it "should close the handle when closed" do
Kevin Clark10674252008-06-18 01:17:30 +000062 handle = mock("TCPServer", :closed? => false)
Kevin Clark3c871292008-06-18 01:12:05 +000063 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
64 @socket.listen
65 handle.should_receive(:close)
66 @socket.close
67 end
Kevin Clark0d6007c2008-06-18 01:13:27 +000068
69 it "should return nil when accepting if there is no handle" do
70 @socket.accept.should be_nil
71 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +000072
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 Clark3c871292008-06-18 01:12:05 +000086 end
87end