David Reiss | ea2cba8 | 2009-03-30 21:35:00 +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") |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 22 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe 'UNIXSocket' do |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 24 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 25 | describe Thrift::UNIXSocket do |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 26 | before(:each) do |
| 27 | @path = '/tmp/thrift_spec_socket' |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 28 | @socket = Thrift::UNIXSocket.new(@path) |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 29 | @handle = double("Handle", :closed? => false) |
| 30 | allow(@handle).to receive(:close) |
| 31 | allow(::UNIXSocket).to receive(:new).and_return(@handle) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 32 | end |
| 33 | |
| 34 | it_should_behave_like "a socket" |
| 35 | |
| 36 | it "should raise a TransportException when it cannot open a socket" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 37 | expect(::UNIXSocket).to receive(:new).and_raise(StandardError) |
| 38 | expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) } |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 39 | end |
Kevin Clark | 5ae0bab | 2008-07-18 21:49:50 +0000 | [diff] [blame] | 40 | |
| 41 | it "should accept an optional timeout" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 42 | allow(::UNIXSocket).to receive(:new) |
| 43 | expect(Thrift::UNIXSocket.new(@path, 5).timeout).to eq(5) |
Kevin Clark | 5ae0bab | 2008-07-18 21:49:50 +0000 | [diff] [blame] | 44 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 45 | |
| 46 | it "should provide a reasonable to_s" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 47 | allow(::UNIXSocket).to receive(:new) |
| 48 | expect(Thrift::UNIXSocket.new(@path).to_s).to eq("domain(#{@path})") |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 49 | end |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 50 | end |
| 51 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 52 | describe Thrift::UNIXServerSocket do |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 53 | before(:each) do |
| 54 | @path = '/tmp/thrift_spec_socket' |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 55 | @socket = Thrift::UNIXServerSocket.new(@path) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 56 | end |
| 57 | |
| 58 | it "should create a handle when calling listen" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 59 | expect(UNIXServer).to receive(:new).with(@path) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 60 | @socket.listen |
| 61 | end |
| 62 | |
| 63 | it "should create a Thrift::UNIXSocket to wrap accepted sockets" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 64 | handle = double("UNIXServer") |
| 65 | expect(UNIXServer).to receive(:new).with(@path).and_return(handle) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 66 | @socket.listen |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 67 | sock = double("sock") |
| 68 | expect(handle).to receive(:accept).and_return(sock) |
| 69 | trans = double("UNIXSocket") |
| 70 | expect(Thrift::UNIXSocket).to receive(:new).and_return(trans) |
| 71 | expect(trans).to receive(:handle=).with(sock) |
| 72 | expect(@socket.accept).to eq(trans) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 73 | end |
| 74 | |
| 75 | it "should close the handle when closed" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 76 | handle = double("UNIXServer", :closed? => false) |
| 77 | expect(UNIXServer).to receive(:new).with(@path).and_return(handle) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 78 | @socket.listen |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 79 | expect(handle).to receive(:close) |
| 80 | allow(File).to receive(:delete) |
Kevin Clark | 1aca9c4 | 2008-06-18 01:18:57 +0000 | [diff] [blame] | 81 | @socket.close |
| 82 | end |
| 83 | |
| 84 | it "should delete the socket when closed" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 85 | handle = double("UNIXServer", :closed? => false) |
| 86 | expect(UNIXServer).to receive(:new).with(@path).and_return(handle) |
Kevin Clark | 1aca9c4 | 2008-06-18 01:18:57 +0000 | [diff] [blame] | 87 | @socket.listen |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 88 | allow(handle).to receive(:close) |
| 89 | expect(File).to receive(:delete).with(@path) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 90 | @socket.close |
| 91 | end |
| 92 | |
| 93 | 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] | 94 | expect(@socket.accept).to be_nil |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 95 | end |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 96 | |
| 97 | it "should return true for closed? when appropriate" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 98 | handle = double("UNIXServer", :closed? => false) |
| 99 | allow(UNIXServer).to receive(:new).and_return(handle) |
| 100 | allow(File).to receive(:delete) |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 101 | @socket.listen |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 102 | expect(@socket).not_to be_closed |
| 103 | allow(handle).to receive(:close) |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 104 | @socket.close |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 105 | expect(@socket).to be_closed |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 106 | @socket.listen |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 107 | expect(@socket).not_to be_closed |
| 108 | allow(handle).to receive(:closed?).and_return(true) |
| 109 | expect(@socket).to be_closed |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 110 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 111 | |
| 112 | it "should provide a reasonable to_s" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 113 | expect(@socket.to_s).to eq("domain(#{@path})") |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 114 | end |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 115 | end |
| 116 | end |