aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/queueing/threaded_consumer_test.rb
blob: 559de2a82d22a7f298e605fb40adeb31bc0619ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'abstract_unit'
require 'rails/queueing'

class TestThreadConsumer < ActiveSupport::TestCase
  class Job
    attr_reader :id
    def initialize(id, &block)
      @id = id
      @block = block
    end

    def run
      @block.call if @block
    end
  end

  def setup
    @queue = Rails::Queueing::Queue.new
    @consumer = Rails::Queueing::ThreadedConsumer.start(@queue)
  end

  def teardown
    @queue.push nil
  end

  test "the jobs are executed" do
    ran = false

    job = Job.new(1) do
      ran = true
    end

    @queue.push job
    sleep 0.1
    assert_equal true, ran
  end

  test "the jobs are not executed synchronously" do
    ran = false

    job = Job.new(1) do
      sleep 0.1
      ran = true
    end

    @queue.push job
    assert_equal false, ran
  end

  test "shutting down the queue synchronously drains the jobs" do
    ran = false

    job = Job.new(1) do
      sleep 0.1
      ran = true
    end

    @queue.push job
    assert_equal false, ran

    @consumer.shutdown

    assert_equal true, ran
  end

  test "log job that raises an exception" do
    require "active_support/log_subscriber/test_helper"
    logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
    Rails.logger = logger

    job = Job.new(1) do
      raise "RuntimeError: Error!"
    end

    @queue.push job
    sleep 0.1

    assert_equal 1, logger.logged(:error).size
    assert_match(/Job Error: RuntimeError: Error!/, logger.logged(:error).last)
  end
end