David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 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 Farrell | 8941458 | 2011-11-10 00:53:17 +0000 | [diff] [blame^] | 20 | require File.expand_path("#{File.dirname(__FILE__)}/spec_helper") |
| 21 | require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared") |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 22 | |
| 23 | class ThriftUNIXSocketSpec < Spec::ExampleGroup |
| 24 | include Thrift |
| 25 | |
| 26 | describe UNIXSocket do |
| 27 | before(:each) do |
| 28 | @path = '/tmp/thrift_spec_socket' |
| 29 | @socket = UNIXSocket.new(@path) |
| 30 | @handle = mock("Handle", :closed? => false) |
| 31 | @handle.stub!(:close) |
| 32 | ::UNIXSocket.stub!(:new).and_return(@handle) |
| 33 | end |
| 34 | |
| 35 | it_should_behave_like "a socket" |
| 36 | |
| 37 | it "should raise a TransportException when it cannot open a socket" do |
| 38 | ::UNIXSocket.should_receive(:new).and_raise(StandardError) |
| 39 | lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN } |
| 40 | end |
Kevin Clark | 5ae0bab | 2008-07-18 21:49:50 +0000 | [diff] [blame] | 41 | |
| 42 | it "should accept an optional timeout" do |
| 43 | ::UNIXSocket.stub!(:new) |
| 44 | UNIXSocket.new(@path, 5).timeout.should == 5 |
| 45 | end |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 46 | end |
| 47 | |
| 48 | describe UNIXServerSocket do |
| 49 | before(:each) do |
| 50 | @path = '/tmp/thrift_spec_socket' |
| 51 | @socket = UNIXServerSocket.new(@path) |
| 52 | end |
| 53 | |
| 54 | it "should create a handle when calling listen" do |
| 55 | UNIXServer.should_receive(:new).with(@path) |
| 56 | @socket.listen |
| 57 | end |
| 58 | |
| 59 | it "should create a Thrift::UNIXSocket to wrap accepted sockets" do |
| 60 | handle = mock("UNIXServer") |
| 61 | UNIXServer.should_receive(:new).with(@path).and_return(handle) |
| 62 | @socket.listen |
| 63 | sock = mock("sock") |
| 64 | handle.should_receive(:accept).and_return(sock) |
| 65 | trans = mock("UNIXSocket") |
| 66 | UNIXSocket.should_receive(:new).and_return(trans) |
| 67 | trans.should_receive(:handle=).with(sock) |
| 68 | @socket.accept.should == trans |
| 69 | end |
| 70 | |
| 71 | it "should close the handle when closed" do |
| 72 | handle = mock("UNIXServer", :closed? => false) |
| 73 | UNIXServer.should_receive(:new).with(@path).and_return(handle) |
| 74 | @socket.listen |
| 75 | handle.should_receive(:close) |
Kevin Clark | 1aca9c4 | 2008-06-18 01:18:57 +0000 | [diff] [blame] | 76 | File.stub!(:delete) |
| 77 | @socket.close |
| 78 | end |
| 79 | |
| 80 | it "should delete the socket when closed" do |
| 81 | handle = mock("UNIXServer", :closed? => false) |
| 82 | UNIXServer.should_receive(:new).with(@path).and_return(handle) |
| 83 | @socket.listen |
| 84 | handle.stub!(:close) |
| 85 | File.should_receive(:delete).with(@path) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 86 | @socket.close |
| 87 | end |
| 88 | |
| 89 | it "should return nil when accepting if there is no handle" do |
| 90 | @socket.accept.should be_nil |
| 91 | end |
Kevin Clark | f8ecb02 | 2008-06-18 01:20:22 +0000 | [diff] [blame] | 92 | |
| 93 | it "should return true for closed? when appropriate" do |
| 94 | handle = mock("UNIXServer", :closed? => false) |
| 95 | UNIXServer.stub!(:new).and_return(handle) |
| 96 | File.stub!(:delete) |
| 97 | @socket.listen |
| 98 | @socket.should_not be_closed |
| 99 | handle.stub!(:close) |
| 100 | @socket.close |
| 101 | @socket.should be_closed |
| 102 | @socket.listen |
| 103 | @socket.should_not be_closed |
| 104 | handle.stub!(:closed?).and_return(true) |
| 105 | @socket.should be_closed |
| 106 | end |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 107 | end |
| 108 | end |