Skip to content

Commit

Permalink
fix: error in Profiler wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ffloyd committed Feb 7, 2024
1 parent 3543fe6 commit 621e311
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 6 additions & 6 deletions lib/flows/plugin/profiler/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions spec/flows/plugin/profiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 621e311

Please sign in to comment.