Sha256: ba724e7c5b6d412e516083e90c04ddeceee709cae0a0d7c3ee69248c7a90b926

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require_relative 'test_helper'

describe Adrian::Dispatcher do
  before do
    $done_items = []
    @q = Adrian::ArrayQueue.new
    @dispatcher = Adrian::Dispatcher.new(:stop_when_done => true)
  end

  describe "work delegation" do
    it "should instantiate an instance of the worker for each item and ask it to perform" do
      worker = Class.new do
        def initialize(item)
          @item = item
        end

        def perform
          $done_items << [@boss, @item.value]
        end

        def report_to(boss)
          @boss = boss
        end
      end

      @q.push(1)
      @q.push(2)
      @q.push(3)

      @dispatcher.start(@q, worker)

      $done_items.must_equal([[@dispatcher, 1], [@dispatcher, 2], [@dispatcher, 3]])
    end
  end

  describe "work evaluation" do
    it "should use the failure handler to handle the result" do
      @dispatcher.on_failure(RuntimeError) do |item, worker, exception|
        @q.push(item)
      end

      @dispatcher.work_done(1, nil)
      @q.pop.must_be_nil

      @dispatcher.work_done(1, nil, nil)
      @q.pop.must_be_nil

      @dispatcher.work_done(1, nil, RuntimeError.new)
      @q.pop.value.must_equal 1
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adrian-1.0.1 test/dispatcher_test.rb
adrian-1.0.0 test/dispatcher_test.rb