Sha256: ed877e1b5f4c3a88887ba20b621cbc826976edb02075e39fb27bdae6f2a68d5f

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

require 'spec_helper'
require 'message_bus/timer_thread'

describe MessageBus::TimerThread do
  before do
    @timer = MessageBus::TimerThread.new
  end

  after do
    @timer.stop
  end

  it "allows you to cancel timers" do
    success = true
    @timer.queue(0.005){success=false}.cancel
    sleep(0.006)
    success.should == true
  end

  it "queues jobs in the correct order" do
    counter = 0
    failed = nil

    items = (0..5).to_a.shuffle
    items.map do |i|
      # threading introduces a delay meaning we need to wait a long time
      Thread.new do
        @timer.queue(i/500.0) do
          failed = true if counter != i
          counter += 1
        end
      end
    end

    wait_for(1500) {
      counter == items.length
    }

    counter.should == items.length
    failed.should == nil
  end

  it "should call the error callback if something goes wrong" do
    error = nil

    @timer.queue do
      boom
    end

    @timer.on_error do |e|
      error = e
    end

    @timer.queue do
      boom
    end

    wait_for(10) do
      error
    end

    error.class.should == NameError
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
message_bus-1.0.12 spec/lib/timer_thread_spec.rb
message_bus-1.0.11 spec/lib/timer_thread_spec.rb