<commit_before># frozen_string_literal: true

require "test_helper"
require "judoscale/que/metrics_collector"
require "securerandom"

module Judoscale
  describe Que::MetricsCollector do
    def enqueue(queue, run_at)
      ActiveRecord::Base.connection.insert <<~SQL
        INSERT INTO que_jobs (job_class, queue, run_at)
        VALUES ('TestJob', '#{queue}', '#{run_at.iso8601(6)}')
      SQL
    end

    subject { Que::MetricsCollector.new }

    describe "#collect" do
      after {
        ActiveRecord::Base.connection.execute("DELETE FROM que_jobs")
        subject.clear_queues
      }

      it "collects latency for each queue" do
        enqueue("default", Time.now - 11)
        enqueue("high", Time.now - 22.2222)

        metrics = subject.collect

        _(metrics.size).must_equal 2
        _(metrics[0].queue_name).must_equal "default"
        _(metrics[0].value).must_be_within_delta 11000, 5
        _(metrics[0].identifier).must_equal :qt
        _(metrics[1].queue_name).must_equal "high"
        _(metrics[1].value).must_be_within_delta 22222, 5
        _(metrics[1].identifier).must_equal :qt
      end

      it "logs debug information for each queue being collected" do
        use_config log_level: :debug do<commit_msg>Increase the delta for checking Que latency tests<commit_after># frozen_string_literal: true

require "test_helper"
require "judoscale/que/metrics_collector"
require "securerandom"

module Judoscale
  describe Que::MetricsCollector do
    def enqueue(queue, run_at)
      ActiveRecord::Base.connection.insert <<~SQL
        INSERT INTO que_jobs (job_class, queue, run_at)
        VALUES ('TestJob', '#{queue}', '#{run_at.iso8601(6)}')
      SQL
    end

    subject { Que::MetricsCollector.new }

    describe "#collect" do
      after {
        ActiveRecord::Base.connection.execute("DELETE FROM que_jobs")
        subject.clear_queues
      }

      it "collects latency for each queue" do
        enqueue("default", Time.now - 11)
        enqueue("high", Time.now - 22.2222)

        metrics = subject.collect

        _(metrics.size).must_equal 2
        _(metrics[0].queue_name).must_equal "default"
        _(metrics[0].value).must_be_within_delta 11000, 10
        _(metrics[0].identifier).must_equal :qt
        _(metrics[1].queue_name).must_equal "high"
        _(metrics[1].value).must_be_within_delta 22222, 10
        _(metrics[1].identifier).must_equal :qt
      end

      it "logs debug information for each queue being collected" do
        use_config log_level: :debug do
{'fraction_non_alphanumeric': 0.10874704491725769, 'fraction_numerical': 0.026004728132387706, 'mean_word_length': 3.0429936305732483, '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': '4813809', 'n_tokens_mistral': 831, 'n_tokens_neox': 758, 'n_words': 180}