Sha256: 0d7268c49a8a21cb128ff75f7cf0fd57dc8ca8321675307d81f1c8b76cd86a9d

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

Stored size: 1.23 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'thread'

CNT = 10000
CNTI = CNT
CNTO = CNT

describe QueueDing::QDing do
  before(:each) do
    @queue = QueueDing::QDing.new

    class Foobar
      def initialize
        @stuff = [1, 2, 3]
      end

    end
    @foo = Foobar.new
  end

  it "QDing can be created" do
    expect(@queue).to_not be_nil
  end

  it "pushes an object into the queue" do
    @queue << @foo
    expect(@queue.first).to eq @foo
  end

  it "extracts an object from the queue" do
    @queue << @foo
    bar = @queue.pop
    expect(bar).to eq @foo
  end

  it "handles concurrency" do
    tout = Thread.new do
      (0..CNTO).each{ |j|
        (i, num) = @queue.next
        expect(j).to eq i
        expect(num).to eq j * 7 + 3
      }
    end
    tin = Thread.new do
      (0..CNTI).map{|i| [i, i * 7 + 3]}.each{|pair| @queue << pair }
    end
    tin.join
    tout.join
    expect(@queue.empty?).to be_truthy
  end

  it "handles teeing" do
    tg = ThreadGroup.new
    tg.add Thread.new {
      sleep 5
      (0..CNT).each{|i| @queue << i }
    }

    20.times do
      tg.add Thread.new {
        (0..CNT).each{|i| expect(i).to eq @queue.next }
      }
    end

    tg.list.each {|t| t.join }
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
queue_ding-0.1.6 spec/queue_ding_spec.rb
queue_ding-0.1.5 spec/queue_ding_spec.rb
queue_ding-0.1.4 spec/queue_ding_spec.rb
queue_ding-0.1.3 spec/queue_ding_spec.rb
queue_ding-0.1.2 spec/queue_ding_spec.rb
queue_ding-0.1.1 spec/queue_ding_spec.rb
queue_ding-0.1.0 spec/queue_ding_spec.rb