| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 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 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 20 | require 'spec_helper' |
| Jake Farrell | 8941458 | 2011-11-10 00:53:17 +0000 | [diff] [blame] | 21 | require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared") |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 22 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe 'Thrift::ServerSocket' do |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 24 | |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 25 | describe Thrift::ServerSocket do |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 26 | before(:each) do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 27 | @socket = Thrift::ServerSocket.new(1234) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 28 | end |
| 29 | |
| 30 | it "should create a handle when calling listen" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 31 | expect(TCPServer).to receive(:new).with(nil, 1234) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 32 | @socket.listen |
| 33 | end |
| 34 | |
| 35 | it "should accept an optional host argument" do |
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 36 | @socket = Thrift::ServerSocket.new('localhost', 1234) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 37 | expect(TCPServer).to receive(:new).with('localhost', 1234) |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 38 | @socket.to_s == "server(localhost:1234)" |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 39 | @socket.listen |
| 40 | end |
| 41 | |
| 42 | it "should create a Thrift::Socket to wrap accepted sockets" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 43 | handle = double("TCPServer") |
| 44 | expect(TCPServer).to receive(:new).with(nil, 1234).and_return(handle) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 45 | @socket.listen |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 46 | sock = double("sock") |
| Dmytro Shteflyuk | 10d5a65 | 2025-11-19 15:24:32 -0500 | [diff] [blame] | 47 | expect(sock).to receive(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 48 | expect(handle).to receive(:accept).and_return(sock) |
| 49 | trans = double("Socket") |
| 50 | expect(Thrift::Socket).to receive(:new).and_return(trans) |
| 51 | expect(trans).to receive(:handle=).with(sock) |
| 52 | expect(@socket.accept).to eq(trans) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 53 | end |
| 54 | |
| 55 | it "should close the handle when closed" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 56 | handle = double("TCPServer", :closed? => false) |
| 57 | expect(TCPServer).to receive(:new).with(nil, 1234).and_return(handle) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 58 | @socket.listen |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 59 | expect(handle).to receive(:close) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 60 | @socket.close |
| 61 | end |
| 62 | |
| 63 | it "should return nil when accepting if there is no handle" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 64 | expect(@socket.accept).to be_nil |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 65 | end |
| 66 | |
| 67 | it "should return true for closed? when appropriate" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 68 | handle = double("TCPServer", :closed? => false) |
| 69 | allow(TCPServer).to receive(:new).and_return(handle) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 70 | @socket.listen |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 71 | expect(@socket).not_to be_closed |
| 72 | allow(handle).to receive(:close) |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 73 | @socket.close |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 74 | expect(@socket).to be_closed |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 75 | @socket.listen |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 76 | expect(@socket).not_to be_closed |
| 77 | allow(handle).to receive(:closed?).and_return(true) |
| 78 | expect(@socket).to be_closed |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 79 | end |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 80 | |
| 81 | it "should provide a reasonable to_s" do |
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 82 | expect(@socket.to_s).to eq("socket(:1234)") |
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 83 | end |
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 84 | end |
| 85 | end |