Permalink
Browse files

Add before_fork hook

Creates a callback which is called before each specs before the spork
process is forked.
  • Loading branch information...
1 parent 2bb3143 commit 98de8978c3f719325524a54637dc242e391d9743 @tom-kuca committed Oct 6, 2011
Showing with 43 additions and 5 deletions.
  1. +26 −0 lib/spork.rb
  2. +2 −1 lib/spork/run_strategy/forking.rb
  3. +15 −4 spec/spork_spec.rb
View
@@ -23,6 +23,20 @@ def prefork(prevent_double_run = true, &block)
return if prevent_double_run && already_ran?(caller.first)
yield
end
+
+ # Run a block BEFORE the fork occurs. By default, if before_fork is called twice in the same file and line number, the supplied block will only be ran once.
+ #
+ # == Parameters
+ #
+ # * +prevent_double_run+ - Pass false to disable double run prevention
+ def before_fork(prevent_double_run = true, &block)
+ return if prevent_double_run && already_ran?(caller.first)
+ if state == :prefork
+ before_fork_procs << block
+ else
+ yield
+ end
+ end
# Run a block AFTER the fork occurs. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
#
@@ -61,6 +75,14 @@ def exec_prefork(&block)
@state = :prefork
yield
end
+
+ # Used by the server. Called to run all of the before_fork blocks.
+ def exec_before_fork(&block)
+ @state = :before_fork
+ before_fork_procs.each { |p| p.call }
+ before_fork_procs.clear
+ yield if block_given?
+ end
# Used by the server. Called to run all of the prefork blocks.
def exec_each_run(&block)
@@ -148,6 +170,10 @@ def each_run_procs
@each_run_procs ||= []
end
+ def before_fork_procs
+ @before_fork_procs ||= []
+ end
+
def after_each_run_procs
@after_each_run_procs ||= []
end
@@ -6,6 +6,7 @@ def self.available?
def run(argv, stderr, stdout)
abort if running?
+ Spork.exec_before_fork
@child = ::Spork::Forker.new do
$stdout, $stderr = stdout, stderr
load test_framework.helper_file
@@ -32,4 +33,4 @@ def running?
def assert_ready!
raise RuntimeError, "This process hasn't loaded the environment yet by loading the prefork block" unless Spork.using_spork?
end
-end
+end
View
@@ -5,6 +5,7 @@ def self.reset!
@state = nil
@using_spork = false
@already_ran = nil
+ @before_fork_procs = nil
@each_run_procs = nil
end
end
@@ -19,6 +20,10 @@ def spec_helper_simulator
Spork.prefork do
@ran << :prefork
end
+
+ Spork.before_fork do
+ @ran << :before_fork
+ end
Spork.each_run do
@ran << :each_run
@@ -35,27 +40,33 @@ def spec_helper_simulator
Spork.exec_prefork { spec_helper_simulator }
@ran.should == [:prefork]
+ Spork.exec_before_fork
+ @ran.should == [:prefork, :before_fork]
+
Spork.exec_each_run
- @ran.should == [:prefork, :each_run]
+ @ran.should == [:prefork, :before_fork, :each_run]
end
it "runs both blocks when Spork not activated" do
- spec_helper_simulator.should == [:prefork, :each_run]
+ spec_helper_simulator.should == [:prefork, :before_fork, :each_run]
end
it "prevents blocks from being ran twice" do
Spork.exec_prefork { spec_helper_simulator }
+ Spork.exec_before_fork
Spork.exec_each_run
@ran.clear
Spork.exec_prefork { spec_helper_simulator }
+ Spork.exec_before_fork
Spork.exec_each_run
@ran.should == []
end
- it "runs multiple prefork and each_run blocks at different locations" do
+ it "runs multiple prefork, before_fork and each_run blocks at different locations" do
Spork.prefork { }
+ Spork.before_fork { }
Spork.each_run { }
- spec_helper_simulator.should == [:prefork, :each_run]
+ spec_helper_simulator.should == [:prefork, :before_fork, :each_run]
end
it "expands a caller line, preserving the line number" do

0 comments on commit 98de897

Please sign in to comment.