aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/queueing/test_queue_test.rb
blob: 78c6c617fe1ae6cd04c6a7cebff4f53c8ee592a8 (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
require 'abstract_unit'
require 'rails/queueing'

class TestQueueTest < ActiveSupport::TestCase
  class Job
    def initialize(&block)
      @block = block
    end

    def run
      @block.call if @block
    end
  end

  def setup
    @queue = Rails::Queueing::TestQueue.new
  end

  def test_drain_raises
    @queue.push Job.new { raise }
    assert_raises(RuntimeError) { @queue.drain }
  end

  def test_jobs
    @queue.push 1
    @queue.push 2
    assert_equal [1,2], @queue.jobs
  end

  def test_contents
    assert @queue.empty?
    job = Job.new
    @queue.push job
    refute @queue.empty?
    assert_equal job, @queue.pop
  end

  def test_order
    processed = []

    job1 = Job.new { processed << 1 }
    job2 = Job.new { processed << 2 }

    @queue.push job1
    @queue.push job2
    @queue.drain

    assert_equal [1,2], processed
  end

  def test_drain
    t = nil
    ran = false

    job = Job.new do
      ran = true
      t = Thread.current
    end

    @queue.push job
    @queue.drain

    assert @queue.empty?
    assert ran, "The job runs synchronously when the queue is drained"
    assert_not_equal t, Thread.current
  end
end