Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | require 'thrift/transport/unixsocket' |
| 3 | require File.dirname(__FILE__) + "/socket_spec_shared" |
| 4 | |
| 5 | class 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 Clark | 5ae0bab | 2008-07-18 21:49:50 +0000 | [diff] [blame] | 23 | |
| 24 | it "should accept an optional timeout" do |
| 25 | ::UNIXSocket.stub!(:new) |
| 26 | UNIXSocket.new(@path, 5).timeout.should == 5 |
| 27 | end |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 28 | 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 Clark | 1aca9c4 | 2008-06-18 01:18:57 +0000 | [diff] [blame] | 58 | 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 Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 68 | @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 Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 74 | |
| 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 Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 89 | end |
| 90 | end |