blob: 163d0c2ce73148216140ce431234da59c149bf34 [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
Dmytro Shteflyuk6e105bf2025-12-04 17:14:59 -050070 expect { @socket.write("fail") }.to raise_error(Thrift::TransportException, "closed stream") { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
71 expect { @socket.read(10) }.to raise_error(Thrift::TransportException, "closed stream") { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
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
Dmytro Shteflyuk8a358b82026-02-22 12:11:12 +053077 expect(@handle).to receive(:read_nonblock).with(17).and_raise(IO::EAGAINWaitReadable)
78 expect(IO).to receive(:select) do |rd, wr, err, timeout|
79 expect(rd).to eq([@handle])
80 expect(wr).to be_nil
81 expect(err).to be_nil
82 expect(timeout).to be > 0
83 expect(timeout).to be <= 3
84 [[@handle], [], []]
85 end
86 expect(@handle).to receive(:read_nonblock).with(17).and_return("test data")
James E. King III27247072018-03-22 20:50:23 -040087 expect(@socket.read(17)).to eq("test data")
Kevin Clark5ae0bab2008-07-18 21:49:50 +000088 end
89
90 it "should support the timeout accessor for write" do
91 @socket.timeout = 3
92 @socket.open
Dmytro Shteflyuk8a358b82026-02-22 12:11:12 +053093 write_calls = 0
94 expect(@handle).to receive(:write_nonblock).exactly(3).times do |chunk|
95 write_calls += 1
96 case write_calls
97 when 1
98 expect(chunk).to eq("test data")
99 raise IO::EAGAINWaitWritable
100 when 2
101 expect(chunk).to eq("test data")
102 4
103 when 3
104 expect(chunk).to eq(" data")
105 5
106 end
107 end
108 expect(IO).to receive(:select) do |rd, wr, err, timeout|
109 expect(rd).to be_nil
110 expect(wr).to eq([@handle])
111 expect(err).to be_nil
112 expect(timeout).to be > 0
113 expect(timeout).to be <= 3
114 [[], [@handle], []]
115 end
James E. King III27247072018-03-22 20:50:23 -0400116 expect(@socket.write("test data")).to eq(9)
Kevin Clark5ae0bab2008-07-18 21:49:50 +0000117 end
118
119 it "should raise an error when read times out" do
120 @socket.timeout = 0.5
121 @socket.open
Dmytro Shteflyuk8a358b82026-02-22 12:11:12 +0530122 expect(@handle).to receive(:read_nonblock).with(17).and_raise(IO::EAGAINWaitReadable)
123 expect(IO).to receive(:select).once { sleep(0.6); nil }
James E. King III27247072018-03-22 20:50:23 -0400124 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 +0000125 end
126
127 it "should raise an error when write times out" do
128 @socket.timeout = 0.5
129 @socket.open
Dmytro Shteflyuk8a358b82026-02-22 12:11:12 +0530130 expect(@handle).to receive(:write_nonblock).with("test data").and_raise(IO::EAGAINWaitWritable)
131 expect(IO).to receive(:select).once { sleep(0.6); nil }
James E. King III27247072018-03-22 20:50:23 -0400132 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 +0000133 end
Dmytro Shteflyuk8a358b82026-02-22 12:11:12 +0530134
135 it "should read buffered SSL data without waiting on the raw socket again" do
136 @socket.timeout = 1
137 @socket.open
138
139 expect(@handle).to receive(:read_nonblock).with(4).ordered.and_raise(IO::EAGAINWaitReadable)
140 expect(IO).to receive(:select).once.ordered do |rd, wr, err, timeout|
141 expect(rd).to eq([@handle])
142 expect(wr).to be_nil
143 expect(err).to be_nil
144 expect(timeout).to be > 0
145 expect(timeout).to be <= 1
146 [[@handle], [], []]
147 end
148 expect(@handle).to receive(:read_nonblock).with(4).ordered.and_return("ABCD")
149 expect(@handle).to receive(:read_nonblock).with(5).ordered.and_return("12345")
150
151 expect(@socket.read(4)).to eq("ABCD")
152 expect(@socket.read(5)).to eq("12345")
153 end
154
155 it "should read without timeout using the blocking path" do
156 @socket.timeout = nil
157 @socket.open
158
159 expect(IO).not_to receive(:select)
160 expect(@handle).not_to receive(:read_nonblock)
161 expect(@handle).to receive(:readpartial).with(4).ordered.and_return("ABCD")
162 expect(@handle).to receive(:readpartial).with(5).ordered.and_return("12345")
163
164 expect(@socket.read(4)).to eq("ABCD")
165 expect(@socket.read(5)).to eq("12345")
166 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +0000167end