Browse files

Implement Statsd#increment

  • Loading branch information...
1 parent 4cbb9de commit b7c7b0cdd792093292e0facf06d98f23f8a9ac04 Rein Henrichs committed Feb 18, 2011
Showing with 66 additions and 4 deletions.
  1. +16 −0 lib/statsd.rb
  2. +26 −0 spec/helper.rb
  3. +24 −4 spec/statsd_spec.rb
View
16 lib/statsd.rb
@@ -0,0 +1,16 @@
+class Statsd
+ attr_reader :host, :port
+ def initialize(host, port)
+ @host, @port = host, port
+ end
+
+ def increment(stat)
+ socket.send("#{stat}:1|c")
+ end
+
+ private
+
+ def socket
+ @socket ||= UDPSocket.new(host, post)
+ end
+end
View
26 spec/helper.rb
@@ -12,3 +12,29 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'statsd'
+
+class FakeUDPSocket
+ def initialize
+ @buffer = []
+ end
+
+ def send(*args)
+ @buffer.push args
+ end
+
+ def recv
+ @buffer.shift
+ end
+
+ def clear
+ @buffer = []
+ end
+
+ def to_s
+ inspect
+ end
+
+ def inspect
+ @buffer.inspect
+ end
+end
View
28 spec/statsd_spec.rb
@@ -1,7 +1,27 @@
-require 'helper'
+require 'spec/helper'
-describe "foo" do
- it "should fail" do
- flunk
+describe Statsd do
+ class Statsd
+ def socket
+ @socket ||= FakeUDPSocket.new
+ end
end
+
+ before { @statsd = Statsd.new('localhost', 1234) }
+ after { @statsd.socket.clear }
+
+ describe "#initialize" do
+ it "should set the host and port" do
+ @statsd.host.must_equal 'localhost'
+ @statsd.port.must_equal 1234
+ end
+ end
+
+ describe "#increment" do
+ it "should format the message according to the statsd spec" do
+ @statsd.increment('foobar')
+ @statsd.socket.recv.must_equal ['foobar:1|c']
+ end
+ end
+
end

0 comments on commit b7c7b0c

Please sign in to comment.