Skip to content

Commit

Permalink
Fix Jruby spec
Browse files Browse the repository at this point in the history
  • Loading branch information
chopraanmol1 committed Jan 11, 2019
1 parent 5ce39e0 commit ddcf495
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/roo/helpers/weak_instance_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def instance_cache(key)
end

def instance_cache_finalizer(key)
proc do
if instance_variable_defined?(key) && (ref = instance_variable_get(key)) && !ref.weakref_alive?
proc do |object_id|
if instance_variable_defined?(key) && (ref = instance_variable_get(key)) && (!ref.weakref_alive? || ref.__getobj__.object_id == object_id)
remove_instance_variable(key)
end
end
Expand Down
43 changes: 32 additions & 11 deletions spec/lib/roo/weak_instance_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
require 'spec_helper'

if RUBY_PLATFORM == "java"
require 'java'
java_import 'java.lang.System'
end

describe Roo::Helpers::WeakInstanceCache do
let(:klass) do
Class.new do
include Roo::Helpers::WeakInstanceCache

def memoized_data
instance_cache(:@memoized_data) do
"Some Costly Operation" * 1_000
"Some Costly Operation #{rand(1000)}" * 1_000
end
end
end
Expand All @@ -30,21 +35,25 @@ def memoized_data
end

it 'should recalculate after GC' do
expect(subject.instance_variables).to_not include(:@memoized_data)
GC.disable
original_id = subject.memoized_data.object_id
expect(subject.memoized_data.object_id).to eq(original_id)
subject.memoized_data && nil
expect(subject.instance_variables).to include(:@memoized_data)

GC.start
expect(subject.memoized_data.object_id).not_to eq(original_id)
force_gc
expect(subject.instance_variables).to_not include(:@memoized_data)
GC.disable
subject.memoized_data && nil
expect(subject.instance_variables).to include(:@memoized_data)
end

it 'must remove instance variable' do
expect(subject.instance_variables).to_not include(:@memoized_data)
GC.disable
subject.memoized_data
subject.memoized_data && nil
expect(subject.instance_variables).to include(:@memoized_data)

GC.start
force_gc
expect(subject.instance_variables).to_not include(:@memoized_data)
end

Expand All @@ -54,16 +63,28 @@ def memoized_data
end
it 'after calculation' do
GC.disable
subject.memoized_data
subject.memoized_data && nil
expect{subject.inspect}.to_not raise_error
expect(subject.inspect).to include("Some Costly Operation")
GC.start
force_gc
end
it 'after GC' do
subject.memoized_data
GC.start
subject.memoized_data && nil
force_gc
expect(subject.instance_variables).to_not include(:@memoized_data)
expect{subject.inspect}.to_not raise_error
expect(subject.inspect).to_not include("Some Costly Operation")
end
end

if RUBY_PLATFORM == "java"
def force_gc
System.gc
sleep(0.1)
end
else
def force_gc
GC.start
end
end
end

0 comments on commit ddcf495

Please sign in to comment.