<commit_before>require_relative "spec_helper"
require_relative "support/queue_helpers"

describe Consumer do
  include QueueHelpers

  let(:logger) { instance_double("Logger") }
  let(:delivery_info) do
    instance_double("Bunny::DeliveryInfo",
                    channel: instance_double("Bunny::Channel", reject: nil),
                    delivery_tag: nil)
  end
  let(:headers) { instance_double("Bunny::MessageProperties", content_type: "application/json") }

  describe "#run" do
    it "increments the counters on the statsd client" do
      statsd_client = StatsdClientMock.new
      stubs = create_bunny_stubs
      queue = stubs.queue

      expect(queue).to receive(:subscribe).and_yield(delivery_info, headers, "message_payload")

      processor = instance_double("HeartbeatProcessor")
      described_class.new(
        queue_name: "some-queue",
        processor: processor,
        rabbitmq_connection: stubs.connection,
        statsd_client: statsd_client,
        logger: logger,
      ).run

      expect(statsd_client.incremented_keys).to eql(["some-queue.started", "some-queue.discarded"])
    end

    it "increments the uncaught_exception counter for uncaught exceptions" do
      statsd_client = StatsdClientMock.new
      stubs = create_bunny_stubs
      queue = stubs.queue

      expect(queue).to receive(:subscribe).and_yield(delivery_info, headers, {}.to_json)

      processor = instance_double("HeartbeatProcessor")
      allow(processor).to receive(:process).and_raise("An expected exception generated by the test")
      expect(logger).to receive(:error)

      expect {
        described_class.new(queue_name: "some-queue", processor: processor, rabbitmq_connection: stubs.connection, statsd_client: statsd_client, logger: logger).run
      }.to raise_error(SystemExit)

      expect(statsd_client.incremented_keys).to eql(["some-queue.started", "some-queue.uncaught_exception"])
    end
  end

  class StatsdClientMock
    attr_reader :incremented_keys

    def initialize
      @incremented_keys = []
    end

    def increment(key)
      @incremented_keys << key
    end
  end
end<commit_msg>Replace constant defined in block with let variable<commit_after>require_relative "spec_helper"
require_relative "support/queue_helpers"

describe Consumer do
  include QueueHelpers

  let(:logger) { instance_double("Logger") }
  let(:delivery_info) do
    instance_double("Bunny::DeliveryInfo",
                    channel: instance_double("Bunny::Channel", reject: nil),
                    delivery_tag: nil)
  end
  let(:headers) { instance_double("Bunny::MessageProperties", content_type: "application/json") }
  let(:statsd_client_mock) do
    Class.new do
      attr_reader :incremented_keys

      def initialize
        @incremented_keys = []
      end

      def increment(key)
        @incremented_keys << key
      end
    end
  end

  describe "#run" do
    it "increments the counters on the statsd client" do
      statsd_client = statsd_client_mock.new
      stubs = create_bunny_stubs
      queue = stubs.queue

      expect(queue).to receive(:subscribe).and_yield(delivery_info, headers, "message_payload")

      processor = instance_double("HeartbeatProcessor")
      described_class.new(
        queue_name: "some-queue",
        processor: processor,
        rabbitmq_connection: stubs.connection,
        statsd_client: statsd_client,
        logger: logger,
      ).run

      expect(statsd_client.incremented_keys).to eql(["some-queue.started", "some-queue.discarded"])
    end

    it "increments the uncaught_exception counter for uncaught exceptions" do
      statsd_client = statsd_client_mock.new
      stubs = create_bunny_stubs
      queue = stubs.queue

      expect(queue).to receive(:subscribe).and_yield(delivery_info, headers, {}.to_json)

      processor = instance_double("HeartbeatProcessor")
      allow(processor).to receive(:process).and_raise("An expected exception generated by the test")
      expect(logger).to receive(:error)

      expect {
        described_class.new(queue_name: "some-queue", processor: processor, rabbitmq_connection: stubs.connection, statsd_client: statsd_client, logger: logger).run
      }.to raise_error(SystemExit)

      expect(statsd_client.incremented_keys).to eql(["some-queue.started", "some-queue.uncaught_exception"])
    end
  end
end
{'fraction_non_alphanumeric': 0.09703380087376408, 'fraction_numerical': 0.0, 'mean_word_length': 3.5741324921135647, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7537240', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1237, 'n_words': 267}