blob: 4d129b0480f7118fc9647fa470264ab3cf8632a4 [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")
9 end
10
11 describe Socket do
12 it "should open a TCPSocket" do
13 TCPSocket.should_receive(:new).with('localhost', 9090).and_return(@handle)
14 @socket.open.should == @handle
15 end
16
17 it "should accept host/port options" do
18 TCPSocket.should_receive(:new).with('my.domain', 1234)
19 Socket.new('my.domain', 1234).open
20 end
21
22 it "should raise a TransportException when it cannot open a socket" do
23 TCPSocket.should_receive(:new).with('localhost', 9090).and_raise(StandardError)
24 lambda { @socket.open }.should raise_error(TransportException, "Could not connect to localhost:9090") { |e| e.type.should == TransportException::NOT_OPEN }
25 end
26
27 it "should be open whenever it has a handle" do
28 @socket.should_not be_open
29 TCPSocket.should_receive(:new).and_return(@handle)
30 @socket.open
31 @socket.should be_open
Kevin Clark6c30dbb2008-06-18 01:15:25 +000032 @socket.handle = nil
Kevin Clark3c871292008-06-18 01:12:05 +000033 @socket.should_not be_open
Kevin Clark6c30dbb2008-06-18 01:15:25 +000034 @socket.handle = @handle
Kevin Clark3c871292008-06-18 01:12:05 +000035 @handle.should_receive(:close)
36 @socket.close
37 @socket.should_not be_open
38 end
39
40 it "should write data to the handle" do
41 TCPSocket.should_receive(:new).and_return(@handle)
42 @socket.open
43 @handle.should_receive(:write).with("foobar")
44 @socket.write("foobar")
45 @handle.should_receive(:write).with("fail").and_raise(StandardError)
46 lambda { @socket.write("fail") }.should raise_error(TransportException) { |e| e.type.should == TransportException::NOT_OPEN }
47 end
48
49 it "should raise an error when it cannot read from the handle" do
50 TCPSocket.should_receive(:new).and_return(@handle)
51 @socket.open
Kevin Clarkc78eeef2008-06-18 01:15:36 +000052 @handle.should_receive(:read).with(17).and_raise(StandardError)
Kevin Clark3c871292008-06-18 01:12:05 +000053 lambda { @socket.read(17) }.should raise_error(TransportException) { |e| e.type.should == TransportException::NOT_OPEN }
54 end
55
56 it "should raise an error when it reads no data from the handle" do
57 TCPSocket.should_receive(:new).and_return(@handle)
58 @socket.open
Kevin Clarkc78eeef2008-06-18 01:15:36 +000059 @handle.should_receive(:read).with(17).and_return("")
Kevin Clark3c871292008-06-18 01:12:05 +000060 lambda { @socket.read(17) }.should raise_error(TransportException, "Socket: Could not read 17 bytes from localhost:9090")
61 end
62
63 it "should return the data read when reading from the handle works" do
64 TCPSocket.should_receive(:new).and_return(@handle)
65 @socket.open
Kevin Clarkc78eeef2008-06-18 01:15:36 +000066 @handle.should_receive(:read).with(17).and_return("test data")
Kevin Clark3c871292008-06-18 01:12:05 +000067 @socket.read(17).should == "test data"
68 end
Kevin Clarkf98286a2008-06-18 01:14:08 +000069
70 it "should declare itself as closed when it has an error" do
Kevin Clarkec9106f2008-06-18 01:14:26 +000071 pending do
72 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
78 end
Kevin Clarkf98286a2008-06-18 01:14:08 +000079 end
Kevin Clark3c871292008-06-18 01:12:05 +000080 end
81
82 describe ServerSocket do
83 before(:each) do
84 @socket = ServerSocket.new(1234)
85 end
86
87 it "should create a handle when calling listen" do
Kevin Clark3c871292008-06-18 01:12:05 +000088 @socket.listen
Kevin Clark6c30dbb2008-06-18 01:15:25 +000089 @socket.handle.should be_an_instance_of(TCPServer)
Kevin Clark3c871292008-06-18 01:12:05 +000090 end
91
Kevin Clarkc6758702008-06-18 01:15:45 +000092 it "should accept an optional host argument" do
93 @socket = ServerSocket.new('localhost', 1234)
94 TCPServer.should_receive(:new).with('localhost', 1234)
95 @socket.listen
96 end
97
Kevin Clark3c871292008-06-18 01:12:05 +000098 it "should create a Thrift::Socket to wrap accepted sockets" do
99 handle = mock("TCPServer")
100 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
101 @socket.listen
102 sock = mock("sock")
103 handle.should_receive(:accept).and_return(sock)
104 trans = mock("Socket")
105 Socket.should_receive(:new).and_return(trans)
Kevin Clark6c30dbb2008-06-18 01:15:25 +0000106 trans.should_receive(:handle=).with(sock)
Kevin Clark3c871292008-06-18 01:12:05 +0000107 @socket.accept.should == trans
108 end
109
110 it "should close the handle when closed" do
111 handle = mock("TCPServer")
112 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
113 @socket.listen
114 handle.should_receive(:close)
115 @socket.close
116 end
Kevin Clark0d6007c2008-06-18 01:13:27 +0000117
118 it "should return nil when accepting if there is no handle" do
119 @socket.accept.should be_nil
120 end
Kevin Clark3c871292008-06-18 01:12:05 +0000121 end
122end