Skip to content

Instantly share code, notes, and snippets.

@ipmsteven
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save ipmsteven/81e2ed741415dc60d0f6 to your computer and use it in GitHub Desktop.

Select an option

Save ipmsteven/81e2ed741415dc60d0f6 to your computer and use it in GitHub Desktop.
alias_method_chain and prepend
# 1. alias_method before prepend
module PreA
def foo
puts "pre a"
super
end
end
class A
def self.foo
puts "A"
end
end
class A
class << self
def foo_with_chain
puts "foo_with_hcain"
foo_without_chain
end
alias_method_chain :foo, :chain
end
end
class A
class << self
prepend PreA
end
end
A.foo()
# 2. alias_method after prepend
module PreA
def foo
puts "pre a"
super
end
end
class A
def self.foo
puts "A"
end
end
class A
class << self
prepend PreA
end
end
class A
class << self
def foo_with_chain
puts "foo_with_hcain"
foo_without_chain
end
alias_method_chain :foo, :chain
end
end
A.foo()
###############################################################################################
# "SystemStackError: stack level too deep" test error in af_xx gem tests when removing a_m_c #
###############################################################################################
###########
# example #
###########
# ruby -Itest test/af_job/queue_test.rb -n test_enqueue
# Run options: -n test_enqueue
# Running tests:
# Error:
# AfJob::QueueTest#test_enqueue:
# SystemStackError: stack level too deep
# /Users/yunlei/.rvm/gems/ruby-2.1.5/gems/newrelic_rpm-3.9.8.273/lib/new_relic/agent/traced_method_stack.rb:83
# Finished tests in 0.084935s, 11.7737 tests/s, 0.0000 assertions/s.
# 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
# The reason it failed here is that:
# In the test_helper of this unit test, `af_job` which contains the prepend stuff is required before the `test_app`,
# and when the test_app is required, it will add ActiveRecord transaction tracer by using newrelic_rpm(using alias_method way).
# then it hit the first case metioned above.
#############################################
# Why it not failed in property app level ? #
#############################################
# because newrelic_rpm is a dependency for property app which will be loaded before our app level prepend stuff.
######################
# Future work #
######################
# make the gem test works, decouple the dependency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment