Sha256: 422de0b88fe6dbf518d0956463c5d3182dda5a403fb14aabd51da41d2c988e0b

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require 'spec_helper'

describe FormatParser::ReadLimiter do
  let(:io) { StringIO.new(Random.new.bytes(1024)) }

  it_behaves_like 'an IO object compatible with IOConstraint'

  it 'does not enforce any limits with default arguments' do
    reader = FormatParser::ReadLimiter.new(io)
    2048.times { reader.seek(1) }
    2048.times { reader.read(4) }
  end

  it 'passes #pos to the delegate' do
    reader = FormatParser::ReadLimiter.new(io)
    expect(reader.pos).to eq(0)
    io.read(2)
    expect(reader.pos).to eq(2)
  end

  it 'enforces the number of seeks' do
    reader = FormatParser::ReadLimiter.new(io, max_seeks: 4)
    4.times { reader.seek(1) }
    expect {
      reader.seek(1)
    }.to raise_error(/Seek budget exceeded/)
  end

  it 'enforces the number of reads' do
    reader = FormatParser::ReadLimiter.new(io, max_reads: 4)
    4.times { reader.read(1) }
    expect {
      reader.read(1)
    }.to raise_error(/calls exceeded \(4 max\)/)
  end

  it 'enforces the number of bytes read' do
    reader = FormatParser::ReadLimiter.new(io, max_bytes: 512)
    reader.read(512)
    expect {
      reader.read(1)
    }.to raise_error(/bytes budget \(512\) exceeded/)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
format_parser-0.3.3 spec/read_limiter_spec.rb
format_parser-0.3.2 spec/read_limiter_spec.rb
format_parser-0.3.1 spec/read_limiter_spec.rb
format_parser-0.3.0 spec/read_limiter_spec.rb