Skip to content

Commit

Permalink
non blocking next_packet shall return the available data before raisi…
Browse files Browse the repository at this point in the history
…ng disconnect
  • Loading branch information
mfazekas committed Aug 14, 2016
1 parent 4a2ec61 commit eb05049
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/net/ssh/transport/packet_stream.rb
Expand Up @@ -86,7 +86,12 @@ def next_packet(mode=:nonblock)
when :nonblock then
if available_for_read?
if fill <= 0
raise Net::SSH::Disconnect, "connection closed by remote host"
result = poll_next_packet
if result.nil?
raise Net::SSH::Disconnect, "connection closed by remote host"
else
return result
end
end
end
poll_next_packet
Expand Down
18 changes: 17 additions & 1 deletion test/transport/test_packet_stream.rb
Expand Up @@ -6,7 +6,7 @@

module Transport

class TestPacketStream < NetSSHTest
class TestPacketStream < NetSSHTest # rubocop:disable Metrics/ClassLength
include Net::SSH::Transport::Constants

def test_client_name_when_getnameinfo_works
Expand Down Expand Up @@ -142,6 +142,22 @@ def test_next_packet_should_eventually_return_packet_when_non_blocking_and_parti
assert_equal DEBUG, packet.type
end

def test_nonblocking_next_packet_should_raise
IO.stubs(:select).returns([[stream]])
stream.stubs(:recv).returns("")
assert_raises(Net::SSH::Disconnect) { stream.next_packet(:nonblock) }
end

def test_nonblocking_next_packet_should_return_packet_before_raise
IO.stubs(:select).returns([[stream]])
stream.send(:input).append(packet)
stream.stubs(:recv).returns("")
packet = stream.next_packet(:nonblock)
assert_not_nil packet
assert_equal DEBUG, packet.type
assert_raises(Net::SSH::Disconnect) { stream.next_packet(:nonblock) }
end

def test_next_packet_should_block_when_requested_until_entire_packet_is_available
IO.stubs(:select).returns([[stream]])
stream.stubs(:recv).returns(packet[0,10], packet[10,20], packet[20..-1])
Expand Down

0 comments on commit eb05049

Please sign in to comment.