blob: abc166a6ba75ebf035a3d58c25bf005aac81d27c [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
23 end
24
25 describe UNIXServerSocket do
26 before(:each) do
27 @path = '/tmp/thrift_spec_socket'
28 @socket = UNIXServerSocket.new(@path)
29 end
30
31 it "should create a handle when calling listen" do
32 UNIXServer.should_receive(:new).with(@path)
33 @socket.listen
34 end
35
36 it "should create a Thrift::UNIXSocket to wrap accepted sockets" do
37 handle = mock("UNIXServer")
38 UNIXServer.should_receive(:new).with(@path).and_return(handle)
39 @socket.listen
40 sock = mock("sock")
41 handle.should_receive(:accept).and_return(sock)
42 trans = mock("UNIXSocket")
43 UNIXSocket.should_receive(:new).and_return(trans)
44 trans.should_receive(:handle=).with(sock)
45 @socket.accept.should == trans
46 end
47
48 it "should close the handle when closed" do
49 handle = mock("UNIXServer", :closed? => false)
50 UNIXServer.should_receive(:new).with(@path).and_return(handle)
51 @socket.listen
52 handle.should_receive(:close)
Kevin Clark1aca9c42008-06-18 01:18:57 +000053 File.stub!(:delete)
54 @socket.close
55 end
56
57 it "should delete the socket when closed" do
58 handle = mock("UNIXServer", :closed? => false)
59 UNIXServer.should_receive(:new).with(@path).and_return(handle)
60 @socket.listen
61 handle.stub!(:close)
62 File.should_receive(:delete).with(@path)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000063 @socket.close
64 end
65
66 it "should return nil when accepting if there is no handle" do
67 @socket.accept.should be_nil
68 end
69 end
70end