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