blob: a0092e189166dbd2c8a300f71300ff9117220b28 [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
29 @handle.should_receive(:read).with(17).and_raise(StandardError)
30 lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
31 end
32
33 it "should raise an error when it reads no data from the handle" do
34 @socket.open
35 @handle.should_receive(:read).with(17).and_return("")
36 lambda { @socket.read(17) }.should raise_error(Thrift::TransportException, "Socket: Could not read 17 bytes from #{@socket.instance_variable_get("@desc")}")
37 end
38
39 it "should return the data read when reading from the handle works" do
40 @socket.open
41 @handle.should_receive(:read).with(17).and_return("test data")
42 @socket.read(17).should == "test data"
43 end
44
45 it "should declare itself as closed when it has an error" do
46 @socket.open
47 @handle.should_receive(:write).with("fail").and_raise(StandardError)
48 @socket.should be_open
49 lambda { @socket.write("fail") }.should raise_error
50 @socket.should_not be_open
51 end
Kevin Clark5da153b2008-06-26 18:35:15 +000052
53 it "should raise an error when the stream is closed" do
54 @socket.open
55 @handle.stub!(:closed?).and_return(true)
56 @socket.should_not be_open
57 lambda { @socket.write("fail") }.should raise_error(IOError, "closed stream")
58 lambda { @socket.read(10) }.should raise_error(IOError, "closed stream")
59 end
Kevin Clark2ddd8ed2008-06-18 01:18:35 +000060end