Created
December 16, 2009 19:22
-
-
Save rsim/258095 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Spec::Runner.configure do |config| | |
config.extend(DataMapper::Spec::AdapterHelpers) | |
config.include(DataMapper::Spec::PendingHelpers) | |
config.after :all do | |
# global model cleanup | |
descendants = DataMapper::Model.descendants.to_a | |
while model = descendants.shift | |
descendants.concat(model.descendants.to_a - [ model ]) | |
parts = model.name.split('::') | |
constant_name = parts.pop.to_sym | |
base = parts.empty? ? Object : Object.full_const_get(parts.join('::')) | |
if base.const_defined?(constant_name) | |
base.send(:remove_const, constant_name) | |
end | |
DataMapper::Model.descendants.delete(model) | |
# remove descendants from model to avoid circular references | |
model.instance_variable_set(:@descendants, nil) | |
end | |
# clear instance variables of all DataMapper objects that were not properly garbage collected | |
[DataMapper::Query, DataMapper::Query::Direction, DataMapper::Collection, | |
DataMapper::Associations::Relationship, DataMapper::PropertySet, DataMapper::Property].each do |klass| | |
ObjectSpace.each_object(klass) do |o| | |
unless o.frozen? | |
o.instance_variables.each do |iv| | |
o.instance_variable_set(iv, nil) | |
end | |
end | |
end | |
end | |
# Report memory usage | |
ObjectSpace.garbage_collect | |
class_count = {} | |
ObjectSpace.each_object do |o| | |
class_name = o.class.name | |
if class_name =~ /^(DataMapper|Blog)/ | |
class_count[class_name] = (class_count[class_name] || 0) + 1 | |
end | |
end | |
puts class_count.to_a.sort_by{|k,v| -v}.map{|k,v| "#{k} #{v}"}.join(" <br/>\n")+"<br>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment