Sha256: 0eb99dca58773cc157a6dd7e4dccdb507b29605f82e497c209a5c8086ff1c58d

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

require "tempfile"

module Busted
  class Tracer

    class FinishedException < Exception; end
    class MissingCommandError < StandardError; end

    def self.exists?
      system "hash dtrace 2>/dev/null"
    end

    def initialize
      @lines = ""
    end

    def start
      spawn
      wait until started?
    end

    def finish
      final_probe
      wait until finished?
      kill
    ensure
      clean_up
    end

    def report
      lines.split("\n").each_with_object({method: []}) do |line, result|
        next if line =~ /\ABusted/
        trace = line.split
        result[:method] << { class: trace[0], sourcefile: trace[1], lineno: trace[2] }
      end
    end

    private

    attr_accessor :lines
    attr_reader :child_pid

    def wait
      sleep 0.1
    end

    def started?
      !(lines << log.read).empty?
    end

    def finished?
      (lines << log.read) =~ /Busted::Tracer::FinishedException/
    end

    def final_probe
      raise FinishedException
    rescue FinishedException
    end

    def spawn
      @child_pid = Process.spawn command, STDERR => STDOUT
    end

    def kill
      `sudo kill -TERM #{child_pid}`
    end

    def parent_pid
      Process.pid
    end

    def probe
      File.expand_path "../../../dtrace/probes/busted/method-cache-clear.d", __FILE__
    end

    def log
      @log ||= Tempfile.new "busted-dtrace.log"
    end

    def clean_up
      log.close!
    end

    def command
      "sudo dtrace -q -o #{log.path} -s #{probe} -p #{parent_pid}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
busted-0.2.3 lib/busted/tracer.rb
busted-0.2.2 lib/busted/tracer.rb
busted-0.2.1 lib/busted/tracer.rb
busted-0.2.0 lib/busted/tracer.rb
busted-0.1.0 lib/busted/tracer.rb