| 30 |
ashish |
1 |
#
|
|
|
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 |
|
|
|
20 |
require File.dirname(__FILE__) + '/spec_helper'
|
|
|
21 |
require File.dirname(__FILE__) + "/socket_spec_shared"
|
|
|
22 |
|
|
|
23 |
class ThriftServerSocketSpec < Spec::ExampleGroup
|
|
|
24 |
include Thrift
|
|
|
25 |
|
|
|
26 |
describe ServerSocket do
|
|
|
27 |
before(:each) do
|
|
|
28 |
@socket = ServerSocket.new(1234)
|
|
|
29 |
end
|
|
|
30 |
|
|
|
31 |
it "should create a handle when calling listen" do
|
|
|
32 |
TCPServer.should_receive(:new).with(nil, 1234)
|
|
|
33 |
@socket.listen
|
|
|
34 |
end
|
|
|
35 |
|
|
|
36 |
it "should accept an optional host argument" do
|
|
|
37 |
@socket = ServerSocket.new('localhost', 1234)
|
|
|
38 |
TCPServer.should_receive(:new).with('localhost', 1234)
|
|
|
39 |
@socket.listen
|
|
|
40 |
end
|
|
|
41 |
|
|
|
42 |
it "should create a Thrift::Socket to wrap accepted sockets" do
|
|
|
43 |
handle = mock("TCPServer")
|
|
|
44 |
TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
|
|
|
45 |
@socket.listen
|
|
|
46 |
sock = mock("sock")
|
|
|
47 |
handle.should_receive(:accept).and_return(sock)
|
|
|
48 |
trans = mock("Socket")
|
|
|
49 |
Socket.should_receive(:new).and_return(trans)
|
|
|
50 |
trans.should_receive(:handle=).with(sock)
|
|
|
51 |
@socket.accept.should == trans
|
|
|
52 |
end
|
|
|
53 |
|
|
|
54 |
it "should close the handle when closed" do
|
|
|
55 |
handle = mock("TCPServer", :closed? => false)
|
|
|
56 |
TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
|
|
|
57 |
@socket.listen
|
|
|
58 |
handle.should_receive(:close)
|
|
|
59 |
@socket.close
|
|
|
60 |
end
|
|
|
61 |
|
|
|
62 |
it "should return nil when accepting if there is no handle" do
|
|
|
63 |
@socket.accept.should be_nil
|
|
|
64 |
end
|
|
|
65 |
|
|
|
66 |
it "should return true for closed? when appropriate" do
|
|
|
67 |
handle = mock("TCPServer", :closed? => false)
|
|
|
68 |
TCPServer.stub!(:new).and_return(handle)
|
|
|
69 |
@socket.listen
|
|
|
70 |
@socket.should_not be_closed
|
|
|
71 |
handle.stub!(:close)
|
|
|
72 |
@socket.close
|
|
|
73 |
@socket.should be_closed
|
|
|
74 |
@socket.listen
|
|
|
75 |
@socket.should_not be_closed
|
|
|
76 |
handle.stub!(:closed?).and_return(true)
|
|
|
77 |
@socket.should be_closed
|
|
|
78 |
end
|
|
|
79 |
end
|
|
|
80 |
end
|