-
-
Save christopher-b/4635047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
class Graphite | |
def initialize(host) | |
@host = host | |
end | |
def socket | |
return @socket if @socket && [email protected]? | |
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment