Permalink
Browse files

Make the client really work with a real UDP socket

  • Loading branch information...
Rein Henrichs
Rein Henrichs committed Feb 19, 2011
1 parent c191808 commit 6ec767ae4dcf88f9d38d6353bbb7dab8a064010c
Showing with 29 additions and 23 deletions.
  1. +8 −18 lib/statsd.rb
  2. +4 −4 spec/helper.rb
  3. +17 −1 spec/statsd_spec.rb
View
@@ -1,24 +1,16 @@
+require 'socket'
+
class Statsd
attr_reader :host, :port
def initialize(host, port)
@host, @port = host, port
end
- def increment(stat, sample_rate=1)
- count stat, 1, sample_rate
- end
-
- def decrement(stat, sample_rate=1)
- count stat, -1, sample_rate
- end
-
- def count(stat, count, sample_rate=1)
- send stat, count, 'c', sample_rate
- end
+ def increment(stat, sample_rate=1); count stat, 1, sample_rate end
+ def decrement(stat, sample_rate=1); count stat, -1, sample_rate end
- def timing(stat, ms, sample_rate=1)
- send stat, ms, 'ms', sample_rate
- end
+ def count( stat, count, sample_rate=1); send stat, count, 'c', sample_rate end
+ def timing(stat, ms, sample_rate=1); send stat, ms, 'ms', sample_rate end
def sampled(sample_rate)
return if sample_rate < 1 and rand > sample_rate
@@ -28,10 +20,8 @@ def sampled(sample_rate)
private
def send(stat, delta, type, sample_rate)
- sampled(sample_rate) { socket.send("#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}") }
+ sampled(sample_rate) { socket.send("#{stat}:#{delta}|#{type}#{'|@' << sample_rate.to_s if sample_rate < 1}", 0, host, port) }
end
- def socket
- @socket ||= UDPSocket.new(host, post)
- end
+ def socket; @socket ||= UDPSocket.new end
end
View
@@ -19,12 +19,12 @@ def initialize
@buffer = []
end
- def send(*args)
- @buffer.push args
+ def send(message, *rest)
+ @buffer.push [message]
end
def recv
- @buffer.shift
+ res = @buffer.shift
end
def clear
@@ -36,6 +36,6 @@ def to_s
end
def inspect
- @buffer.inspect
+ "<FakeUDPSocket: #{@buffer.inspect}>"
end
end
View
@@ -32,7 +32,7 @@ def socket
@statsd.increment('foobar', 0.5)
@statsd.socket.recv.must_equal ['foobar:1|c|@0.5']
end
- end
+end
end
describe "#decrement" do
@@ -95,3 +95,19 @@ def socket
end
end
+
+describe Statsd do
+ describe "with a real UDP socket" do
+ it "should actually send stuff over the socket" do
+ socket = UDPSocket.new
+ host, port = 'localhost', 12345
+ socket.bind(host, port)
+
+ statsd = Statsd.new(host, port)
+ p statsd.__send__(:socket)
+ statsd.increment('foobar')
+ message = socket.recvfrom(16).first
+ message.must_equal 'foobar:1|c'
+ end
+ end
+end if ENV['LIVE']

0 comments on commit 6ec767a

Please sign in to comment.