<commit_before>require "spec_helper"
require "walmart_open/connection_manager"
require "walmart_open/client"
require "walmart_open/product_request"


describe WalmartOpen::ConnectionManager do
  context "#request" do
    context "when product request" do
      it "did not sleep as did not exceed calls per second threshold" do
        client = WalmartOpen::Client.new({product_calls_per_second: 1})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::ProductRequest.new
        expect(request_object).to receive(:submit).and_return("").once
        expect(connection).not_to receive(:sleep)

        connection.request(request_object)
      end

      it "slept due to exceeding calls per second threshold" do
        client = WalmartOpen::Client.new({product_calls_per_second: 3})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::ProductRequest.new
        expect(request_object).to receive(:submit).and_return("").at_least(:once)
        expect(connection).to receive(:sleep).once

        connection.request(request_object)  # request four times in one second
        connection.request(request_object)
        connection.request(request_object)
        connection.request(request_object)
      end
    end

    context "when commerce request" do
      it "did not sleep as did not exceed calls per second threshold" do
        client = WalmartOpen::Client.new({commerce_calls_per_second: 1})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::CommerceRequest.new
        expect(request_object).to receive(:submit).and_return("").once
        expect(connection).not_to receive(:sleep)
        connection.request(request_object)
      end

      it "slept due to exceeding calls per second threshold" do
        client = WalmartOpen::Client.new({commerce_calls_per_second: 3})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::CommerceRequest.new
        expect(request_object).to receive(:submit).and_return("").at_least(:once)
        expect(connection).to receive(:sleep).once

        connection.request(request_object)   # request four times in one second
        connection.request(request_object)
        connection.request(request_object)
        connection.request(request_object)
      end
    end
  end
end

<commit_msg>Update connection_manager_spec.rb to use Timecop to more accurately test throttle.<commit_after>require "spec_helper"
require "walmart_open/connection_manager"
require "walmart_open/client"
require "walmart_open/product_request"
require "timecop"

describe WalmartOpen::ConnectionManager do
  context "#request" do
    context "when product request" do
      it "did not sleep as did not exceed calls per second threshold" do
        client = WalmartOpen::Client.new({product_calls_per_second: 1})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::ProductRequest.new
        expect(request_object).to receive(:submit).and_return("").once
        expect(connection).not_to receive(:sleep)

        connection.request(request_object)
      end

      it "slept due to exceeding calls per second threshold" do
        client = WalmartOpen::Client.new({product_calls_per_second: 1})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::ProductRequest.new
        expect(request_object).to receive(:submit).and_return("").at_least(:once)
        expect(connection).to receive(:sleep).with(1).twice  # two requests slept

        Timecop.freeze(Time.now) do
          connection.request(request_object)  # request three times in one second
          connection.request(request_object)
          connection.request(request_object)
        end
      end
    end

    context "when commerce request" do
      it "did not sleep as did not exceed calls per second threshold" do
        client = WalmartOpen::Client.new({commerce_calls_per_second: 1})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::CommerceRequest.new
        expect(request_object).to receive(:submit).and_return("").once
        expect(connection).not_to receive(:sleep)
        connection.request(request_object)
      end

      it "slept due to exceeding calls per second threshold" do
        client = WalmartOpen::Client.new({commerce_calls_per_second: 1})
        connection = WalmartOpen::ConnectionManager.new(client)
        request_object = WalmartOpen::CommerceRequest.new
        expect(request_object).to receive(:submit).and_return("").at_least(:once)
        expect(connection).to receive(:sleep).with(1).twice  # two requests slept

        Timecop.freeze(Time.now) do
          connection.request(request_object)  # request three times in one second
          connection.request(request_object)
          connection.request(request_object)
        end
      end
    end
  end
end


{'fraction_non_alphanumeric': 0.08948723025143536, 'fraction_numerical': 0.0019798059790140567, 'mean_word_length': 3.5927272727272728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5890712', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1299, 'n_words': 309}