aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/queue_test.rb
blob: ec809d7cc1085426a4fa99ebfd9658f7912da5bf (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'isolation/abstract_unit'
require 'rack/test'

module ApplicationTests
  class GeneratorsTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
    end

    def teardown
      teardown_app
    end

    def app_const
      @app_const ||= Class.new(Rails::Application)
    end

    test "the queue is a TestQueue in test mode" do
      app("test")
      assert_kind_of Rails::Queueing::TestQueue, Rails.application.queue
      assert_kind_of Rails::Queueing::TestQueue, Rails.queue
    end

    test "the queue is a Queue in development mode" do
      app("development")
      assert_kind_of Rails::Queueing::Queue, Rails.application.queue
      assert_kind_of Rails::Queueing::Queue, Rails.queue
    end

    test "in development mode, an enqueued job will be processed in a separate thread" do
      app("development")
      current = Thread.current

      job = Struct.new(:origin, :target).new(Thread.current)
      def job.run
        self.target = Thread.current
      end

      Rails.queue.push job
      sleep 0.1

      assert job.target, "The job was run"
      assert_not_equal job.origin, job.target
    end

    test "in test mode, explicitly draining the queue will process it in a separate thread" do
      app("test")
      current = Thread.current

      job = Struct.new(:origin, :target).new(Thread.current)
      def job.run
        self.target = Thread.current
      end

      Rails.queue.push job
      Rails.queue.drain

      assert job.target, "The job was run"
      assert_not_equal job.origin, job.target
    end

    test "in test mode, the queue can be observed" do
      app("test")

      job = Class.new(Struct.new(:id)) do
        def run
        end
      end

      jobs = (1..10).map do |id|
        job.new(id)
      end

      jobs.each do |job|
        Rails.queue.push job
      end

      assert_equal jobs, Rails.queue.jobs
    end

    test "a custom queue implementation can be provided" do
      add_to_env_config "production", <<-RUBY
        require "my_queue"
        config.queue = MyQueue
      RUBY

      app_file "lib/my_queue.rb", <<-RUBY
        class MyQueue
          def push(job)
            job.run
          end
        end
      RUBY

      app("production")

      assert_kind_of MyQueue, Rails.queue

      job = Class.new(Struct.new(:id, :ran)) do
        def run
          self.ran = true
        end
      end

      job1 = job.new(1)
      Rails.queue.push job1

      assert_equal true, job1.ran
    end
  end
end