blob: 222a1c796d8017601bd7b568391c28914808f58d [file] [log] [blame]
Kevin Clark2ddd8ed2008-06-18 01:18:35 +00001require File.dirname(__FILE__) + '/spec_helper'
2require 'thrift/transport/unixsocket'
3require File.dirname(__FILE__) + "/socket_spec_shared"
4
5class ThriftUNIXSocketSpec < Spec::ExampleGroup
6 include Thrift
7
8 describe UNIXSocket do
9 before(:each) do
10 @path = '/tmp/thrift_spec_socket'
11 @socket = UNIXSocket.new(@path)
12 @handle = mock("Handle", :closed? => false)
13 @handle.stub!(:close)
14 ::UNIXSocket.stub!(:new).and_return(@handle)
15 end
16
17 it_should_behave_like "a socket"
18
19 it "should raise a TransportException when it cannot open a socket" do
20 ::UNIXSocket.should_receive(:new).and_raise(StandardError)
21 lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
22 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000023
24 it "should accept an optional timeout" do
25 ::UNIXSocket.stub!(:new)
26 UNIXSocket.new(@path, 5).timeout.should == 5
27 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000028 end
29
30 describe UNIXServerSocket do
31 before(:each) do
32 @path = '/tmp/thrift_spec_socket'
33 @socket = UNIXServerSocket.new(@path)
34 end
35
36 it "should create a handle when calling listen" do
37 UNIXServer.should_receive(:new).with(@path)
38 @socket.listen
39 end
40
41 it "should create a Thrift::UNIXSocket to wrap accepted sockets" do
42 handle = mock("UNIXServer")
43 UNIXServer.should_receive(:new).with(@path).and_return(handle)
44 @socket.listen
45 sock = mock("sock")
46 handle.should_receive(:accept).and_return(sock)
47 trans = mock("UNIXSocket")
48 UNIXSocket.should_receive(:new).and_return(trans)
49 trans.should_receive(:handle=).with(sock)
50 @socket.accept.should == trans
51 end
52
53 it "should close the handle when closed" do
54 handle = mock("UNIXServer", :closed? => false)
55 UNIXServer.should_receive(:new).with(@path).and_return(handle)
56 @socket.listen
57 handle.should_receive(:close)
Kevin Clark1aca9c42008-06-18 01:18:57 +000058 File.stub!(:delete)
59 @socket.close
60 end
61
62 it "should delete the socket when closed" do
63 handle = mock("UNIXServer", :closed? => false)
64 UNIXServer.should_receive(:new).with(@path).and_return(handle)
65 @socket.listen
66 handle.stub!(:close)
67 File.should_receive(:delete).with(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000068 @socket.close
69 end
70
71 it "should return nil when accepting if there is no handle" do
72 @socket.accept.should be_nil
73 end
Kevin Clarkf8ecb022008-06-18 01:20:22 +000074
75 it "should return true for closed? when appropriate" do
76 handle = mock("UNIXServer", :closed? => false)
77 UNIXServer.stub!(:new).and_return(handle)
78 File.stub!(:delete)
79 @socket.listen
80 @socket.should_not be_closed
81 handle.stub!(:close)
82 @socket.close
83 @socket.should be_closed
84 @socket.listen
85 @socket.should_not be_closed
86 handle.stub!(:closed?).and_return(true)
87 @socket.should be_closed
88 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000089 end
90end