blob: 4a7dda9056fa8fb339d54a8ef9a0cae61ce6ca6c [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
32 @socket.set_handle nil
33 @socket.should_not be_open
34 @socket.set_handle @handle
35 @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
52 @handle.should_receive(:recv).with(17).and_raise(StandardError)
53 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
59 @handle.should_receive(:recv).with(17).and_return("")
60 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
66 @handle.should_receive(:recv).with(17).and_return("test data")
67 @socket.read(17).should == "test data"
68 end
69 end
70
71 describe ServerSocket do
72 before(:each) do
73 @socket = ServerSocket.new(1234)
74 end
75
76 it "should create a handle when calling listen" do
77 TCPServer.should_receive(:new).with(nil, 1234)
78 @socket.listen
79 end
80
81 it "should create a Thrift::Socket to wrap accepted sockets" do
82 handle = mock("TCPServer")
83 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
84 @socket.listen
85 sock = mock("sock")
86 handle.should_receive(:accept).and_return(sock)
87 trans = mock("Socket")
88 Socket.should_receive(:new).and_return(trans)
89 trans.should_receive(:set_handle).with(sock)
90 @socket.accept.should == trans
91 end
92
93 it "should close the handle when closed" do
94 handle = mock("TCPServer")
95 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
96 @socket.listen
97 handle.should_receive(:close)
98 @socket.close
99 end
100 end
101end