blob: ec9e550056c3be9d7f9f8323a7ff76c757e45a70 [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
James E. King III27247072018-03-22 20:50:23 -040031 expect(TCPServer).to receive(:new).with(nil, 1234)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000032 @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)
James E. King III27247072018-03-22 20:50:23 -040037 expect(TCPServer).to receive(:new).with('localhost', 1234)
James E. King III9aaf2952018-03-20 15:06:08 -040038 @socket.to_s == "server(localhost:1234)"
Bryan Duxburyd1d15422009-04-04 00:58:03 +000039 @socket.listen
40 end
41
42 it "should create a Thrift::Socket to wrap accepted sockets" do
James E. King III27247072018-03-22 20:50:23 -040043 handle = double("TCPServer")
44 expect(TCPServer).to receive(:new).with(nil, 1234).and_return(handle)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000045 @socket.listen
James E. King III27247072018-03-22 20:50:23 -040046 sock = double("sock")
47 expect(handle).to receive(:accept).and_return(sock)
48 trans = double("Socket")
49 expect(Thrift::Socket).to receive(:new).and_return(trans)
50 expect(trans).to receive(:handle=).with(sock)
51 expect(@socket.accept).to eq(trans)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000052 end
53
54 it "should close the handle when closed" do
James E. King III27247072018-03-22 20:50:23 -040055 handle = double("TCPServer", :closed? => false)
56 expect(TCPServer).to receive(:new).with(nil, 1234).and_return(handle)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000057 @socket.listen
James E. King III27247072018-03-22 20:50:23 -040058 expect(handle).to receive(:close)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000059 @socket.close
60 end
61
62 it "should return nil when accepting if there is no handle" do
James E. King III27247072018-03-22 20:50:23 -040063 expect(@socket.accept).to be_nil
Bryan Duxburyd1d15422009-04-04 00:58:03 +000064 end
65
66 it "should return true for closed? when appropriate" do
James E. King III27247072018-03-22 20:50:23 -040067 handle = double("TCPServer", :closed? => false)
68 allow(TCPServer).to receive(:new).and_return(handle)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000069 @socket.listen
James E. King III27247072018-03-22 20:50:23 -040070 expect(@socket).not_to be_closed
71 allow(handle).to receive(:close)
Bryan Duxburyd1d15422009-04-04 00:58:03 +000072 @socket.close
James E. King III27247072018-03-22 20:50:23 -040073 expect(@socket).to be_closed
Bryan Duxburyd1d15422009-04-04 00:58:03 +000074 @socket.listen
James E. King III27247072018-03-22 20:50:23 -040075 expect(@socket).not_to be_closed
76 allow(handle).to receive(:closed?).and_return(true)
77 expect(@socket).to be_closed
Bryan Duxburyd1d15422009-04-04 00:58:03 +000078 end
James E. King III9aaf2952018-03-20 15:06:08 -040079
80 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -040081 expect(@socket.to_s).to eq("socket(:1234)")
James E. King III9aaf2952018-03-20 15:06:08 -040082 end
Bryan Duxburyd1d15422009-04-04 00:58:03 +000083 end
84end