Sha256: 4b7f08460c5afd1fd03790cd2c24baa62650f823e27d0da544bd2bb02fd6bce5

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

require 'spec_helper'

describe GnipApi::PowerTrack::Buffer do
  before do
    @buffer = GnipApi::PowerTrack::Buffer.new(:terminator => '--')
  end
  
  it 'has "--" as terminator char' do
    expect(@buffer.terminator).to eq('--')
  end

  it 'stores string data' do
    expect(@buffer.data.kind_of?(String)).to eq(true)
  end

  describe '#insert!' do
    it 'apends data at the end' do
      @buffer.insert! "asd"
      expect(@buffer.data[-1]).to eq('d')
      @buffer.insert! 'lol'
      expect(@buffer.data[-1]).to eq('l')
    end
  end

  describe '#read!' do
    context 'with keep-alive signals' do
      it 'does not return empty items' do
        ['asd--', '--', 'qwe--', '--', '--'].each{|item| @buffer.insert!(item)}
        data = @buffer.read!
        expect(data).to eq(['asd', 'qwe']) # last item is considered incomplete
      end
    end

    context 'with partial data' do
      before do
        @buffer.insert! 'asd--qwe--x'
      end
      
      it 'gets complete objects' do
        objects = @buffer.read!
        expect(objects.empty?).to eq(false)
      end

      it 'consumes data from buffer' do
        @buffer.read!
        expect(@buffer.data).to eq('x')
      end
    end

    context 'with complete data' do
      before do
        @buffer.insert! 'asd--qwe--zxc'
      end

      it 'gets complete objects except for last one' do
        objects = @buffer.read!
        expect(objects.size).to eq(2)
      end

      it 'removes the data read' do
        @buffer.read!
        expect(@buffer.data.include?('asd')).to eq(false)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gnip_api-1.2.4 spec/gnip_api/power_track/buffer_spec.rb
gnip_api-1.2.3 spec/gnip_api/power_track/buffer_spec.rb