# frozen_string_literal: true

require "test_helper"
require "judoscale/request"
require "judoscale/config"

module Judoscale
  describe Request do
    let(:request) { Request.new(env, config) }
    let(:env) { {} }
    let(:config) { Config.instance }

    describe "#queue_time" do
      it "handles X_REQUEST_START in integer milliseconds (Heroku)" do
        started_at = Time.now - 2
        ended_at = started_at + 1
        env["HTTP_X_REQUEST_START"] = (started_at.to_f * 1000).to_i.to_s

        _(request.queue_time(ended_at)).must_be_within_delta 1000, 1
      end

      it "handles X_REQUEST_START in seconds with fractional milliseconds (nginx)" do
        started_at = Time.now - 2
        ended_at = started_at + 1
        env["HTTP_X_REQUEST_START"] = "t=#{format "%.3f", started_at.to_f}"

        _(request.queue_time(ended_at)).must_be_within_delta 1000, 1
      end
    end
  end
end

# frozen_string_literal: true

require "test_helper"
require "judoscale/request"
require "judoscale/config"

module Judoscale
  describe Request do
    let(:request) { Request.new(env, config) }
    let(:env) { {} }
    let(:config) { Config.instance }

    describe "#queue_time" do
      it "handles X_REQUEST_START in integer milliseconds (Heroku)" do
        started_at = Time.now - 2
        ended_at = started_at + 1
        env["HTTP_X_REQUEST_START"] = (started_at.to_f * 1000).to_i.to_s

        _(request.queue_time(ended_at)).must_be_within_delta 1000, 1
      end

      it "handles X_REQUEST_START in seconds with fractional milliseconds (nginx)" do
        started_at = Time.now - 2
        ended_at = started_at + 1
        env["HTTP_X_REQUEST_START"] = "t=#{format "%.3f", started_at.to_f}"

        _(request.queue_time(ended_at)).must_be_within_delta 1000, 1
      end

      it "subtracts the network time / request body wait available in puma from the queue time" do
        started_at = Time.now - 2
        ended_at = started_at + 1
        env["HTTP_X_REQUEST_START"] = (started_at.to_f * 1000).to_i.to_s
        env["puma.request_body_wait"] = 50

        _(request.queue_time(ended_at)).must_be_within_delta 950, 1
      end
    end
  end
end

Add test for queue time calc accounting for network time
Add test for queue time calc accounting for network time

The time spent in Puma waiting for the request body, or how we sometimes
call it "network time", is not included in the queue time for our
purposes of autoscaling here, this test helps ensure that behavior.
