<commit_before>require 'test_helper'

class StatsDTest < Test::Unit::TestCase

  def setup
    StatsD.stubs(:rand).returns(0.0)

    UDPSocket.stubs(:new).returns(@socket = mock('socket'))
    @socket.stubs(:connect)
    @socket.stubs(:send)
    StatsD.invalidate_socket

    StatsD.stubs(:logger).returns(@logger = mock('logger'))
    @logger.stubs(:info)
    @logger.stubs(:error)
  end

  def test_statsd_measure_with_explicit_value
    StatsD.expects(:collect).with(:ms, 'values.foobar', 42, {})
    StatsD.measure('values.foobar', 42)
  end

  def test_statsd_measure_with_explicit_value_and_sample_rate
    StatsD.expects(:collect).with(:ms, 'values.foobar', 42, :sample_rate => 0.1)
    StatsD.measure('values.foobar', 42, :sample_rate => 0.1)
  end

  def test_statsd_measure_with_benchmarked_value
    Benchmark.stubs(:realtime).returns(1.12)
    StatsD.expects(:collect).with(:ms, 'values.foobar', 1120.0, {})
    StatsD.measure('values.foobar') do
      #noop
    end
  end

  def test_statsd_measure_executes_and_returns_block
    StatsD.expects(:collect).with(:ms, 'values.foobar', instance_of(Float), :sample_rate => 1.0)
    return_value = StatsD.measure('values.foobar', sample_rate: 1.0) do
      'foo'
    end

    assert_equal 'foo', return_value
  end

  def test_statsd_measure_with_benchmarked_value_and_options<commit_msg>Another 1.8 syntax fix :/<commit_after>require 'test_helper'

class StatsDTest < Test::Unit::TestCase

  def setup
    StatsD.stubs(:rand).returns(0.0)

    UDPSocket.stubs(:new).returns(@socket = mock('socket'))
    @socket.stubs(:connect)
    @socket.stubs(:send)
    StatsD.invalidate_socket

    StatsD.stubs(:logger).returns(@logger = mock('logger'))
    @logger.stubs(:info)
    @logger.stubs(:error)
  end

  def test_statsd_measure_with_explicit_value
    StatsD.expects(:collect).with(:ms, 'values.foobar', 42, {})
    StatsD.measure('values.foobar', 42)
  end

  def test_statsd_measure_with_explicit_value_and_sample_rate
    StatsD.expects(:collect).with(:ms, 'values.foobar', 42, :sample_rate => 0.1)
    StatsD.measure('values.foobar', 42, :sample_rate => 0.1)
  end

  def test_statsd_measure_with_benchmarked_value
    Benchmark.stubs(:realtime).returns(1.12)
    StatsD.expects(:collect).with(:ms, 'values.foobar', 1120.0, {})
    StatsD.measure('values.foobar') do
      #noop
    end
  end

  def test_statsd_measure_executes_and_returns_block
    StatsD.expects(:collect).with(:ms, 'values.foobar', instance_of(Float), :sample_rate => 1.0)
    return_value = StatsD.measure('values.foobar', :sample_rate => 1.0) do
      'foo'
    end

    assert_equal 'foo', return_value
  end

  def test_statsd_measure_with_benchmarked_value_and_options
{'fraction_non_alphanumeric': 0.13863216266173753, 'fraction_numerical': 0.019963031423290204, 'mean_word_length': 4.522448979591837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '7278283', 'n_tokens_mistral': 1055, 'n_tokens_neox': 958, 'n_words': 141}