blob: 574e2bbc94d204ddac13407b652798d76f089e07 [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
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000020require File.dirname(__FILE__) + '/spec_helper'
21require 'thrift/transport/unixsocket'
22require File.dirname(__FILE__) + "/socket_spec_shared"
23
24class ThriftUNIXSocketSpec < Spec::ExampleGroup
25 include Thrift
26
27 describe UNIXSocket do
28 before(:each) do
29 @path = '/tmp/thrift_spec_socket'
30 @socket = UNIXSocket.new(@path)
31 @handle = mock("Handle", :closed? => false)
32 @handle.stub!(:close)
33 ::UNIXSocket.stub!(:new).and_return(@handle)
34 end
35
36 it_should_behave_like "a socket"
37
38 it "should raise a TransportException when it cannot open a socket" do
39 ::UNIXSocket.should_receive(:new).and_raise(StandardError)
40 lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
41 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000042
43 it "should accept an optional timeout" do
44 ::UNIXSocket.stub!(:new)
45 UNIXSocket.new(@path, 5).timeout.should == 5
46 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000047 end
48
49 describe UNIXServerSocket do
50 before(:each) do
51 @path = '/tmp/thrift_spec_socket'
52 @socket = UNIXServerSocket.new(@path)
53 end
54
55 it "should create a handle when calling listen" do
56 UNIXServer.should_receive(:new).with(@path)
57 @socket.listen
58 end
59
60 it "should create a Thrift::UNIXSocket to wrap accepted sockets" do
61 handle = mock("UNIXServer")
62 UNIXServer.should_receive(:new).with(@path).and_return(handle)
63 @socket.listen
64 sock = mock("sock")
65 handle.should_receive(:accept).and_return(sock)
66 trans = mock("UNIXSocket")
67 UNIXSocket.should_receive(:new).and_return(trans)
68 trans.should_receive(:handle=).with(sock)
69 @socket.accept.should == trans
70 end
71
72 it "should close the handle when closed" do
73 handle = mock("UNIXServer", :closed? => false)
74 UNIXServer.should_receive(:new).with(@path).and_return(handle)
75 @socket.listen
76 handle.should_receive(:close)
Kevin Clark1aca9c42008-06-18 01:18:57 +000077 File.stub!(:delete)
78 @socket.close
79 end
80
81 it "should delete the socket when closed" do
82 handle = mock("UNIXServer", :closed? => false)
83 UNIXServer.should_receive(:new).with(@path).and_return(handle)
84 @socket.listen
85 handle.stub!(:close)
86 File.should_receive(:delete).with(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000087 @socket.close
88 end
89
90 it "should return nil when accepting if there is no handle" do
91 @socket.accept.should be_nil
92 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +000093
94 it "should return true for closed? when appropriate" do
95 handle = mock("UNIXServer", :closed? => false)
96 UNIXServer.stub!(:new).and_return(handle)
97 File.stub!(:delete)
98 @socket.listen
99 @socket.should_not be_closed
100 handle.stub!(:close)
101 @socket.close
102 @socket.should be_closed
103 @socket.listen
104 @socket.should_not be_closed
105 handle.stub!(:closed?).and_return(true)
106 @socket.should be_closed
107 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000108 end
109end