Sha256: 96ff155ca6e15cfcd912e7d4a1966fc532f429176dee801b0a509c5a1edf0e8c

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

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_instrument_yields_the_payload_for_further_modification
        assert_equal 2, instrumenter.instrument("awesome") { |p| p[:result] = 1 + 1 }
        assert_equal 1, notifier.finishes.size
        name, _, payload = notifier.finishes.first
        assert_equal "awesome", name
        assert_equal Hash[:result => 2], payload
      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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activejob-lock-0.0.2 rails/activesupport/test/notifications/instrumenter_test.rb
activejob-lock-0.0.1 rails/activesupport/test/notifications/instrumenter_test.rb