summaryrefslogtreecommitdiff
path: root/test/test_buffered_io.rb
blob: 10026866b685a2e6f7770f77b8e937900e4cd85b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'common'
require 'net/ssh/buffered_io'

class TestBufferedIo < NetSSHTest
  def test_fill_should_pull_from_underlying_io
    io.expects(:recv).with(8192).returns("here is some data")
    assert_equal 17, io.fill
    assert_equal 17, io.available
    assert_equal "here is some data", io.read_available(20)
  end

  def test_enqueue_should_not_write_to_underlying_io
    assert !io.pending_write?
    io.expects(:send).never
    io.enqueue("here is some data")
    assert io.pending_write?
  end

  def test_send_pending_should_not_fail_when_no_writes_are_pending
    assert !io.pending_write?
    io.expects(:send).never
    assert_nothing_raised { io.send_pending }
  end

  def test_send_pending_with_pending_writes_should_write_to_underlying_io
    io.enqueue("here is some data")
    io.expects(:send).with("here is some data", 0).returns(17)
    assert io.pending_write?
    assert_nothing_raised { io.send_pending }
    assert !io.pending_write?
  end

  def test_wait_for_pending_sends_should_write_only_once_if_all_can_be_written_at_once
    io.enqueue("here is some data")
    io.expects(:send).with("here is some data", 0).returns(17)
    assert io.pending_write?
    assert_nothing_raised { io.wait_for_pending_sends }
    assert !io.pending_write?
  end

  def test_wait_for_pending_sends_should_write_multiple_times_if_first_write_was_partial
    io.enqueue("here is some data")

    io.expects(:send).with("here is some data", 0).returns(10)
    io.expects(:send).with("me data", 0).returns(4)
    io.expects(:send).with("ata", 0).returns(3)

    IO.expects(:select).times(2).with(nil, [io]).returns([[], [io]])

    assert_nothing_raised { io.wait_for_pending_sends }
    assert !io.pending_write?
  end

  private

  def io
    @io ||= begin
      io = mock("io")
      io.extend(Net::SSH::BufferedIo)
      io
    end
  end
end