blob: 2d17fd3cc5c150af3e85dbadf643c64b066c0f92 [file] [log] [blame]
Kevin Clark2ddd8ed2008-06-18 01:18:35 +00001require File.dirname(__FILE__) + '/spec_helper'
2
3shared_examples_for "a socket" do
4 it "should open a socket" do
5 @socket.open.should == @handle
6 end
7
8 it "should be open whenever it has a handle" do
9 @socket.should_not be_open
10 @socket.open
11 @socket.should be_open
12 @socket.handle = nil
13 @socket.should_not be_open
14 @socket.handle = @handle
15 @socket.close
16 @socket.should_not be_open
17 end
18
19 it "should write data to the handle" do
20 @socket.open
21 @handle.should_receive(:write).with("foobar")
22 @socket.write("foobar")
23 @handle.should_receive(:write).with("fail").and_raise(StandardError)
24 lambda { @socket.write("fail") }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
25 end
26
27 it "should raise an error when it cannot read from the handle" do
28 @socket.open
Kevin Clark4bd89162008-07-08 00:47:49 +000029 @handle.should_receive(:readpartial).with(17).and_raise(StandardError)
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000030 lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
31 end
32
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000033 it "should return the data read when reading from the handle works" do
34 @socket.open
Kevin Clark4bd89162008-07-08 00:47:49 +000035 @handle.should_receive(:readpartial).with(17).and_return("test data")
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000036 @socket.read(17).should == "test data"
37 end
38
39 it "should declare itself as closed when it has an error" do
40 @socket.open
41 @handle.should_receive(:write).with("fail").and_raise(StandardError)
42 @socket.should be_open
43 lambda { @socket.write("fail") }.should raise_error
44 @socket.should_not be_open
45 end
Kevin Clark5da153b2008-06-26 18:35:15 +000046
47 it "should raise an error when the stream is closed" do
48 @socket.open
49 @handle.stub!(:closed?).and_return(true)
50 @socket.should_not be_open
51 lambda { @socket.write("fail") }.should raise_error(IOError, "closed stream")
52 lambda { @socket.read(10) }.should raise_error(IOError, "closed stream")
53 end
Kevin Clark5ae0bab2008-07-18 21:49:50 +000054
55 it "should support the timeout accessor for read" do
56 @socket.timeout = 3
57 @socket.open
58 IO.should_receive(:select).with([@handle], nil, nil, 3).and_return([[@handle], [], []])
59 @handle.should_receive(:readpartial).with(17).and_return("test data")
60 @socket.read(17).should == "test data"
61 end
62
63 it "should support the timeout accessor for write" do
64 @socket.timeout = 3
65 @socket.open
66 IO.should_receive(:select).with(nil, [@handle], nil, 3).twice.and_return([[], [@handle], []])
67 @handle.should_receive(:write_nonblock).with("test data").and_return(4)
68 @handle.should_receive(:write_nonblock).with(" data").and_return(5)
69 @socket.write("test data").should == 9
70 end
71
72 it "should raise an error when read times out" do
73 @socket.timeout = 0.5
74 @socket.open
75 IO.should_receive(:select).with([@handle], nil, nil, 0.5).at_least(1).times.and_return(nil)
76 lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::TIMED_OUT }
77 end
78
79 it "should raise an error when write times out" do
80 @socket.timeout = 0.5
81 @socket.open
82 IO.should_receive(:select).with(nil, [@handle], nil, 0.5).any_number_of_times.and_return(nil)
83 lambda { @socket.write("test data") }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::TIMED_OUT }
84 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000085end