blob: 0bea8294789d8d74985921bd08ee5897d9c349a5 [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
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000024
Jake Farrella87810f2012-09-28 01:59:04 +000025 describe Thrift::UNIXSocket do
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000026 before(:each) do
27 @path = '/tmp/thrift_spec_socket'
Jake Farrella87810f2012-09-28 01:59:04 +000028 @socket = Thrift::UNIXSocket.new(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000029 @handle = mock("Handle", :closed? => false)
30 @handle.stub!(:close)
31 ::UNIXSocket.stub!(:new).and_return(@handle)
32 end
33
34 it_should_behave_like "a socket"
35
36 it "should raise a TransportException when it cannot open a socket" do
37 ::UNIXSocket.should_receive(:new).and_raise(StandardError)
38 lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
39 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000040
41 it "should accept an optional timeout" do
42 ::UNIXSocket.stub!(:new)
Jake Farrella87810f2012-09-28 01:59:04 +000043 Thrift::UNIXSocket.new(@path, 5).timeout.should == 5
Kevin Clark5ae0bab2008-07-18 21:49:50 +000044 end
James E. King III9aaf2952018-03-20 15:06:08 -040045
46 it "should provide a reasonable to_s" do
47 ::UNIXSocket.stub!(:new)
48 Thrift::UNIXSocket.new(@path).to_s.should == "domain(#{@path})"
49 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000050 end
51
Jake Farrella87810f2012-09-28 01:59:04 +000052 describe Thrift::UNIXServerSocket do
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000053 before(:each) do
54 @path = '/tmp/thrift_spec_socket'
Jake Farrella87810f2012-09-28 01:59:04 +000055 @socket = Thrift::UNIXServerSocket.new(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000056 end
57
58 it "should create a handle when calling listen" do
59 UNIXServer.should_receive(:new).with(@path)
60 @socket.listen
61 end
62
63 it "should create a Thrift::UNIXSocket to wrap accepted sockets" do
64 handle = mock("UNIXServer")
65 UNIXServer.should_receive(:new).with(@path).and_return(handle)
66 @socket.listen
67 sock = mock("sock")
68 handle.should_receive(:accept).and_return(sock)
69 trans = mock("UNIXSocket")
Jake Farrella87810f2012-09-28 01:59:04 +000070 Thrift::UNIXSocket.should_receive(:new).and_return(trans)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000071 trans.should_receive(:handle=).with(sock)
72 @socket.accept.should == trans
73 end
74
75 it "should close the handle when closed" do
76 handle = mock("UNIXServer", :closed? => false)
77 UNIXServer.should_receive(:new).with(@path).and_return(handle)
78 @socket.listen
79 handle.should_receive(:close)
Kevin Clark1aca9c42008-06-18 01:18:57 +000080 File.stub!(:delete)
81 @socket.close
82 end
83
84 it "should delete the socket when closed" do
85 handle = mock("UNIXServer", :closed? => false)
86 UNIXServer.should_receive(:new).with(@path).and_return(handle)
87 @socket.listen
88 handle.stub!(:close)
89 File.should_receive(:delete).with(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000090 @socket.close
91 end
92
93 it "should return nil when accepting if there is no handle" do
94 @socket.accept.should be_nil
95 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +000096
97 it "should return true for closed? when appropriate" do
98 handle = mock("UNIXServer", :closed? => false)
99 UNIXServer.stub!(:new).and_return(handle)
100 File.stub!(:delete)
101 @socket.listen
102 @socket.should_not be_closed
103 handle.stub!(:close)
104 @socket.close
105 @socket.should be_closed
106 @socket.listen
107 @socket.should_not be_closed
108 handle.stub!(:closed?).and_return(true)
109 @socket.should be_closed
110 end
James E. King III9aaf2952018-03-20 15:06:08 -0400111
112 it "should provide a reasonable to_s" do
113 @socket.to_s.should == "domain(#{@path})"
114 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000115 end
116end