Permalink
Browse files

Add specs.

  • Loading branch information...
1 parent b75da34 commit b0ca6f2160511db3f211d841943e2db57db08dce @pelletier pelletier committed May 8, 2012
Showing with 134 additions and 22 deletions.
  1. +3 −0 Gemfile
  2. +25 −0 Gemfile.lock
  3. +5 −1 lib/simple-graphite.rb
  4. +7 −4 simple-graphite.gemspec
  5. +43 −0 spec/simple-graphite_spec.rb
  6. +51 −0 spec/spec_helper.rb
  7. +0 −7 test/simple-graphite_test.rb
  8. +0 −10 test/test_helper.rb
View
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gemspec
View
@@ -0,0 +1,25 @@
+PATH
+ remote: .
+ specs:
+ simple-graphite (1.1.1)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ diff-lcs (1.1.3)
+ rspec (2.10.0)
+ rspec-core (~> 2.10.0)
+ rspec-expectations (~> 2.10.0)
+ rspec-mocks (~> 2.10.0)
+ rspec-core (2.10.0)
+ rspec-expectations (2.10.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.10.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rspec
+ rspec-mocks
+ simple-graphite!
View
@@ -12,14 +12,18 @@ def initialize(options = {})
end
def push_to_graphite
+ raise "You need to provide both the hostname and the port" if @host.nil? || @port.nil?
socket = TCPSocket.new(@host, @port)
yield socket
socket.close
end
def send_metrics(metrics_hash)
current_time = time_now
- push_to_graphite {(metrics_hash.map { |k,v| [k, v, current_time, '\n'] }).join(' ')}
+ push_to_graphite do |graphite|
+ graphite.puts((metrics_hash.map { |k,v| [k, v, current_time].join(' ') + "\n" }).join(''))
+ end
+ current_time
end
def hostname
View
@@ -18,19 +18,22 @@ Gem::Specification.new do |s|
]
s.files = [
"lib/simple-graphite.rb",
- "test/simple-graphite_test.rb",
- "test/test_helper.rb"
+ "spec/simple-graphite_test.rb",
+ "spec/test_helper.rb"
]
s.homepage = %q{http://github.com/imeyer/simple-graphite}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{Simple hook into graphite}
s.test_files = [
- "test/test_helper.rb",
- "test/simple-graphite_test.rb"
+ "spec/test_helper.rb",
+ "spec/simple-graphite_test.rb"
]
+ s.add_development_dependency "rspec"
+ s.add_development_dependency "rspec-mocks"
+
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
@@ -0,0 +1,43 @@
+require 'spec_helper'
+require 'simple-graphite'
+
+
+describe Graphite do
+
+ it "has a default port" do
+ a = Graphite.new
+ a.port.should == 2003
+ end
+
+ it "needs to have the hostname and the port" do
+ a = Graphite.new
+ lambda {a.push_to_graphite}.should raise_error(RuntimeError, "You need to provide both the hostname and the port")
+ end
+
+ it "returns time" do
+ a = Graphite.new
+ a.time_now.should == Time.now.to_i
+ end
+
+ it "has pushes row messages to graphite without restriction" do
+ ftcp = mock_tcp()
+ a = Graphite.new(:host => 'localhost')
+ a.push_to_graphite do |graphite|
+ graphite.puts "hello world"
+ end
+
+ ftcp.buffer.should == "hello world"
+ end
+
+ it "sends metrics from an hash" do
+ ftcp = mock_tcp()
+ a = Graphite.new(:host => 'localhost')
+
+ time = a.send_metrics({
+ 'foo.bar' => 200,
+ 'foo.test' => 10.2
+ })
+ ftcp.buffer.should == "foo.bar 200 #{time}\nfoo.test 10.2 #{time}\n"
+ end
+
+end
View
@@ -0,0 +1,51 @@
+require 'socket'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+end
+
+TCP_NEW = TCPSocket.method(:new) unless defined? TCP_NEW
+
+class FakeTCPSocket
+ attr :buffer, true
+
+ def initialize
+ @buffer = ""
+ end
+
+ def readline
+ end
+
+ def flush
+ end
+
+ def write(some_text)
+ @buffer += some_text
+ end
+
+ def readchar
+ end
+
+ def read(num)
+ end
+
+ def close
+ end
+
+ def puts(some_text)
+ @buffer += some_text
+ end
+
+end
+
+def mock_tcp()
+ ftcp = FakeTCPSocket.new
+ TCPSocket.stub!(:new).and_return { ftcp }
+ ftcp
+end
+
+def unmock_tcp
+ TCKSocket.stub!(:new).and_return {TCP_NEW.call}
+end
@@ -1,7 +0,0 @@
-require 'test_helper'
-
-class EtsyGraphiteTest < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
- end
-end
View
@@ -1,10 +0,0 @@
-require 'rubygems'
-require 'test/unit'
-require 'shoulda'
-
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'simple-graphite'
-
-class Test::Unit::TestCase
-end

0 comments on commit b0ca6f2

Please sign in to comment.