Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Added basic specs
Browse files Browse the repository at this point in the history
  • Loading branch information
iconara committed Jul 9, 2012
1 parent 0968643 commit e7b7b94
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .rspec
@@ -0,0 +1,4 @@
-I lib
--color
--format doc
spec
7 changes: 7 additions & 0 deletions Gemfile
@@ -0,0 +1,7 @@
source :rubygems

gemspec

group :test do
gem 'rspec'
end
30 changes: 30 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,30 @@
PATH
remote: .
specs:
multimeter (1.0.0.pre0-java)
json
metrics-core-jars

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
json (1.7.3-java)
metrics-core-jars (2.1.2-java)
slf4j-jars (~> 1)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.0)
rspec-expectations (2.11.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.0)
slf4j-jars (1.6.2-java)

PLATFORMS
java

DEPENDENCIES
multimeter!
rspec
37 changes: 28 additions & 9 deletions lib/multimeter.rb
Expand Up @@ -244,31 +244,42 @@ def each_metric
end

def get(name)
@registry.all_metrics[name]
@registry.all_metrics[create_name(name)]
end

def gauge(name, options={}, &block)
if get(name) && block_given?
raise ArgumentError, %(Cannot redeclare gauge #{name})
end
@registry.new_gauge(create_name(name), ProcGauge.new(block))
end

def counter(name, options={})
@registry.new_counter(create_name(name))
error_translation do
@registry.new_counter(create_name(name))
end
end

def meter(name, options={})
event_type = (options[:event_type] || '').to_s
time_unit = TIME_UNITS[options[:time_unit] || :seconds]
@registry.new_meter(create_name(name), event_type, time_unit)
error_translation do
event_type = (options[:event_type] || '').to_s
time_unit = TIME_UNITS[options[:time_unit] || :seconds]
@registry.new_meter(create_name(name), event_type, time_unit)
end
end

def histogram(name, options={})
@registry.new_histogram(create_name(name), !!options[:biased])
error_translation do
@registry.new_histogram(create_name(name), !!options[:biased])
end
end

def timer(name, options={})
duration_unit = TIME_UNITS[options[:duration_unit] || :milliseconds]
rate_unit = TIME_UNITS[options[:rate_unit] || :seconds]
@registry.new_timer(create_name(name), duration_unit, rate_unit)
error_translation do
duration_unit = TIME_UNITS[options[:duration_unit] || :milliseconds]
rate_unit = TIME_UNITS[options[:rate_unit] || :seconds]
@registry.new_timer(create_name(name), duration_unit, rate_unit)
end
end

private
Expand All @@ -281,6 +292,14 @@ def timer(name, options={})
def create_name(name)
::Yammer::Metrics::MetricName.new(@group, @type, name.to_s)
end

def error_translation
begin
yield
rescue java.lang.ClassCastException => cce
raise ArgumentError, %(Cannot redeclare a metric as another type)
end
end
end

class ProcGauge < ::Yammer::Metrics::Gauge
Expand Down
1 change: 1 addition & 0 deletions multimeter.gemspec
Expand Up @@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.rubyforge_project = 'multimeter'

s.add_dependency 'metrics-core-jars'
s.add_dependency 'json'

s.files = FileList['lib/**/*.rb'].to_a
s.require_paths = %w[lib]
Expand Down
39 changes: 39 additions & 0 deletions spec/multimeter/registry_spec.rb
@@ -0,0 +1,39 @@
require_relative '../spec_helper'


module Multimeter
describe Registry do
let :registry do
Multimeter.registry('a_group', 'some_type')
end

context 'when creating metrics' do
[:counter, :meter, :histogram, :timer].each do |metric_type|
it "##{metric_type} creates a new #{metric_type}" do
metric = registry.send(metric_type, :some_name)
metric.should_not be_nil
end
end

it '#gauge creates a gauge' do
registry.gauge(:some_name) { 42 }
end

it 'caches metric objects' do
registry.counter(:some_name).should equal(registry.counter(:some_name))
end

it 'raises an error when a gauge is redeclared, but not when it is accessed without a block' do
g1 = registry.gauge(:some_name) { 42 }
g2 = registry.gauge(:some_name)
g1.should equal(g2)
expect { registry.gauge(:some_name) { 43 } }.to raise_error(ArgumentError)
end

it 'raises an error if a metric is redeclared as another type' do
registry.counter(:some_name)
expect { registry.meter(:some_name) }.to raise_error(ArgumentError)
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -0,0 +1 @@
require 'multimeter'
80 changes: 80 additions & 0 deletions spec/yammer/metrics_spec.rb
@@ -0,0 +1,80 @@
require_relative '../spec_helper'


module Yammer::Metrics
describe Yammer::Metrics do
let :registry do
Multimeter.registry('a_group', 'some_type')
end

describe Counter do
describe '#to_h' do
it 'returns a hash representation of the counter' do
c = registry.counter(:a_counter)
c.inc
c.to_h.should == {:count => 1}
end
end
end

describe Meter do
describe '#to_h' do
it 'returns a hash representation of the meter' do
m = registry.meter(:some_meter, :event_type => 'stuff')
m.mark
h = m.to_h
h[:event_type].should == 'stuff'
h[:count].should == 1
h[:mean_rate].should be_a(Numeric)
h[:one_minute_rate].should be_a(Numeric)
h[:five_minute_rate].should be_a(Numeric)
h[:fifteen_minute_rate].should be_a(Numeric)
end
end
end

describe Histogram do
it 'returns a hash representation of the histogram' do
hs = registry.histogram(:some_hist)
hs.update(4)
h = hs.to_h
h[:count].should == 1
h[:max].should be_a(Numeric)
h[:min].should be_a(Numeric)
h[:mean].should be_a(Numeric)
h[:std_dev].should be_a(Numeric)
h[:sum].should be_a(Numeric)
end
end

describe Timer do
describe '#to_h' do
it 'returns a hash representation of the timer' do
t = registry.timer(:some_timer)
t.measure { }
h = t.to_h
h[:event_type].should == 'calls'
h[:count].should == 1
h[:mean_rate].should be_a(Numeric)
h[:one_minute_rate].should be_a(Numeric)
h[:five_minute_rate].should be_a(Numeric)
h[:fifteen_minute_rate].should be_a(Numeric)
h[:max].should be_a(Numeric)
h[:min].should be_a(Numeric)
h[:mean].should be_a(Numeric)
h[:std_dev].should be_a(Numeric)
h[:sum].should be_a(Numeric)
end
end
end

describe Gauge do
describe '#to_h' do
it 'returns a hash representation of the gauge' do
g = registry.gauge(:some_gauge) { 42 }
g.to_h.should == {:value => 42}
end
end
end
end
end

0 comments on commit e7b7b94

Please sign in to comment.