blob: 30b4648310674c780902faf40406c5f5454674e8 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Kevin Clark3c871292008-06-18 01:12:05 +000020require File.dirname(__FILE__) + '/spec_helper'
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000021require File.dirname(__FILE__) + "/socket_spec_shared"
Kevin Clark3c871292008-06-18 01:12:05 +000022
23class ThriftSocketSpec < Spec::ExampleGroup
24 include Thrift
25
Kevin Clark3c871292008-06-18 01:12:05 +000026 describe Socket do
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000027 before(:each) do
28 @socket = Socket.new
29 @handle = mock("Handle", :closed? => false)
30 @handle.stub!(:close)
Kevin Clark36376452009-03-07 00:03:15 +000031 @handle.stub!(:connect_nonblock)
32 ::Socket.stub!(:new).and_return(@handle)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000033 end
34
35 it_should_behave_like "a socket"
36
37 it "should raise a TransportException when it cannot open a socket" do
Kevin Clark36376452009-03-07 00:03:15 +000038 ::Socket.should_receive(:new).and_raise(StandardError)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000039 lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
40 end
41
Kevin Clark36376452009-03-07 00:03:15 +000042 it "should open a ::Socket with default args" do
43 ::Socket.should_receive(:new).and_return(mock("Handle", :connect_nonblock => true))
44 ::Socket.should_receive(:getaddrinfo).with("localhost", 9090).and_return([[]])
45 ::Socket.should_receive(:sockaddr_in)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000046 @socket.open
Kevin Clark3c871292008-06-18 01:12:05 +000047 end
48
49 it "should accept host/port options" do
Kevin Clark36376452009-03-07 00:03:15 +000050 ::Socket.should_receive(:new).and_return(mock("Handle", :connect_nonblock => true))
51 ::Socket.should_receive(:getaddrinfo).with("my.domain", 1234).and_return([[]])
52 ::Socket.should_receive(:sockaddr_in)
Kevin Clark3c871292008-06-18 01:12:05 +000053 Socket.new('my.domain', 1234).open
54 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000055
56 it "should accept an optional timeout" do
Kevin Clark36376452009-03-07 00:03:15 +000057 ::Socket.stub!(:new)
Kevin Clark5ae0bab2008-07-18 21:49:50 +000058 Socket.new('localhost', 8080, 5).timeout.should == 5
59 end
Kevin Clark3c871292008-06-18 01:12:05 +000060 end
61
62 describe ServerSocket do
63 before(:each) do
64 @socket = ServerSocket.new(1234)
65 end
66
67 it "should create a handle when calling listen" do
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000068 TCPServer.should_receive(:new).with(nil, 1234)
Kevin Clark3c871292008-06-18 01:12:05 +000069 @socket.listen
70 end
71
Kevin Clarkc6758702008-06-18 01:15:45 +000072 it "should accept an optional host argument" do
73 @socket = ServerSocket.new('localhost', 1234)
74 TCPServer.should_receive(:new).with('localhost', 1234)
75 @socket.listen
76 end
77
Kevin Clark3c871292008-06-18 01:12:05 +000078 it "should create a Thrift::Socket to wrap accepted sockets" do
79 handle = mock("TCPServer")
80 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
81 @socket.listen
82 sock = mock("sock")
83 handle.should_receive(:accept).and_return(sock)
84 trans = mock("Socket")
85 Socket.should_receive(:new).and_return(trans)
Kevin Clark6c30dbb2008-06-18 01:15:25 +000086 trans.should_receive(:handle=).with(sock)
Kevin Clark3c871292008-06-18 01:12:05 +000087 @socket.accept.should == trans
88 end
89
90 it "should close the handle when closed" do
Kevin Clark10674252008-06-18 01:17:30 +000091 handle = mock("TCPServer", :closed? => false)
Kevin Clark3c871292008-06-18 01:12:05 +000092 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
93 @socket.listen
94 handle.should_receive(:close)
95 @socket.close
96 end
Kevin Clark0d6007c2008-06-18 01:13:27 +000097
98 it "should return nil when accepting if there is no handle" do
99 @socket.accept.should be_nil
100 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +0000101
102 it "should return true for closed? when appropriate" do
103 handle = mock("TCPServer", :closed? => false)
104 TCPServer.stub!(:new).and_return(handle)
105 @socket.listen
106 @socket.should_not be_closed
107 handle.stub!(:close)
108 @socket.close
109 @socket.should be_closed
110 @socket.listen
111 @socket.should_not be_closed
112 handle.stub!(:closed?).and_return(true)
113 @socket.should be_closed
114 end
Kevin Clark3c871292008-06-18 01:12:05 +0000115 end
116end