blob: 5fddc16a77b93d545bf1115f33c6b9ba40f67cff [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001#
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 Farrella87810f2012-09-28 01:59:04 +000020require 'spec_helper'
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000021
22shared_examples_for "a socket" do
23 it "should open a socket" do
24 @socket.open.should == @handle
25 end
26
27 it "should be open whenever it has a handle" do
28 @socket.should_not be_open
29 @socket.open
30 @socket.should be_open
31 @socket.handle = nil
32 @socket.should_not be_open
33 @socket.handle = @handle
34 @socket.close
35 @socket.should_not be_open
36 end
37
38 it "should write data to the handle" do
39 @socket.open
40 @handle.should_receive(:write).with("foobar")
41 @socket.write("foobar")
42 @handle.should_receive(:write).with("fail").and_raise(StandardError)
43 lambda { @socket.write("fail") }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
44 end
45
46 it "should raise an error when it cannot read from the handle" do
47 @socket.open
Kevin Clark4bd89162008-07-08 00:47:49 +000048 @handle.should_receive(:readpartial).with(17).and_raise(StandardError)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000049 lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
50 end
51
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000052 it "should return the data read when reading from the handle works" do
53 @socket.open
Kevin Clark4bd89162008-07-08 00:47:49 +000054 @handle.should_receive(:readpartial).with(17).and_return("test data")
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000055 @socket.read(17).should == "test data"
56 end
57
58 it "should declare itself as closed when it has an error" do
59 @socket.open
60 @handle.should_receive(:write).with("fail").and_raise(StandardError)
61 @socket.should be_open
62 lambda { @socket.write("fail") }.should raise_error
63 @socket.should_not be_open
64 end
Kevin Clark5da153b2008-06-26 18:35:15 +000065
66 it "should raise an error when the stream is closed" do
67 @socket.open
68 @handle.stub!(:closed?).and_return(true)
69 @socket.should_not be_open
70 lambda { @socket.write("fail") }.should raise_error(IOError, "closed stream")
71 lambda { @socket.read(10) }.should raise_error(IOError, "closed stream")
72 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000073
74 it "should support the timeout accessor for read" do
75 @socket.timeout = 3
76 @socket.open
77 IO.should_receive(:select).with([@handle], nil, nil, 3).and_return([[@handle], [], []])
78 @handle.should_receive(:readpartial).with(17).and_return("test data")
79 @socket.read(17).should == "test data"
80 end
81
82 it "should support the timeout accessor for write" do
83 @socket.timeout = 3
84 @socket.open
85 IO.should_receive(:select).with(nil, [@handle], nil, 3).twice.and_return([[], [@handle], []])
86 @handle.should_receive(:write_nonblock).with("test data").and_return(4)
87 @handle.should_receive(:write_nonblock).with(" data").and_return(5)
88 @socket.write("test data").should == 9
89 end
90
91 it "should raise an error when read times out" do
92 @socket.timeout = 0.5
93 @socket.open
Bryan Duxbury83c47952010-09-17 20:17:21 +000094 IO.should_receive(:select).once {sleep(0.5); nil}
Kevin Clark5ae0bab2008-07-18 21:49:50 +000095 lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::TIMED_OUT }
96 end
97
98 it "should raise an error when write times out" do
99 @socket.timeout = 0.5
100 @socket.open
101 IO.should_receive(:select).with(nil, [@handle], nil, 0.5).any_number_of_times.and_return(nil)
102 lambda { @socket.write("test data") }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::TIMED_OUT }
103 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000104end