spec/onstomp/connections/base_spec.rb in onstomp-1.0.6 vs spec/onstomp/connections/base_spec.rb in onstomp-1.0.7

- old
+ new

@@ -17,10 +17,20 @@ Base.new(io, client) } let(:frame) { mock('frame') } + + describe "timeouts" do + it "defaults write timeout to nil" do + connection.write_timeout.should be_nil + end + + it "defaults read timeout to 120 seconds" do + connection.read_timeout.should == 120 + end + end describe ".method_missing" do it "should raise an unsupported command error if the method ends in _frame" do lambda { connection.lame_frame }.should raise_error(OnStomp::UnsupportedCommandError) end @@ -45,11 +55,12 @@ connection.duration_since_transmitted.should be_nil end it "should be the difference between now and the last_transmitted_at in milliseconds" do Time.stub(:now => 10) connection.stub(:last_transmitted_at => 8.5) - connection.duration_since_transmitted.should == 1500 + # Be careful, floating point will give you problems + connection.duration_since_transmitted.should == 1500.0 end end describe ".duration_since_received" do it "should be nil if last_received_at is nil" do @@ -57,11 +68,11 @@ connection.duration_since_received.should be_nil end it "should be the difference between now and the last_received_at in milliseconds" do Time.stub(:now => 10) connection.stub(:last_received_at => 6) - connection.duration_since_received.should == 4000 + connection.duration_since_received.should == 4000.0 end end describe ".close" do it "should close the socket if blocking is true" do @@ -278,15 +289,22 @@ connection.stub(:connected? => true) IO.stub(:select => true) io.should_receive(:read_nonblock).with(Base::MAX_BYTES_PER_READ).and_raise(Errno::EWOULDBLOCK) lambda { connection.io_process_read }.should_not raise_error end - it "should close the connection and not raise error if an EOFError is raised?" do + it "closes the connection and re-raises EOFError when connecting" do connection.stub(:connected? => true) IO.stub(:select => true) io.should_receive(:read_nonblock).with(Base::MAX_BYTES_PER_READ).and_raise(EOFError) io.should_receive(:close) + lambda { connection.io_process_read(true) }.should raise_error(EOFError) + end + it "closes the connection without re-raising EOFError when not connecting" do + connection.stub(:connected? => true) + IO.stub(:select => true) + io.should_receive(:read_nonblock).with(Base::MAX_BYTES_PER_READ).and_raise(EOFError) + io.should_receive(:close) lambda { connection.io_process_read }.should_not raise_error end it "should close the connection and re-raise if an IOError is raised" do connection.stub(:connected? => true) IO.stub(:select => true) @@ -309,17 +327,21 @@ io.should_receive(:close) io.should_receive(:read_nonblock).with(Base::MAX_BYTES_PER_READ).and_raise(Exception) lambda { connection.io_process_read }.should raise_error(Exception) triggered.should be_true end - it "should trigger a blocked close if checking timeout and it is exceeded" do + it "triggers a blocked close and raises ConnectionTimeoutError when connecting" do triggered = false connection.on_blocked { triggered = true } + connection.read_timeout = 0.5 IO.stub(:select => false) - connection.stub(:read_timeout_exceeded? => true, :connected? => true) + connection.stub(:connected? => true) io.should_receive(:close) - connection.io_process_read(true) + connection.__send__(:update_last_received) + lambda do + connection.io_process_read(true) while true + end.should raise_error(OnStomp::ConnectionTimeoutError) triggered.should be_true end end describe "read helpers" do @@ -352,21 +374,21 @@ connection.read_timeout = nil connection.__send__(:read_timeout_exceeded?).should be_false end it "should not exceed the timeout if duration is less than timeout" do connection.read_timeout = 10 - connection.stub(:duration_since_received => 9000) + connection.stub(:duration_since_received => 9000.0) connection.__send__(:read_timeout_exceeded?).should be_false end it "should not exceed the timeout if duration is equal to timeout" do connection.read_timeout = 10 - connection.stub(:duration_since_received => 10000) + connection.stub(:duration_since_received => 10000.0) connection.__send__(:read_timeout_exceeded?).should be_false end it "should exceed the timeout if duration is greater than timeout" do connection.read_timeout = 10 - connection.stub(:duration_since_received => 10001) + connection.stub(:duration_since_received => 10000.1) connection.__send__(:read_timeout_exceeded?).should be_true end end describe "write helpers" do @@ -411,13 +433,13 @@ Time.stub(:now => 59) connection.push_write_buffer 'FRAME_SERIALIZED', frame Time.stub(:now => 69) connection.__send__(:write_timeout_exceeded?).should be_false end - it "should not exceed the timeout if the duratio is greater but there's no buffered data" do + it "should not exceed the timeout if the duration is greater but there's no buffered data" do connection.write_timeout = 1 - connection.stub(:duration_since_transmitted => 5000) + connection.stub(:duration_since_transmitted => 5000.0) connection.__send__(:write_timeout_exceeded?).should be_false end it "should exceed the timeout if buffered and duration is greater than timeout" do Time.stub(:now => 59) connection.write_timeout = 10 @@ -485,9 +507,33 @@ connection.stub(:io_process_write => nil, :io_process_read => nil) connection.stub(:connected? => false) connection.io_process connection.io_process triggered.should == 1 + end + + it "re-raises an error raised while writing during connect" do + io.stub(:closed? => false) + connection.stub(:connect_frame => connect_frame) + connection.stub(:serializer => OnStomp::Connections::Serializers::Stomp_1_0.new) + connection.stub(:ready_for_write? => true) + connection.stub(:write_nonblock) { raise EOFError } + connection.stub(:io_process_read).with(true).and_yield(connected_frame) + lambda do + connection.connect(client) + end.should raise_error(EOFError) + end + + it "re-raises an EOFError raised while reading during connect" do + io.stub(:closed? => false) + connection.stub(:connect_frame => connect_frame) + connection.stub(:serializer => OnStomp::Connections::Serializers::Stomp_1_0.new) + connection.stub(:ready_for_read? => true) + connection.stub(:read_nonblock) { raise EOFError } + connection.stub(:io_process_write).and_yield(connect_frame) + lambda do + connection.connect(client) + end.should raise_error(EOFError) end end describe ".configure" do let(:client_bindings) {