class Mutiny::Isolation

This code originally based on Markus Schirp’s implementation of Mutant::Isolation::Fork

https://github.com/mbj/mutant/blob/master/lib/mutant/isolation.rb

Constants

Error
Pipe

An inter-process communication mechanism for sending and receiving (marshalled) data over an IO pipe

Attributes

isolated_code[R]

Public Class Methods

call(&block) click to toggle source

Runs the given block, isolating the global state so that changes cannot leak out to the caller’s runtime

# File lib/mutiny/isolation.rb, line 10
def self.call(&block)
  new(block).run_in_isolation
rescue => exception
  raise Error, exception
end
new(isolated_code) click to toggle source
# File lib/mutiny/isolation.rb, line 18
def initialize(isolated_code)
  @isolated_code = isolated_code
end

Public Instance Methods

run_and_send_result_via(comms) click to toggle source
# File lib/mutiny/isolation.rb, line 33
def run_and_send_result_via(comms)
  Vacuum.silence($stderr) do
    result = isolated_code.call
    comms.send(result) # send the result to the parent process over the pipes
  end
end
run_in_isolation() click to toggle source
# File lib/mutiny/isolation.rb, line 22
def run_in_isolation
  Pipe.with do |comms|
    begin
      pid = Process.fork { run_and_send_result_via(comms) }
      comms.receive # wait to receive the result form the child process
    ensure
      Process.waitpid(pid) if pid
    end
  end
end