Sha256: b5640c2838dc0b8482afc143ae25143811e8c2a9e160d6e371ef82af7b8a641d

Contents?: true

Size: 1.25 KB

Versions: 12

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require_relative '../../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 queue every jobs" do
    i = 0
    m = Mutex.new
    every = @timer.every(0.001) { m.synchronize { i += 1 if i < 3 } }
    # allow lots of time, cause in test mode stuff can be slow
    wait_for(1000) do
      m.synchronize do
        every.cancel if i == 3
        i == 3
      end
    end
    sleep 0.002
    i.must_equal 3
  end

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

  it "queues jobs in the correct order" do
    results = []
    (0..3).to_a.reverse.each do |i|
      @timer.queue(0.005 * i) do
        results << i
      end
    end

    wait_for(3000) {
      4 == results.length
    }

    results.must_equal [0, 1, 2, 3]
  end

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

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

    @timer.queue do
      boom
    end

    wait_for(100) do
      error
    end

    error.class.must_equal NameError
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
message_bus-4.3.8 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.3.7 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.3.6 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.3.4 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.3.3 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.3.2 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.3.1 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.3.0 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.2.0 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.1.0 spec/lib/message_bus/timer_thread_spec.rb
message_bus-4.0.0 spec/lib/message_bus/timer_thread_spec.rb
message_bus-3.4.0 spec/lib/message_bus/timer_thread_spec.rb