aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/notifications/instrumenter_test.rb
blob: 62a9b61464ee364faf51a3bf6ccd294c8e8484d0 (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
require 'abstract_unit'
require 'active_support/notifications/instrumenter'

module ActiveSupport
  module Notifications
    class InstrumenterTest < ActiveSupport::TestCase
      class TestNotifier
        attr_reader :starts, :finishes

        def initialize
          @starts   = []
          @finishes = []
        end

        def start(*args);  @starts << args; end
        def finish(*args); @finishes << args; end
      end

      attr_reader :instrumenter, :notifier, :payload

      def setup
        super
        @notifier     = TestNotifier.new
        @instrumenter = Instrumenter.new @notifier
        @payload      =  { :foo => Object.new }
      end

      def test_instrument
        called  = false
        instrumenter.instrument("foo", payload) {
          called = true
        }

        assert called
      end

      def test_start
        instrumenter.start("foo", payload)
        assert_equal [["foo", instrumenter.id, payload]], notifier.starts
        assert_predicate notifier.finishes, :empty?
      end

      def test_finish
        instrumenter.finish("foo", payload)
        assert_equal [["foo", instrumenter.id, payload]], notifier.finishes
        assert_predicate notifier.starts, :empty?
      end
    end
  end
end