Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Configuration tweaks. More test coverage. Timeouts on TCP and UDP con…

…nections. Exception handling for any errors.
  • Loading branch information...
commit d867542beecd924ea18755c29112b39d7d32e1ea 1 parent e4b5fde
@marckysharky marckysharky authored
View
1  .gitignore
@@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
+coverage/*
View
2  .rvmrc
@@ -1 +1 @@
-rvm use --create 1.8.7@fozzie
+rvm use --create 1.9.3@fozzie
View
2  Rakefile
@@ -1 +1 @@
-require "bundler/gem_tasks"
+require "bundler/gem_tasks"
View
4 fozzie.gemspec
@@ -17,7 +17,9 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
-
+
+ s.add_development_dependency 'rake'
s.add_development_dependency "rspec"
s.add_development_dependency "mocha"
+ s.add_development_dependency "simplecov"
end
View
16 lib/core_ext/hash.rb
@@ -0,0 +1,16 @@
+class Hash
+
+ # Return self as symbolized keys hash
+ def symbolize_keys
+ self.dup.inject({}) do |hsh, (k,v)|
+ hsh[k.to_sym] = (v.respond_to?(:symbolize_keys) ? v.symbolize_keys : v)
+ hsh
+ end
+ end
+
+ # Replace self with symbolized keys hash
+ def symbolize_keys!
+ self.replace(self.symbolize_keys)
+ end
+
+end
View
13 lib/fozzie.rb
@@ -1,17 +1,8 @@
-require "fozzie/configuration"
+require "fozzie/config"
require "fozzie/connection"
require "fozzie/methods"
require "fozzie/version"
module Fozzie
- extend Fozzie::Methods
-
- def self.c
- config
- end
-
- def self.config
- @config ||= Configuration.new
- end
-
+ extend Fozzie::Methods, Fozzie::Config
end
View
19 lib/fozzie/config.rb
@@ -0,0 +1,19 @@
+require "fozzie/configuration"
+
+module Fozzie
+ module Config
+
+ def c
+ config
+ end
+
+ def config(&block)
+ @config ||= Configuration.new
+ end
+
+ def configure(&block)
+ yield c if block_given?
+ end
+
+ end
+end
View
21 lib/fozzie/configuration.rb
@@ -1,19 +1,34 @@
require 'ostruct'
+require 'core_ext/hash'
module Fozzie
+ # Fozzie configuration allows assignment of global properties
+ # that will be used within the Fozzie codebase.
class Configuration < OpenStruct
def initialize(args = {})
- super self.class.default_configuration.merge(args)
+ super self.class.merge_config(args)
end
private
+ # Handle the merging of the given configuaration, and the default config.
+ # @return [Hash]
+ def self.merge_config(args = {})
+ arg = self.default_configuration.merge(args.symbolize_keys)
+ arg.delete_if {|key, val| !self.default_configuration.keys.include?(key.to_sym) }
+ arg
+ end
+
+ # Default configuration settings
+ # @return [Hash]
def self.default_configuration
{
- :host => '127.0.0.1',
- :port => 8081
+ :host => '127.0.0.1',
+ :port => 999,
+ :via => :tcp,
+ :timeout => 100
}.dup
end
View
24 lib/fozzie/connection.rb
@@ -1,11 +1,35 @@
+require 'socket'
+require 'ipaddr'
+
module Fozzie
+
+ # TODO:: expose the supported connections in a clear way
+ # Connection to the data gathering service
class Connection
+ # send a packet to the data warehouse/service
def self.send_data(data)
+ meth = "via_#{Fozzie.c.via.to_s}"
+ raise RuntimeError, "#{Fozzie.c.via.to_s} currently not supported" unless self.respond_to?(meth)
+ self.send(meth, data)
+ end
+
+ private
+
+ # Send packet via tcp
+ def self.via_tcp(data)
TCPSocket.open(Fozzie.c.host, Fozzie.c.port) do |sock|
+ s.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, Fozzie.c.timeout)
sock.puts(data)
end rescue nil
end
+ # Send packet via udp
+ def self.via_udp(data)
+ s = UDPSocket.new
+ s.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, Fozzie.c.timeout)
+ s.send(data, 0, Fozzie.c.host, Fozzie.c.port) rescue nil
+ end
+
end
end
View
33 spec/lib/core_ext/hash_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+require 'core_ext/hash'
+
+describe Hash do
+
+ it { should respond_to(:symbolize_keys) }
+ it { should respond_to(:symbolize_keys!) }
+
+ it "manipulates keys correctly" do
+ {
+ '1' => 1,
+ '2' => 2,
+ '3' => 3
+ }.symbolize_keys.should == {
+ :"1" => 1,
+ :"2" => 2,
+ :"3" => 3
+ }
+ end
+
+ it "returns copy when bang not provided" do
+ hsh = { '1' => 1, '2' => 2, '3' => 3 }
+ hsh.symbolize_keys
+ hsh.should == { '1' => 1, '2' => 2, '3' => 3 }
+ end
+
+ it "replaces self when bang provided" do
+ hsh = { '1' => 1, '2' => 2, '3' => 3 }
+ hsh.symbolize_keys!
+ hsh.should == { :"1" => 1, :"2" => 2, :"3" => 3 }
+ end
+
+end
View
26 spec/lib/fozzie/config_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe Fozzie::Config do
+
+ subject do
+ F = Class.new { extend Fozzie::Config } unless defined?(F)
+ F
+ end
+
+ it { should respond_to(:configure) }
+ it { should respond_to(:config) }
+ it { should respond_to(:c) }
+
+ it "allows dynamic assignment" do
+ {
+ :host => 'somewhere.local',
+ :port => 99,
+ :via => :udp,
+ :timeout => 1
+ }.each do |field, val|
+ F.configure {|c| c.send("#{field}=", val) }
+ F.c.send(field).should == val
+ end
+ end
+
+end
View
32 spec/lib/fozzie/connection_spec.rb
@@ -1,13 +1,41 @@
require 'spec_helper'
+require 'socket'
describe Fozzie::Connection do
it { Fozzie::Connection.should respond_to(:send_data) }
it "#send_data" do
- proc {
- Fozzie::Connection.send_data('blablabla')
+ proc {
+ Fozzie::Connection.send_data('foo.bar.baz 42 74857843')
}.should_not raise_error
end
+ it "ignores errors on connecting" do
+ Fozzie.config {|c| c.host = 'nowhere' }
+ proc {
+ 10.times { Fozzie::Connection.send_data('%s %s %s' % ['test.bin', rand(80), Time.now.to_i]) }
+ }.should_not raise_error
+ end
+
+ describe "connects via" do
+
+ before do
+ @value = 'foo.bar.baz 42 74857843'
+ end
+
+ it "tcp" do
+ Fozzie.configure {|c| c.via = :tcp }
+ Fozzie::Connection.expects(:via_tcp)
+ Fozzie::Connection.send_data(@value)
+ end
+
+ it "udp" do
+ Fozzie.configure {|c| c.via = :udp }
+ Fozzie::Connection.expects(:via_udp)
+ Fozzie::Connection.send_data(@value)
+ end
+
+ end
+
end
View
4 spec/lib/fozzie_spec.rb
@@ -27,5 +27,9 @@
Fozzie::Connection.expects(:send_data).with('test.bucket:1|c|@0.1')
Fozzie.sample('test.bucket', 1, :count, 0.1)
end
+
+ it "rejects bad types" do
+ proc { Fozzie.sample('test.bucket', 1, :blabla, 0.1) }.should raise_error(NotImplementedError)
+ end
end
View
10 spec/spec_helper.rb
@@ -2,8 +2,16 @@
lib = File.expand_path('lib', root)
$:.unshift(root, lib)
-require 'fozzie'
+require 'simplecov'
RSpec.configure do |config|
config.mock_with :mocha
+end
+
+require 'fozzie'
+Fozzie.configure do |config|
+ config.host = '127.0.0.1'
+ config.port = 8809
+ config.via = :tcp
+ config.timeout = 5
end
Please sign in to comment.
Something went wrong with that request. Please try again.