blob: c5866ee74c69a1ddfc4bebd7c4102c117f8e3c16 [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 Clark5ae0bab2008-07-18 21:49:50 +000031
32 it "should accept an optional timeout" do
33 TCPSocket.stub!(:new)
34 Socket.new('localhost', 8080, 5).timeout.should == 5
35 end
Kevin Clark3c871292008-06-18 01:12:05 +000036 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 Clark2ddd8ed2008-06-18 01:18:35 +000044 TCPServer.should_receive(:new).with(nil, 1234)
Kevin Clark3c871292008-06-18 01:12:05 +000045 @socket.listen
46 end
47
Kevin Clarkc6758702008-06-18 01:15:45 +000048 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 Clark3c871292008-06-18 01:12:05 +000054 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 Clark6c30dbb2008-06-18 01:15:25 +000062 trans.should_receive(:handle=).with(sock)
Kevin Clark3c871292008-06-18 01:12:05 +000063 @socket.accept.should == trans
64 end
65
66 it "should close the handle when closed" do
Kevin Clark10674252008-06-18 01:17:30 +000067 handle = mock("TCPServer", :closed? => false)
Kevin Clark3c871292008-06-18 01:12:05 +000068 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
69 @socket.listen
70 handle.should_receive(:close)
71 @socket.close
72 end
Kevin Clark0d6007c2008-06-18 01:13:27 +000073
74 it "should return nil when accepting if there is no handle" do
75 @socket.accept.should be_nil
76 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +000077
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 Clark3c871292008-06-18 01:12:05 +000091 end
92end