Skip to content

Commit

Permalink
[m] Provide Spork.process_counter that increased for every run
Browse files Browse the repository at this point in the history
  • Loading branch information
phuongnd08 committed Feb 18, 2012
1 parent 23d61ce commit 6546a50
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lib/spork.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -81,22 +98,22 @@ 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)
end
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 }
Expand All @@ -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(/:.+/, '')
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/spork/run_strategy/forking.rb
Expand Up @@ -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
Expand Down

0 comments on commit 6546a50

Please sign in to comment.