Skip to content

Instantly share code, notes, and snippets.

@joakimk
Created January 25, 2012 20:14
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joakimk/1678399 to your computer and use it in GitHub Desktop.
Save joakimk/1678399 to your computer and use it in GitHub Desktop.
Graphite client for ruby with specs
require 'socket'
class Graphite
def initialize(host)
@host = host
end
def socket
return @socket if @socket && !@socket.closed?
@socket = TCPSocket.new(@host, 2003)
end
def report(key, value, time = Time.now)
begin
socket.write("#{key} #{value.to_f} #{time.to_i}\n")
rescue Errno::EPIPE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED
@socket = nil
nil
end
end
def close_socket
@socket.close if @socket
@socket = nil
end
end
require 'graphite'
describe Graphite do
it "should create a socket and report data to it" do
time = Time.now
TCPSocket.should_receive(:new).with("host", 2003).and_return(socket = mock(:closed? => false))
socket.should_receive(:write).with("hello.world 10.5 #{time.to_i}\n")
graphite = Graphite.new("host")
graphite.report("hello.world", 10.5, time)
end
it "should reuse the same socket" do
time = Time.now
TCPSocket.should_receive(:new).once.with("host", 2003).and_return(socket = mock(:closed? => false))
socket.should_receive(:write).twice
graphite = Graphite.new("host")
graphite.report("hello.world", 10.5, time)
graphite.report("hello.world", 10, time)
end
it "should not reuse a socket that is closed" do
time = Time.now
TCPSocket.should_receive(:new).twice.with("host", 2003).and_return(socket = mock(:closed? => false))
socket.should_receive(:write)
socket.should_receive(:write)
graphite = Graphite.new("host")
graphite.report("hello.world", 10.5, time)
socket.stub!(:closed? => true)
graphite.report("hello.world", 10, time)
end
context "error handling" do
it "should reset the socket on Errno::EPIPE" do
time = Time.now
TCPSocket.should_receive(:new).twice.with("host", 2003).and_return(socket = mock(:closed? => false))
socket.should_receive(:write).and_raise(Errno::EPIPE)
socket.should_receive(:write)
graphite = Graphite.new("host")
graphite.report("hello.world", 10.5, time)
graphite.report("hello.world", 10, time)
end
it "should fail silently on Errno::EHOSTUNREACH" do
TCPSocket.should_receive(:new).and_raise(Errno::EHOSTUNREACH)
graphite = Graphite.new("host")
-> {
graphite.report("hello.world", 10.5, Time.now)
}.should_not raise_error
end
it "should fail silently on Errno::ECONNREFUSED" do
TCPSocket.should_receive(:new).and_raise(Errno::ECONNREFUSED)
graphite = Graphite.new("host")
-> {
graphite.report("hello.world", 10.5, Time.now)
}.should_not raise_error
end
end
it "should be possible to force the socket close" do
time = Time.now
TCPSocket.should_receive(:new).twice.with("host", 2003).and_return(socket = mock(:closed? => false))
socket.should_receive(:write)
socket.should_receive(:write)
graphite = Graphite.new("host")
graphite.report("hello.world", 10.5, time)
socket.should_receive(:close)
graphite.close_socket
graphite.report("hello.world", 10, time)
end
end
@luxflux
Copy link

luxflux commented Jan 11, 2013

Why not creating a gem with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment