aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/initializers/notifications_test.rb
blob: b847ac6b361f5528e2d9e796de4c22a6edbb1e34 (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
require "isolation/abstract_unit"

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

    def setup
      build_app
    end

    def teardown
      teardown_app
    end

    def instrument(*args, &block)
      ActiveSupport::Notifications.instrument(*args, &block)
    end

    def wait
      ActiveSupport::Notifications.notifier.wait
    end

    test "rails log_subscribers are added" do
      add_to_config <<-RUBY
        config.colorize_logging = false
      RUBY

      require "#{app_path}/config/environment"
      require "active_support/log_subscriber/test_helper"

      logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
      ActiveRecord::Base.logger = logger

      # Mimic Active Record notifications
      instrument "sql.active_record", name: "SQL", sql: "SHOW tables"
      wait

      assert_equal 1, logger.logged(:debug).size
      assert_match(/SHOW tables/, logger.logged(:debug).last)
    end

    test "rails load_config_initializer event is instrumented" do
      app_file "config/initializers/foo.rb", ""

      events = []
      callback = ->(*_) { events << _ }
      ActiveSupport::Notifications.subscribed(callback, "load_config_initializer.railties") do
        app
      end

      assert_equal %w[load_config_initializer.railties], events.map(&:first)
      assert_includes events.first.last[:initializer], "config/initializers/foo.rb"
    end
  end
end