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 | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 22 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 23 | describe 'Socket' do |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 24 | |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 25 | describe Thrift::Socket do |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 26 | before(:each) do |
Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 27 | @socket = Thrift::Socket.new |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 28 | @handle = double("Handle", :closed? => false) |
| 29 | allow(@handle).to receive(:close) |
| 30 | allow(@handle).to receive(:connect_nonblock) |
| 31 | allow(@handle).to receive(:setsockopt) |
| 32 | allow(::Socket).to receive(:new).and_return(@handle) |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 33 | end |
| 34 | |
| 35 | it_should_behave_like "a socket" |
| 36 | |
| 37 | it "should raise a TransportException when it cannot open a socket" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 38 | expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]]) |
| 39 | expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) } |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 40 | end |
| 41 | |
Kevin Clark | 3637645 | 2009-03-07 00:03:15 +0000 | [diff] [blame] | 42 | it "should open a ::Socket with default args" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 43 | expect(::Socket).to receive(:new).and_return(double("Handle", :connect_nonblock => true, :setsockopt => nil)) |
| 44 | expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]]) |
| 45 | expect(::Socket).to receive(:sockaddr_in) |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 46 | @socket.to_s == "socket(localhost:9090)" |
Kevin Clark | 2ddd8ed | 2008-06-18 01:18:35 +0000 | [diff] [blame] | 47 | @socket.open |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 48 | end |
| 49 | |
| 50 | it "should accept host/port options" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 51 | expect(::Socket).to receive(:new).and_return(double("Handle", :connect_nonblock => true, :setsockopt => nil)) |
| 52 | expect(::Socket).to receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]]) |
| 53 | expect(::Socket).to receive(:sockaddr_in) |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 54 | @socket = Thrift::Socket.new('my.domain', 1234).open |
| 55 | @socket.to_s == "socket(my.domain:1234)" |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 56 | end |
Kevin Clark | 5ae0bab | 2008-07-18 21:49:50 +0000 | [diff] [blame] | 57 | |
| 58 | it "should accept an optional timeout" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 59 | allow(::Socket).to receive(:new) |
| 60 | expect(Thrift::Socket.new('localhost', 8080, 5).timeout).to eq(5) |
Kevin Clark | 5ae0bab | 2008-07-18 21:49:50 +0000 | [diff] [blame] | 61 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 62 | |
| 63 | it "should provide a reasonable to_s" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame^] | 64 | allow(::Socket).to receive(:new) |
| 65 | expect(Thrift::Socket.new('myhost', 8090).to_s).to eq("socket(myhost:8090)") |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 66 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 67 | end |
Kevin Clark | 3c87129 | 2008-06-18 01:12:05 +0000 | [diff] [blame] | 68 | end |