Sha256: 941e5b10aa45173bd820aaf6033ebb9806c12160eeb7c29b23bb1f1913208468

Contents?: true

Size: 1.17 KB

Versions: 14

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

require_relative 'wrapper'

module Covered
	class Forks < Wrapper
		def start
			super
			
			Handler.start(self)
		end
		
		def finish
			Handler.finish
			
			super
		end
		
		module Handler
			LOCK = Mutex.new
			
			class << self
				attr :coverage
				
				def start(coverage)
					LOCK.synchronize do
						if @coverage
							raise ArgumentError, "Coverage is already being tracked!"
						end
						
						@coverage = coverage
					end
				end
				
				def finish
					LOCK.synchronize do
						@coverage = nil
					end
				end
				
				def after_fork
					return unless coverage = Handler.coverage
					pid = Process.pid
					
					# Any pre-existing coverage is being tracked by the parent process, so discard it.
					coverage.clear
					
					at_exit do
						# Don't break forked children:
						if Process.pid == pid
							coverage.finish
						end
					end
				end
			end
			
			def _fork
				pid = super
				
				if pid.zero?
					Handler.after_fork
				end
				
				return pid
			end
			
			::Process.singleton_class.prepend(self)
		end
		
		private_constant :Handler
	end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
covered-0.26.0 lib/covered/forks.rb
covered-0.25.1 lib/covered/forks.rb
covered-0.25.0 lib/covered/forks.rb
covered-0.24.3 lib/covered/forks.rb
covered-0.24.2 lib/covered/forks.rb
covered-0.24.1 lib/covered/forks.rb
covered-0.24.0 lib/covered/forks.rb
covered-0.23.0 lib/covered/forks.rb
covered-0.22.1 lib/covered/forks.rb
covered-0.22.0 lib/covered/forks.rb
covered-0.21.0 lib/covered/forks.rb
covered-0.20.2 lib/covered/forks.rb
covered-0.20.1 lib/covered/forks.rb
covered-0.20.0 lib/covered/forks.rb