require 'minitest/autorun'
require 'minitest/pride'

ENV['RACK_ENV'] = 'test'
$TESTING = true

require 'bundler/setup'
require 'rack/test'

require 'sidekiq'
require 'sidekiq-benchmark'

require 'delorean'
require 'pry'

Bundler.require

Sidekiq.configure_server do |cfg|
  cfg.redis = {url: "redis://#{ENV['REDIS_HOST'] || 'localhost'}/15"}
end

Sidekiq.configure_client do |cfg|
  cfg.redis = {url: "redis://#{ENV['REDIS_HOST'] || 'localhost'}/15"}
end

module Sidekiq
  module Benchmark
    module Test

      class WorkerMock
        include Sidekiq::Worker
        include Sidekiq::Benchmark::Worker

        attr_reader :bm_obj, :metric_names, :assigned_metric, :counter

        def initialize
          @assigned_metric = 0.1
          @counter = 0

          benchmark do |bm|
            bm.test_metric do
              2.times do |i|
                bm.send("nested_test_metric_#{i}") do
                  Delorean.jump 1
                  @counter += 100500
                end
              end
            end

            bm.assigned_metric @assigned_metric
          end

          @metric_names = [:test_metric, :nested_test_metric_1, :job_time]
        end
      end

      class AlterWorkerMock < WorkerMock
        def initialize
          benchmark.test_metric do
            Delorean.jump 1
          end

          benchmark.other_metric do
            Delorean.jump 1
          end

          @metric_names = [:test_metric, :other_metric]
        end

        def multiply(a, b)
          a * b
        end

        def finish
          benchmark.finish
        end
      end

      class ContinuingWorkerMock < WorkerMock
        def initialize
          benchmark do |bm|
            bm.continued_metric do
              Delorean.jump 1
            end

            bm.continued_metric do
              Delorean.jump 1
            end
          end
        end
      end

      def self.flush_db
        Sidekiq.redis do |conn|
          conn.flushdb
        end
      end

    end
  end
end
