| 30 |
ashish |
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 |
|
|
|
20 |
require File.dirname(__FILE__) + '/spec_helper'
|
|
|
21 |
|
|
|
22 |
shared_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
|
|
|
48 |
@handle.should_receive(:readpartial).with(17).and_raise(StandardError)
|
|
|
49 |
lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == 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 |
@handle.should_receive(:readpartial).with(17).and_return("test data")
|
|
|
55 |
@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
|
|
|
65 |
|
|
|
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
|
|
|
73 |
|
|
|
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
|
|
|
94 |
IO.should_receive(:select).with([@handle], nil, nil, 0.5).at_least(1).times.and_return(nil)
|
|
|
95 |
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
|
|
|
104 |
end
|