From 621e31157a42ccdbf2d2ff5fe25cc9be99e70a45 Mon Sep 17 00:00:00 2001 From: Roman Kolesnev Date: Wed, 7 Feb 2024 19:47:03 +0100 Subject: [PATCH] fix: error in Profiler wrapper --- lib/flows/plugin/profiler/wrapper.rb | 12 ++++++------ spec/flows/plugin/profiler_spec.rb | 16 +++++++++------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/flows/plugin/profiler/wrapper.rb b/lib/flows/plugin/profiler/wrapper.rb index 89e0b74..1743a87 100644 --- a/lib/flows/plugin/profiler/wrapper.rb +++ b/lib/flows/plugin/profiler/wrapper.rb @@ -6,17 +6,17 @@ module Wrapper class << self def make_instance_wrapper(method_name) # rubocop:disable Metrics/MethodLength Module.new.tap do |mod| - mod.define_method(method_name) do |*args, &block| # rubocop:disable Metrics/MethodLength + mod.define_method(method_name) do |*args, **kwargs, &block| # rubocop:disable Metrics/MethodLength thread = Thread.current klass = self.class - return super(*args, &block) unless thread[THREAD_VAR_FLAG] + return super(*args, **kwargs, &block) unless thread[THREAD_VAR_FLAG] report = thread[THREAD_VAR_REPORT] report.add(:started, klass, :instance, method_name, nil) before = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_microsecond) - super(*args, &block) + super(*args, **kwargs, &block) ensure if before after = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_microsecond) @@ -28,16 +28,16 @@ def make_instance_wrapper(method_name) # rubocop:disable Metrics/MethodLength def make_singleton_wrapper(method_name) # rubocop:disable Metrics/MethodLength Module.new.tap do |mod| - mod.define_method(method_name) do |*args, &block| # rubocop:disable Metrics/MethodLength + mod.define_method(method_name) do |*args, **kwargs, &block| # rubocop:disable Metrics/MethodLength thread = Thread.current - return super(*args, &block) unless thread[THREAD_VAR_FLAG] + return super(*args, **kwargs, &block) unless thread[THREAD_VAR_FLAG] report = thread[THREAD_VAR_REPORT] report.add(:started, self, :singleton, method_name, nil) before = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_microsecond) - super(*args, &block) + super(*args, **kwargs, &block) ensure if before after = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_microsecond) diff --git a/spec/flows/plugin/profiler_spec.rb b/spec/flows/plugin/profiler_spec.rb index ab428e5..3a9cb0d 100644 --- a/spec/flows/plugin/profiler_spec.rb +++ b/spec/flows/plugin/profiler_spec.rb @@ -3,6 +3,8 @@ RSpec.describe Flows::Plugin::Profiler do let(:report) do instance_double(described_class::Report::Raw).tap do |dbl| + # `===` checks if report is instance of child of Report class + # so our double also should pass this check allow(described_class::Report).to receive(:===).and_call_original allow(described_class::Report).to receive(:===).with(dbl).and_return true @@ -21,25 +23,25 @@ describe '.profile' do subject(:profile) do described_class.profile(report) do - user_class.on_singleton - user_class.new.on_instance + user_class.on_singleton(:i_am_arg, kwarg: :i_am_kwarg) { :i_am_from_block } + user_class.new.on_instance(:i_am_arg, kwarg: :i_am_kwarg) { :i_am_from_block } end end let(:user_class) do Class.new do - def on_instance - :from_instance + def on_instance(arg, kwarg:) + [:from_instance, arg, kwarg, yield] end - def self.on_singleton - :from_singleton + def self.on_singleton(arg, kwarg:) + [:from_singleton, arg, kwarg, yield] end end end it 'returns block value' do - expect(profile).to eq :from_instance + expect(profile).to eq %i[from_instance i_am_arg i_am_kwarg i_am_from_block] end context 'with instance method' do