Sha256: 6c40dd8c45078227361cc5671762395455f26289b94398035a472bae7848f365

Contents?: true

Size: 1.67 KB

Versions: 78

Compression:

Stored size: 1.67 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 'exposes #reads, #seeks, #bytes' do
    reader = FormatParser::ReadLimiter.new(io)
    expect(reader.pos).to eq(0)
    reader.read(2)
    reader.seek(3)
    reader.seek(4)

    expect(reader.reads).to eq(1)
    expect(reader.bytes).to eq(2)
    expect(reader.seeks).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

  it 'can be reset!' do
    reader = FormatParser::ReadLimiter.new(io, max_bytes: 512)
    reader.read(512)
    expect {
      reader.read(1)
    }.to raise_error(/budget/)
    reader.reset_limits!
    reader.read(1)
  end
end

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
format_parser-0.15.0 spec/read_limiter_spec.rb
format_parser-0.14.1 spec/read_limiter_spec.rb
format_parser-0.14.0 spec/read_limiter_spec.rb
format_parser-0.13.6 spec/read_limiter_spec.rb
format_parser-0.13.5 spec/read_limiter_spec.rb
format_parser-0.13.4 spec/read_limiter_spec.rb
format_parser-0.13.3 spec/read_limiter_spec.rb
format_parser-0.13.2 spec/read_limiter_spec.rb
format_parser-0.13.1 spec/read_limiter_spec.rb
format_parser-0.13.0 spec/read_limiter_spec.rb
format_parser-0.12.4 spec/read_limiter_spec.rb
format_parser-0.12.2 spec/read_limiter_spec.rb
format_parser-0.12.1 spec/read_limiter_spec.rb
format_parser-0.12.0 spec/read_limiter_spec.rb
format_parser-0.11.0 spec/read_limiter_spec.rb
format_parser-0.10.0 spec/read_limiter_spec.rb
format_parser-0.9.4 spec/read_limiter_spec.rb
format_parser-0.9.3 spec/read_limiter_spec.rb