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
20require 'spec_helper'
21
22shared_examples_for "a socket" do
23  it "should open a socket" do
24    expect(@socket.open).to eq(@handle)
25  end
26
27  it "should be open whenever it has a handle" do
28    expect(@socket).not_to be_open
29    @socket.open
30    expect(@socket).to be_open
31    @socket.handle = nil
32    expect(@socket).not_to be_open
33    @socket.handle = @handle
34    @socket.close
35    expect(@socket).not_to be_open
36  end
37
38  it "should write data to the handle" do
39    @socket.open
40    expect(@handle).to receive(:write).with("foobar")
41    @socket.write("foobar")
42    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) }
44  end
45
46  it "should raise an error when it cannot read from the handle" do
47    @socket.open
48    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) }
50  end
51
52  it "should return the data read when reading from the handle works" do
53    @socket.open
54    expect(@handle).to receive(:readpartial).with(17).and_return("test data")
55    expect(@socket.read(17)).to eq("test data")
56  end
57
58  it "should declare itself as closed when it has an error" do
59    @socket.open
60    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
64  end
65
66  it "should raise an error when the stream is closed" do
67    @socket.open
68    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")
72  end
73
74  it "should support the timeout accessor for read" do
75    @socket.timeout = 3
76    @socket.open
77    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")
80  end
81
82  it "should support the timeout accessor for write" do
83    @socket.timeout = 3
84    @socket.open
85    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)
89  end
90
91  it "should raise an error when read times out" do
92    @socket.timeout = 0.5
93    @socket.open
94    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) }
96  end
97
98  it "should raise an error when write times out" do
99    @socket.timeout = 0.5
100    @socket.open
101    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) }
103  end
104end
105