blob: 32bdb71f0adfff494520465a2e59cb10386511cc [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
James E. King III27247072018-03-22 20:50:23 -040024 expect(@socket.open).to eq(@handle)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000025 end
26
27 it "should be open whenever it has a handle" do
James E. King III27247072018-03-22 20:50:23 -040028 expect(@socket).not_to be_open
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000029 @socket.open
James E. King III27247072018-03-22 20:50:23 -040030 expect(@socket).to be_open
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000031 @socket.handle = nil
James E. King III27247072018-03-22 20:50:23 -040032 expect(@socket).not_to be_open
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000033 @socket.handle = @handle
34 @socket.close
James E. King III27247072018-03-22 20:50:23 -040035 expect(@socket).not_to be_open
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000036 end
37
38 it "should write data to the handle" do
39 @socket.open
James E. King III27247072018-03-22 20:50:23 -040040 expect(@handle).to receive(:write).with("foobar")
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000041 @socket.write("foobar")
James E. King III27247072018-03-22 20:50:23 -040042 expect(@handle).to receive(:write).with("fail").and_raise(StandardError)
43 expect { @socket.write("fail") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000044 end
45
46 it "should raise an error when it cannot read from the handle" do
47 @socket.open
James E. King III27247072018-03-22 20:50:23 -040048 expect(@handle).to receive(:readpartial).with(17).and_raise(StandardError)
49 expect { @socket.read(17) }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000050 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
James E. King III27247072018-03-22 20:50:23 -040054 expect(@handle).to receive(:readpartial).with(17).and_return("test data")
55 expect(@socket.read(17)).to eq("test data")
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000056 end
57
58 it "should declare itself as closed when it has an error" do
59 @socket.open
James E. King III27247072018-03-22 20:50:23 -040060 expect(@handle).to receive(:write).with("fail").and_raise(StandardError)
61 expect(@socket).to be_open
62 expect { @socket.write("fail") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
63 expect(@socket).not_to be_open
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000064 end
Kevin Clark5da153b2008-06-26 18:35:15 +000065
66 it "should raise an error when the stream is closed" do
67 @socket.open
James E. King III27247072018-03-22 20:50:23 -040068 allow(@handle).to receive(:closed?).and_return(true)
69 expect(@socket).not_to be_open
70 expect { @socket.write("fail") }.to raise_error(IOError, "closed stream")
71 expect { @socket.read(10) }.to raise_error(IOError, "closed stream")
Kevin Clark5da153b2008-06-26 18:35:15 +000072 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
James E. King III27247072018-03-22 20:50:23 -040077 expect(IO).to receive(:select).with([@handle], nil, nil, 3).and_return([[@handle], [], []])
78 expect(@handle).to receive(:readpartial).with(17).and_return("test data")
79 expect(@socket.read(17)).to eq("test data")
Kevin Clark5ae0bab2008-07-18 21:49:50 +000080 end
81
82 it "should support the timeout accessor for write" do
83 @socket.timeout = 3
84 @socket.open
James E. King III27247072018-03-22 20:50:23 -040085 expect(IO).to receive(:select).with(nil, [@handle], nil, 3).twice.and_return([[], [@handle], []])
86 expect(@handle).to receive(:write_nonblock).with("test data").and_return(4)
87 expect(@handle).to receive(:write_nonblock).with(" data").and_return(5)
88 expect(@socket.write("test data")).to eq(9)
Kevin Clark5ae0bab2008-07-18 21:49:50 +000089 end
90
91 it "should raise an error when read times out" do
92 @socket.timeout = 0.5
93 @socket.open
James E. King III27247072018-03-22 20:50:23 -040094 expect(IO).to receive(:select).once {sleep(0.5); nil}
95 expect { @socket.read(17) }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::TIMED_OUT) }
Kevin Clark5ae0bab2008-07-18 21:49:50 +000096 end
97
98 it "should raise an error when write times out" do
99 @socket.timeout = 0.5
100 @socket.open
James E. King III27247072018-03-22 20:50:23 -0400101 allow(IO).to receive(:select).with(nil, [@handle], nil, 0.5).and_return(nil)
102 expect { @socket.write("test data") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::TIMED_OUT) }
Kevin Clark5ae0bab2008-07-18 21:49:50 +0000103 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000104end