aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb
blob: 4e8ff60eb31bef4125ae9a32c0205d273ae55a73 (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
# frozen_string_literal: true

module CacheInstrumentationBehavior
  def test_fetch_multi_uses_write_multi_entries_store_provider_interface
    assert_called_with(@cache, :write_multi_entries) do
      @cache.fetch_multi "a", "b", "c" do |key|
        key * 2
      end
    end
  end

  def test_write_multi_instrumentation
    writes = { "a" => "aa", "b" => "bb" }

    events = with_instrumentation "write_multi" do
      @cache.write_multi(writes)
    end

    assert_equal %w[ cache_write_multi.active_support ], events.map(&:name)
    assert_nil events[0].payload[:super_operation]
    assert_equal({ "a" => "aa", "b" => "bb" }, events[0].payload[:key])
  end

  def test_instrumentation_with_fetch_multi_as_super_operation
    @cache.write("b", "bb")

    events = with_instrumentation "read_multi" do
      @cache.fetch_multi("a", "b") { |key| key * 2 }
    end

    assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
    assert_equal :fetch_multi, events[0].payload[:super_operation]
    assert_equal ["b"], events[0].payload[:hits]
  end

  def test_read_multi_instrumentation
    @cache.write("b", "bb")

    events = with_instrumentation "read_multi" do
      @cache.read_multi("a", "b") { |key| key * 2 }
    end

    assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
    assert_equal ["b"], events[0].payload[:hits]
  end

  private
    def with_instrumentation(method)
      event_name = "cache_#{method}.active_support"

      [].tap do |events|
        ActiveSupport::Notifications.subscribe event_name do |*args|
          events << ActiveSupport::Notifications::Event.new(*args)
        end
        yield
      end
    ensure
      ActiveSupport::Notifications.unsubscribe event_name
    end
end