Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[m] Provide Spork.process_counter that increased for every run

  • Loading branch information...
commit 6546a50c6f5883e949ba8200426f8754ba63a4a4 1 parent 23d61ce
@phuongnd08 authored
Showing with 32 additions and 13 deletions.
  1. +30 −13 lib/spork.rb
  2. +2 −0  lib/spork/run_strategy/forking.rb
View
43 lib/spork.rb
@@ -13,7 +13,18 @@ module Spork
autoload :Diagnoser, (LIBDIR + 'spork/diagnoser').to_s
autoload :GemHelpers, (LIBDIR + 'spork/gem_helpers').to_s
+ @process_counter_semaphore = Mutex.new
+
class << self
+
+ def process_counter
+ @process_counter || 0
+ end
+
+ def process_counter=(counter)
+ @process_counter = counter
+ end
+
# Run a block, during prefork mode. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
#
# == Parameters
@@ -23,7 +34,7 @@ def prefork(prevent_double_run = true, &block)
return if prevent_double_run && already_ran?(caller.first)
yield
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.
#
# == Parameters
@@ -37,7 +48,13 @@ def each_run(prevent_double_run = true, &block)
yield
end
end
-
+
+ def increase_process_counter
+ @process_counter_semaphore.synchronize do
+ self.process_counter += 1
+ end
+ end
+
# Run a block after specs are run.
#
# == Parameters
@@ -55,13 +72,13 @@ def using_spork?
def state
@state ||= :not_using_spork
end
-
+
# Used by the server. Called when loading the prefork blocks of the code.
def exec_prefork(&block)
@state = :prefork
yield
end
-
+
# Used by the server. Called to run all of the prefork blocks.
def exec_each_run(&block)
@state = :run
@@ -70,7 +87,7 @@ def exec_each_run(&block)
each_run_procs.clear
yield if block_given?
end
-
+
# Used by the server. Called to run all of the after_each_run blocks.
def exec_after_each_run
# processes in reverse order similar to at_exit
@@ -81,9 +98,9 @@ def exec_after_each_run
# Traps an instance method of a class (or module) so any calls to it don't actually run until Spork.exec_each_run
def trap_method(klass, method_name)
method_name_without_spork, method_name_with_spork = alias_method_names(method_name, :spork)
-
+
klass.class_eval <<-EOF, __FILE__, __LINE__ + 1
- alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork})
+ alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork})
def #{method_name}(*args, &block)
Spork.each_run(false) do
#{method_name_without_spork}(*args, &block)
@@ -91,12 +108,12 @@ def #{method_name}(*args, &block)
end
EOF
end
-
+
# Same as trap_method, but for class methods instead
def trap_class_method(klass, method_name)
trap_method((class << klass; self; end), method_name)
end
-
+
def detect_and_require(subfolder)
([LIBDIR.to_s] + other_spork_gem_load_paths).uniq.each do |gem_path|
Dir.glob(File.join(gem_path, subfolder)).each { |file| require file }
@@ -123,11 +140,11 @@ def alias_method_names(method_name, feature)
/^(.+?)([\?\!]{0,1})$/.match(method_name.to_s)
["#{$1}_without_spork#{$2}", "#{$1}_with_spork#{$2}"]
end
-
+
def already_ran
@already_ran ||= []
end
-
+
def expanded_caller(caller_line)
file, line = caller_line.split(/:(\d+)/)
line.gsub(/:.+/, '')
@@ -137,13 +154,13 @@ def expanded_caller(caller_line)
end
expanded
end
-
+
def already_ran?(caller_script_and_line)
return true if already_ran.include?(expanded_caller(caller_script_and_line))
already_ran << expanded_caller(caller_script_and_line)
false
end
-
+
def each_run_procs
@each_run_procs ||= []
end
View
2  lib/spork/run_strategy/forking.rb
@@ -6,6 +6,8 @@ def self.available?
def run(argv, stderr, stdout)
abort if running?
+ Spork.increase_process_counter
+
@child = ::Spork::Forker.new do
$stdout, $stderr = stdout, stderr
load test_framework.helper_file
Please sign in to comment.
Something went wrong with that request. Please try again.