New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mocha test failures when using JRuby #274
Comments
@chrisroos: It's worth remembering that those snapshot assertions depend on the introspection gem which might not work correctly in JRuby. |
A quick glance through the introspection gem shows only one issue: Arbitrary walking of all objects in the system is not supported under JRuby without a flag, since there's no simple/efficient way to walk all objects on a JVM heap (concurrent threads, GC, objects moving around, etc). However, if this were changed to I'll try to run the mocha tests and see if JRuby master is any better. Many fixes since 9.1.5.0. |
I get the same 9 failures on JRuby master with or without ObjectSpace enabled. Investigating. |
Ok so these all seem to be the same problem, but I'm confused why it doesn't work (or rather, I'm confused why it works in MRI. Basically, when mocking a method for a class/object that already has that method, I see the call to I'm focusing on one test right now: def test_should_stub_public_method_and_leave_it_unchanged_after_test
klass = Class.new do
class << self
def my_class_method
:original_return_value
end
public :my_class_method
def self.public(*args); end
end
end
assert_snapshot_unchanged(klass) do
test_result = run_as_test do
klass.stubs(:my_class_method).returns(:new_return_value)
assert_method_visibility klass, :my_class_method, :public
assert_equal :new_return_value, klass.my_class_method
end
assert_passed(test_result)
end
assert_equal :original_return_value, klass.my_class_method
end
I just don't understand where that undef damage gets undone. |
Ok I think I've untangled some of it. So I was following the wrong path. Confirm for me that I have this right... When mocking an object, the method is not directly removed. Instead, a new module is prepended onto that object's class. Any methods that already existed on the original that now need to be mocked are undef'ed so they'll go through method_missing. Eventually, the prepended module's undef is removed with So it may be that our |
Ok, revision...under Ruby 2+, the prepended module gets a new definition for the method in question that just redispatches to method_missing. That method is later removed, so the dispatch returns to the original method. So it's getting overwritten by the def or improperly removed by the later remove_method. |
Hey @headius. Thanks for investigating!
Yes - @floehopper and I think that your understanding sounds correct.
Can you clarify what "it" is in this sentence? The original method? Assuming you are talking about the original method then based on the assertion messages the most likely scenario is that it's being incorrectly removed. |
Or at least removed for purposes of snapshotting. I thought I'd be able to reproduce the problem with this code, but it works fine on JRuby, contrary to what I expected based on mocha: M = Module.new
cls = Class.new do
class << self
def foo
:old
end
prepend M
end
end
module M
def foo
:new
end
end
p cls.foo
module M
remove_method :foo
end
p cls.foo
p cls.public_methods - Class.public_methods
__END__
Running on JRuby (matches MRI):
$ jruby blah.rb
:new
:old
[:foo] So either I'm not doing this the same way as mocha, or the snapshot is getting messed up somehow. |
Indeed, it seems to be something related to the snapshot. Here I print out the non-Class public methods for the given test, and the method the snapshot claims was removed is actually there:
Perhaps it is expecting the Method objects to be idempotent in some way? |
Ah-ha, I think I've figured it out. It appears our
After the prepend, the owner of a given
|
With the fix in jruby/jruby#4250, all mocha tests appear to pass. I am still evaluating that fix, though. |
@headius: Thanks for getting to the bottom of the problem. Let us know if you think there's anything wrong with the mocha or introspection code and/or if there's anything we can do to help you. |
@floehopper I see no other issues with introspection. We were in error here. I am a bit confused why the ObjectSpace.each_object use never produced a warning on JRuby. Perhaps that path is not followed for JRuby? In any case, it could be made to work on JRuby all the time, and still be equivalent logic, if you changed it to each_object(Class). |
jruby/jruby#4250 has been merged and will be in JRuby 9.1.6.0 (probably released in the next week). |
@headius: Many thanks. I've actually just removed |
We're planning to keep this issue open until we can verify that the Mocha tests pass in JRuby v9.1.6.0, ideally by adding JRuby to the Travis CI build (see #282). |
You could try adding jruby-head right now, so we can confirm master is green on mocha before release. |
This is to see whether jruby/jruby#4250 has solved the problem in #274.
I tried adding However, I then tried building JRuby at the HEAD commit and the Mocha build passes OK. I assume that for some reason the |
@floehopper Oh bother. Ok, I'll look into why it isn't updating. It's supposed to update every time we have a green build. |
I just restarted the build for the branch with
I've also just installed JRuby v9.1.6.0 and I see the same failures locally. I suspect they might be fixable by filtering out JRuby lines from the backtrace, so I'm going to investigate that now. |
I've fixed the assertion failures and updated the build matrix to use the latest stable version of JRuby in this new branch and the build passed. I plan to use this to open a PR and then to get it merged. |
Now that I've demonstrated that the Mocha build passes with the latest stable version of JRuby in #288, I'm going to close this issue. |
I came across these test failures when trying to merge PR #225.
It would appear that the changes in commit 43d5667 mean that we now have some test failures when using JRuby (I tested 9.0.0.0.pre2, 9.0.0.0 and 9.1.5.0).
The text was updated successfully, but these errors were encountered: