blob: 1301d540f480e4900d80d5629637b0250a160951 [file] [log] [blame]
Bryan Duxburyd1d15422009-04-04 00:58:03 +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
Jake Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Jake Farrell89414582011-11-10 00:53:17 +000021require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
Bryan Duxburyd1d15422009-04-04 00:58:03 +000022
Jake Farrella87810f2012-09-28 01:59:04 +000023describe 'Thrift::ServerSocket' do
Bryan Duxburyd1d15422009-04-04 00:58:03 +000024
Jake Farrella87810f2012-09-28 01:59:04 +000025 describe Thrift::ServerSocket do
Bryan Duxburyd1d15422009-04-04 00:58:03 +000026 before(:each) do
Jake Farrella87810f2012-09-28 01:59:04 +000027 @socket = Thrift::ServerSocket.new(1234)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000028 end
29
30 it "should create a handle when calling listen" do
31 TCPServer.should_receive(:new).with(nil, 1234)
32 @socket.listen
33 end
34
35 it "should accept an optional host argument" do
Jake Farrella87810f2012-09-28 01:59:04 +000036 @socket = Thrift::ServerSocket.new('localhost', 1234)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000037 TCPServer.should_receive(:new).with('localhost', 1234)
38 @socket.listen
39 end
40
41 it "should create a Thrift::Socket to wrap accepted sockets" do
42 handle = mock("TCPServer")
43 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
44 @socket.listen
45 sock = mock("sock")
46 handle.should_receive(:accept).and_return(sock)
47 trans = mock("Socket")
Jake Farrella87810f2012-09-28 01:59:04 +000048 Thrift::Socket.should_receive(:new).and_return(trans)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000049 trans.should_receive(:handle=).with(sock)
50 @socket.accept.should == trans
51 end
52
53 it "should close the handle when closed" do
54 handle = mock("TCPServer", :closed? => false)
55 TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
56 @socket.listen
57 handle.should_receive(:close)
58 @socket.close
59 end
60
61 it "should return nil when accepting if there is no handle" do
62 @socket.accept.should be_nil
63 end
64
65 it "should return true for closed? when appropriate" do
66 handle = mock("TCPServer", :closed? => false)
67 TCPServer.stub!(:new).and_return(handle)
68 @socket.listen
69 @socket.should_not be_closed
70 handle.stub!(:close)
71 @socket.close
72 @socket.should be_closed
73 @socket.listen
74 @socket.should_not be_closed
75 handle.stub!(:closed?).and_return(true)
76 @socket.should be_closed
77 end
78 end
79end