Sha256: 6c3a91d25c17caa80a90202e3ef791bfbd5b4812292fe95cc8b5f75d871ca27e

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

module Mutant
  # Module providing isolation
  module Isolation
    Error = Class.new(RuntimeError)

    module None

      # Call block in isolation
      #
      # @return [Object]
      #
      # @raise [Error]
      #   if block terminates abnormal
      #
      # @api private
      #
      def self.call(&block)
        block.call
      rescue => exception
        raise Error, exception
      end
    end

    module Fork

      # Call block in isolation
      #
      # This isolation implements the fork strategy.
      # Future strategies will probably use a process pool that can
      # handle multiple mutation kills, in-isolation at once.
      #
      # @return [Object]
      #   returns block execution result
      #
      # @raise [Error]
      #   if block terminates abnormal
      #
      # @api private
      #
      # rubocop:disable MethodLength
      #
      def self.call(&block)
        reader, writer = IO.pipe.map(&:binmode)

        pid = Process.fork do
          File.open('/dev/null', 'w') do |file|
            $stderr.reopen(file)
            reader.close
            writer.write(Marshal.dump(block.call))
            writer.close
          end
        end

        writer.close
        Marshal.load(reader.read)
      rescue => exception
        raise Error, exception
      ensure
        Process.waitpid(pid) if pid
      end

    end # Fork

  end # Isolator
end # Mutant

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mutant-0.8.0 lib/mutant/isolation.rb
mutant-0.7.9 lib/mutant/isolation.rb
mutant-0.7.8 lib/mutant/isolation.rb
mutant-0.7.7 lib/mutant/isolation.rb
mutant-0.7.6 lib/mutant/isolation.rb
mutant-0.7.5 lib/mutant/isolation.rb
mutant-0.7.4 lib/mutant/isolation.rb