File: read_nonblock_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (64 lines) | stat: -rw-r--r-- 1,853 bytes parent folder | download
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
64
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "IO#read_nonblock" do
  before(:each) do
    @read, @write = IO.pipe
  end

  after(:each) do
    @read.close rescue nil
    @write.close rescue nil
  end

  it "raises EAGAIN when there is no data" do
    lambda { @read.read_nonblock(5) }.should raise_error(Errno::EAGAIN)
  end

  ruby_version_is "1.9" do
    it "raises IO::WaitReadable when there is no data" do
      lambda { @read.read_nonblock(5) }.should raise_error(IO::WaitReadable)
    end
  end

  it "returns at most the number of bytes requested" do
    @write << "hello"
    @read.read_nonblock(4).should == "hell"
  end

  it "returns less data if that is all that is available" do
    @write << "hello"
    @read.read_nonblock(10).should == "hello"
  end

  not_compliant_on :rubinius, :jruby do
    ruby_version_is ""..."1.9" do
      it "changes the behavior of #read to nonblocking" do
        @write << "hello"
        @read.read_nonblock(5)

        # Yes, use normal IO#read here. #read_nonblock has changed the internal
        # flags of @read to be nonblocking, so now any normal read calls raise
        # EAGAIN if there is no data.
        lambda { @read.read(5) }.should raise_error(Errno::EAGAIN)
      end
    end

    # This feature was changed in 1.9
    # see also: [ruby-dev:25101] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/25101
    #   and #2469 http://redmine.ruby-lang.org/issues/show/2469
  end

  it "raises IOError on closed stream" do
    lambda { IOSpecs.closed_io.read_nonblock(5) }.should raise_error(IOError)
  end

  it "raises EOFError when the end is reached" do
    @write << "hello"
    @write.close

    @read.read_nonblock(5)

    lambda { @read.read_nonblock(5) }.should raise_error(EOFError)
  end
end