# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic
  module Agent
    class AdaptiveSamplerTest < Minitest::Test
      include MultiverseHelpers

      setup_and_teardown_agent do
        NewRelic::Agent.config.add_config_for_testing(:'distributed_tracing.enabled' => true)
        # hard reset on the adaptive_sampler
        NewRelic::Agent.instance.instance_variable_set(:@adaptive_sampler, AdaptiveSampler.new)
      end

      def test_adaptive_sampler_valid_stats_and_reset_after_harvest
        nr_freeze_process_time

        sampled_count = 0
        20.times do |i|
          in_transaction("test_txn_#{i}") do |txn|
            sampled_count += 1 if txn.sampled?
          end
        end

        stats = NewRelic::Agent.instance.adaptive_sampler.stats

        assert_equal 0, stats[:seen_last]
        assert_equal 20, stats[:seen]
        assert_equal sampled_count, stats[:sampled_count]

        advance_process_time(60)

        in_transaction('test_txn_20') {}

        stats = NewRelic::Agent.instance.adaptive_sampler.stats

        assert_equal 1, stats[:seen]
        assert_equal 20, stats[:seen_last]
      end
    end
  end
end
