blob: 437ca83614115393ff58d6d067c53f5f60b2fe45 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +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")
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000022
Jake Farrella87810f2012-09-28 01:59:04 +000023describe 'UNIXSocket' do
Jake Farrella87810f2012-09-28 01:59:04 +000024 describe Thrift::UNIXSocket do
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000025 before(:each) do
26 @path = '/tmp/thrift_spec_socket'
Jake Farrella87810f2012-09-28 01:59:04 +000027 @socket = Thrift::UNIXSocket.new(@path)
James E. King III27247072018-03-22 20:50:23 -040028 @handle = double("Handle", :closed? => false)
29 allow(@handle).to receive(:close)
30 allow(::UNIXSocket).to receive(:new).and_return(@handle)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000031 end
32
33 it_should_behave_like "a socket"
34
35 it "should raise a TransportException when it cannot open a socket" do
James E. King III27247072018-03-22 20:50:23 -040036 expect(::UNIXSocket).to receive(:new).and_raise(StandardError)
37 expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000038 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000039
40 it "should accept an optional timeout" do
James E. King III27247072018-03-22 20:50:23 -040041 allow(::UNIXSocket).to receive(:new)
42 expect(Thrift::UNIXSocket.new(@path, 5).timeout).to eq(5)
Kevin Clark5ae0bab2008-07-18 21:49:50 +000043 end
Dmytro Shteflyukf5c80a42026-03-08 19:09:43 -040044
James E. King III9aaf2952018-03-20 15:06:08 -040045 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -040046 allow(::UNIXSocket).to receive(:new)
47 expect(Thrift::UNIXSocket.new(@path).to_s).to eq("domain(#{@path})")
James E. King III9aaf2952018-03-20 15:06:08 -040048 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000049 end
50
Jake Farrella87810f2012-09-28 01:59:04 +000051 describe Thrift::UNIXServerSocket do
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000052 before(:each) do
53 @path = '/tmp/thrift_spec_socket'
Jake Farrella87810f2012-09-28 01:59:04 +000054 @socket = Thrift::UNIXServerSocket.new(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000055 end
56
57 it "should create a handle when calling listen" do
James E. King III27247072018-03-22 20:50:23 -040058 expect(UNIXServer).to receive(:new).with(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000059 @socket.listen
60 end
61
62 it "should create a Thrift::UNIXSocket to wrap accepted sockets" do
James E. King III27247072018-03-22 20:50:23 -040063 handle = double("UNIXServer")
64 expect(UNIXServer).to receive(:new).with(@path).and_return(handle)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000065 @socket.listen
James E. King III27247072018-03-22 20:50:23 -040066 sock = double("sock")
67 expect(handle).to receive(:accept).and_return(sock)
68 trans = double("UNIXSocket")
69 expect(Thrift::UNIXSocket).to receive(:new).and_return(trans)
70 expect(trans).to receive(:handle=).with(sock)
71 expect(@socket.accept).to eq(trans)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000072 end
73
74 it "should close the handle when closed" do
James E. King III27247072018-03-22 20:50:23 -040075 handle = double("UNIXServer", :closed? => false)
76 expect(UNIXServer).to receive(:new).with(@path).and_return(handle)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000077 @socket.listen
James E. King III27247072018-03-22 20:50:23 -040078 expect(handle).to receive(:close)
79 allow(File).to receive(:delete)
Kevin Clark1aca9c42008-06-18 01:18:57 +000080 @socket.close
81 end
82
83 it "should delete the socket when closed" do
James E. King III27247072018-03-22 20:50:23 -040084 handle = double("UNIXServer", :closed? => false)
85 expect(UNIXServer).to receive(:new).with(@path).and_return(handle)
Kevin Clark1aca9c42008-06-18 01:18:57 +000086 @socket.listen
James E. King III27247072018-03-22 20:50:23 -040087 allow(handle).to receive(:close)
88 expect(File).to receive(:delete).with(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000089 @socket.close
90 end
91
92 it "should return nil when accepting if there is no handle" do
James E. King III27247072018-03-22 20:50:23 -040093 expect(@socket.accept).to be_nil
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000094 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +000095
96 it "should return true for closed? when appropriate" do
James E. King III27247072018-03-22 20:50:23 -040097 handle = double("UNIXServer", :closed? => false)
98 allow(UNIXServer).to receive(:new).and_return(handle)
99 allow(File).to receive(:delete)
Kevin Clarkf8ecb022008-06-18 01:20:22 +0000100 @socket.listen
James E. King III27247072018-03-22 20:50:23 -0400101 expect(@socket).not_to be_closed
102 allow(handle).to receive(:close)
Kevin Clarkf8ecb022008-06-18 01:20:22 +0000103 @socket.close
James E. King III27247072018-03-22 20:50:23 -0400104 expect(@socket).to be_closed
Kevin Clarkf8ecb022008-06-18 01:20:22 +0000105 @socket.listen
James E. King III27247072018-03-22 20:50:23 -0400106 expect(@socket).not_to be_closed
107 allow(handle).to receive(:closed?).and_return(true)
108 expect(@socket).to be_closed
Kevin Clarkf8ecb022008-06-18 01:20:22 +0000109 end
James E. King III9aaf2952018-03-20 15:06:08 -0400110
111 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -0400112 expect(@socket.to_s).to eq("domain(#{@path})")
James E. King III9aaf2952018-03-20 15:06:08 -0400113 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000114 end
115end