Skip to content
This repository
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
Mar 28, 2011
David Chelimsky Set each example's instance of the group to nil after processing.
This dereferences the example group instance which contains all of the
example's state, thereby releasing each example for garbage collection
after processing.

Note that this does not dereference state initialized in before(:all),
but those instance variables are cleared out in a separate process.
dc684ae
David Chelimsky nullify ivars after each example f6cb592
5  lib/rspec/core/example.rb
@@ -58,7 +58,10 @@ def run(example_group_instance, reporter)
58 58
         rescue Exception => e
59 59
           set_exception(e)
60 60
         ensure
61  
-          @example_group_instance.example = nil
  61
+          @example_group_instance.instance_variables.each do |ivar|
  62
+            @example_group_instance.instance_variable_set(ivar, nil)
  63
+          end
  64
+          @example_group_instance = nil
62 65
 
63 66
           begin
64 67
             assign_auto_description
37  spec/rspec/core/example_spec.rb
@@ -93,6 +93,14 @@ def assert(val)
93 93
   end
94 94
 
95 95
   describe "#run" do
  96
+    it "sets its reference to the example group instance to nil" do
  97
+      group = RSpec::Core::ExampleGroup.describe do
  98
+        example('example') { 1.should eq(1) }
  99
+      end
  100
+      group.run
  101
+      group.examples.first.instance_variable_get("@example_group_instance").should be_nil
  102
+    end
  103
+
96 104
     it "runs after(:each) when the example passes" do
97 105
       after_run = false
98 106
       group = RSpec::Core::ExampleGroup.describe do
@@ -168,6 +176,35 @@ def assert(val)
168 176
         "around (after)"
169 177
       ])
170 178
     end
  179
+
  180
+    context "clearing ivars" do
  181
+      it "sets ivars to nil to prep them for GC" do
  182
+        group = RSpec::Core::ExampleGroup.describe do
  183
+          before(:all)  { @before_all  = :before_all }
  184
+          before(:each) { @before_each = :before_each }
  185
+          after(:each)  { @after_each = :after_each }
  186
+          after(:all)   { @after_all  = :after_all }
  187
+        end
  188
+        example = group.example("does something") do
  189
+          @in_example = :in_example
  190
+        end
  191
+        example_group_instance = group.new
  192
+        example.run(example_group_instance, double('reporter').as_null_object)
  193
+
  194
+        %w[@before_all @before_each @after_each @after_all].each do |ivar|
  195
+          example_group_instance.instance_variable_get(ivar).should be_nil
  196
+        end
  197
+      end
  198
+
  199
+      it "does not impact the before_all_ivars which are copied to each example" do
  200
+        group = RSpec::Core::ExampleGroup.describe do
  201
+          before(:all) { @before_all = "abc" }
  202
+          example("first") { @before_all.should_not be_nil }
  203
+          example("second") { @before_all.should_not be_nil }
  204
+        end
  205
+        group.run.should be_true
  206
+      end
  207
+    end
171 208
   end
172 209
 
173 210
   describe "#pending" do

No commit comments for this range

Something went wrong with that request. Please try again.