module Util

Public Class Methods

guard_against_slow_spec(example) click to toggle source
# File lib/turtle_hunter/util.rb, line 6
  def self.guard_against_slow_spec(example)
    @example = example
    return unless passes_preconditions?

    fail_after = set_defaults
    started_at = example.metadata[:execution_result].started_at
    now = Time.now
    runtime = (now - started_at) * 1000

    if runtime > fail_after
      message = <<-HEREDOC
      Expected spec to run within #{fail_after} milliseconds but it took #{runtime} milliseconds.
      Lenience was set to `#{lenience?}`

      If you want to disable turtle_hunter for this run, set `ENV["FAIL_SLOW"]=false` while you run specs.

      If you want to skip turtle_hunter for this spec, add `#{SKIP_FLAG}: true` to the example.
      HEREDOC
      fail message
    end
  end

Private Class Methods

lenience?() click to toggle source
# File lib/turtle_hunter/util.rb, line 50
def self.lenience?
  LENIENCE_FLAG.present? && ENV[LENIENCE_FLAG].present?
end
passes_preconditions?() click to toggle source
# File lib/turtle_hunter/util.rb, line 30
def self.passes_preconditions?
  return if @example.nil?
  return if @example.metadata[SKIP_FLAG.to_sym] == true
  return if ENV["FAIL_SLOW"] != "true"
  true
end
set_defaults() click to toggle source
# File lib/turtle_hunter/util.rb, line 37
def self.set_defaults
  spec_type = @example.metadata[:type]
  time = case spec_type
  when :model then MODEL_THRESHOLD
  when :controller then CONTROLLER_THRESHOLD
  when :feature then FEATURE_THRESHOLD
  else ELSE_THRESHOLD
  end

  time *= LENIENCE_LEVEL.to_f if lenience?
  time
end