Skip to content

Commit

Permalink
Add type assertion for sample_rate
Browse files Browse the repository at this point in the history
  • Loading branch information
MalazAlamir committed May 19, 2016
1 parent aa2bd95 commit fada7c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/statsd/instrument/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def assert_statsd_call(metric_type, metric_name, options = {}, &block)

metrics.each do |metric|

assert within_numeric_range?(metric.sample_rate), "Unexpected sample rate type for metric #{metric_name}, must be numeric"
assert_equal options[:sample_rate], metric.sample_rate, "Unexpected StatsD sample rate for metric #{metric_name}" if options[:sample_rate]
assert_equal options[:value], metric.value, "Unexpected value submitted for StatsD metric #{metric_name}" if options[:value]

Expand All @@ -66,4 +67,8 @@ def assert_statsd_call(metric_type, metric_name, options = {}, &block)
metric
end
end

def within_numeric_range?(object)
object.kind_of?(Numeric) && (0.0..1.0).cover?(object)
end
end
8 changes: 8 additions & 0 deletions test/assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ def test_assert_statsd_call_with_tags
end
end

def test_assert_statsd_call_with_wrong_sample_rate_type
assert_assertion_triggered "Unexpected sample rate type for metric counter, must be numeric" do
@test_case.assert_statsd_increment('counter', tags: ['a', 'b']) do
StatsD.increment('counter', sample_rate: 'abc', tags: ['a', 'b'])
end
end
end

def test_nested_assertions
assert_no_assertion_triggered do
@test_case.assert_statsd_increment('counter1') do
Expand Down

0 comments on commit fada7c8

Please sign in to comment.