blob: 4f5868b68e049416b7143d726667551a454c56e8 [file] [log] [blame]
Kevin Clark3c871292008-06-18 01:12:05 +00001require File.dirname(__FILE__) + '/spec_helper'
2
3class ThriftSocketSpec < Spec::ExampleGroup
4 include Thrift
5
6 before(:each) do
7 @socket = Socket.new
8 @handle = mock("Handle")
Kevin Clark1d4b2d82008-06-18 01:16:11 +00009 @handle.stub!(:close)
Kevin Clark3c871292008-06-18 01:12:05 +000010 end
11
12 describe Socket do
13 it "should open a TCPSocket" do
14 TCPSocket.should_receive(:new).with('localhost', 9090).and_return(@handle)
15 @socket.open.should == @handle
16 end
17
18 it "should accept host/port options" do
19 TCPSocket.should_receive(:new).with('my.domain', 1234)
20 Socket.new('my.domain', 1234).open
21 end
22
23 it "should raise a TransportException when it cannot open a socket" do
24 TCPSocket.should_receive(:new).with('localhost', 9090).and_raise(StandardError)
25 lambda { @socket.open }.should raise_error(TransportException, "Could not connect to localhost:9090") { |e| e.type.should == TransportException::NOT_OPEN }
26 end
27
28 it "should be open whenever it has a handle" do
29 @socket.should_not be_open
30 TCPSocket.should_receive(:new).and_return(@handle)
31 @socket.open
32 @socket.should be_open
Kevin Clark6c30dbb2008-06-18 01:15:25 +000033 @socket.handle = nil
Kevin Clark3c871292008-06-18 01:12:05 +000034 @socket.should_not be_open
Kevin Clark6c30dbb2008-06-18 01:15:25 +000035 @socket.handle = @handle
Kevin Clark3c871292008-06-18 01:12:05 +000036 @handle.should_receive(:close)
37 @socket.close
38 @socket.should_not be_open
39 end
40
41 it "should write data to the handle" do
42 TCPSocket.should_receive(:new).and_return(@handle)
43 @socket.open
44 @handle.should_receive(:write).with("foobar")
45 @socket.write("foobar")
46 @handle.should_receive(:write).with("fail").and_raise(StandardError)
47 lambda { @socket.write("fail") }.should raise_error(TransportException) { |e| e.type.should == TransportException::NOT_OPEN }
48 end
49
50 it "should raise an error when it cannot read from the handle" do
51 TCPSocket.should_receive(:new).and_return(@handle)
52 @socket.open
Kevin Clarkc78eeef2008-06-18 01:15:36 +000053 @handle.should_receive(:read).with(17).and_raise(StandardError)
Kevin Clark3c871292008-06-18 01:12:05 +000054 lambda { @socket.read(17) }.should raise_error(TransportException) { |e| e.type.should == TransportException::NOT_OPEN }
55 end
56
57 it "should raise an error when it reads no data from the handle" do
58 TCPSocket.should_receive(:new).and_return(@handle)
59 @socket.open
Kevin Clarkc78eeef2008-06-18 01:15:36 +000060 @handle.should_receive(:read).with(17).and_return("")
Kevin Clark3c871292008-06-18 01:12:05 +000061 lambda { @socket.read(17) }.should raise_error(TransportException, "Socket: Could not read 17 bytes from localhost:9090")
62 end
63
64 it "should return the data read when reading from the handle works" do
65 TCPSocket.should_receive(:new).and_return(@handle)
66 @socket.open
Kevin Clarkc78eeef2008-06-18 01:15:36 +000067 @handle.should_receive(:read).with(17).and_return("test data")
Kevin Clark3c871292008-06-18 01:12:05 +000068 @socket.read(17).should == "test data"
69 end
Kevin Clarkf98286a2008-06-18 01:14:08 +000070
71 it "should declare itself as closed when it has an error" do
Kevin Clark1d4b2d82008-06-18 01:16:11 +000072 TCPSocket.should_receive(:new).and_return(@handle)
73 @socket.open
74 @handle.should_receive(:write).with("fail").and_raise(StandardError)
75 @socket.should be_open
76 lambda { @socket.write("fail") }.should raise_error
77 @socket.should_not be_open
Kevin Clarkf98286a2008-06-18 01:14:08 +000078 end
Kevin Clark3c871292008-06-18 01:12:05 +000079 end
80
81 describe ServerSocket do
82 before(:each) do
83 @socket = ServerSocket.new(1234)
84 end
85
86 it "should create a handle when calling listen" do
Kevin Clark3c871292008-06-18 01:12:05 +000087 @socket.listen
Kevin Clark6c30dbb2008-06-18 01:15:25 +000088 @socket.handle.should be_an_instance_of(TCPServer)
Kevin Clark3c871292008-06-18 01:12:05 +000089 end
90
Kevin Clarkc6758702008-06-18 01:15:45 +000091 it "should accept an optional host argument" do
92 @socket = ServerSocket.new('localhost', 1234)
93 TCPServer.should_receive(:new).with('localhost', 1234)
94 @socket.listen
95 end
96
Kevin Clark3c871292008-06-18 01:12:05 +000097 it "should create a Thrift::Socket to wrap accepted sockets" do
98 handle = mock("TCPServer")
99 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
100 @socket.listen
101 sock = mock("sock")
102 handle.should_receive(:accept).and_return(sock)
103 trans = mock("Socket")
104 Socket.should_receive(:new).and_return(trans)
Kevin Clark6c30dbb2008-06-18 01:15:25 +0000105 trans.should_receive(:handle=).with(sock)
Kevin Clark3c871292008-06-18 01:12:05 +0000106 @socket.accept.should == trans
107 end
108
109 it "should close the handle when closed" do
110 handle = mock("TCPServer")
111 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
112 @socket.listen
113 handle.should_receive(:close)
114 @socket.close
115 end
Kevin Clark0d6007c2008-06-18 01:13:27 +0000116
117 it "should return nil when accepting if there is no handle" do
118 @socket.accept.should be_nil
119 end
Kevin Clark3c871292008-06-18 01:12:05 +0000120 end
121end