Permalink
Browse files

Tests, tests, tests.

  • Loading branch information...
1 parent 1a6bffc commit f3232cfe2933c4062bc909f188f899f8ab0a9966 Hayes Davis committed Jun 2, 2011
View
7 Rakefile
@@ -0,0 +1,7 @@
+task :test do
+ test_root = File.expand_path(File.dirname(__FILE__)+"/test")
+ require File.join(test_root,"test_helper")
+ Dir.glob("#{test_root}/**/*_test.rb") do |file|
+ require File.expand_path(file)
+ end
+end
View
53 test/helpers/filter_test.rb
@@ -0,0 +1,53 @@
+class FilterTest < Test::Unit::TestCase
+
+ def setup
+ @handler = mock("handler")
+ end
+
+ def test_constructor_block_used_as_filter
+ filter = Camayoc::Handlers::Filter.new(@handler) do |type,event|
+ event.stat.length > 5
+ end
+ @handler.expects(:count).once
+ @handler.expects(:timing).once
+ filter.count(Camayoc::StatEvent.new("foo:bar","short",500))
+ filter.count(Camayoc::StatEvent.new("foo:bar","very_long",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","short",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","very_long",500))
+ end
+
+ def test_with_filters_on_namespaced_stat
+ filter = Camayoc::Handlers::Filter.new(@handler,:with=>/a{2}/)
+ @handler.expects(:count).once
+ @handler.expects(:timing).once
+ filter.count(Camayoc::StatEvent.new("foo:bar","aa_blah",500))
+ filter.count(Camayoc::StatEvent.new("foo:bar","bb_blah",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","aa_blah",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","bb_blah",500))
+ end
+
+ def test_constructor_block_used_as_filter
+ filter = Camayoc::Handlers::Filter.new(@handler,
+ :if=>Proc.new{|type,event| type == :timing && event.value > 1000 })
+
+ @handler.expects(:count).never
+ @handler.expects(:timing).once
+ filter.count(Camayoc::StatEvent.new("foo:bar","short",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","short",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","very_long",1500))
+ end
+
+ def test_constructor_block_used_as_filter
+ filter = Camayoc::Handlers::Filter.new(@handler,
+ :unless=>Proc.new{|type,event| type == :count })
+
+ @handler.expects(:count).never
+ @handler.expects(:timing).twice
+ filter.count(Camayoc::StatEvent.new("foo:bar","short",500))
+ filter.count(Camayoc::StatEvent.new("foo:bar","very_long",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","short",500))
+ filter.timing(Camayoc::StatEvent.new("foo:bar","very_long",1500))
+ end
+
+
+end
View
22 test/helpers/statsd_test.rb
@@ -0,0 +1,22 @@
+class StatsdTest < Test::Unit::TestCase
+
+ def setup
+ @statsd = Camayoc::Handlers::Statsd.new(:host=>"localhost",:port=>1234)
+ end
+
+ def test_count_sends_correct_statsd_message
+ expect_message("foo.bar.baz:500|c")
+ @statsd.count(Camayoc::StatEvent.new("foo:bar","baz",500,{}))
+ end
+
+ def test_timing_sends_correct_statsd_message
+ expect_message("foo.bar.time:100|ms")
+ @statsd.timing(Camayoc::StatEvent.new("foo:bar","time",100,{}))
+ end
+
+ private
+ def expect_message(message)
+ @statsd.__send__(:socket).expects(:send).with(message,0,"localhost",1234)
+ end
+
+end
View
57 test/registration_test.rb
@@ -0,0 +1,57 @@
+class RegistrationTest < Test::Unit::TestCase
+
+ def test_accessing_stats_creates_it
+ stats = Camayoc["foo:bar"]
+ assert_equal(Camayoc::Stats,stats.class)
+ end
+
+ def test_accessing_stats_returns_same_instance
+ stats1 = Camayoc["foo:bar"]
+ stats2 = Camayoc["foo:bar"]
+ assert_same(stats1,stats2)
+ end
+
+ def test_accessing_path_with_no_ancestor_leaves_parent_nil
+ stats1 = Camayoc["foo:bar"]
+ assert_nil(stats1.parent)
+ end
+
+ def test_accessing_path_with_already_existing_ancestor_sets_parent
+ parent = Camayoc["foo"]
+ child = Camayoc["foo:bar"]
+ assert_same(parent,child.parent)
+ end
+
+ def test_accessing_path_with_intermediate_steps_gets_closest_existing_ancestor_as_parent
+ parent = Camayoc["foo"]
+ child = Camayoc["foo:a:b:c:baz"]
+ assert_same(parent,child.parent)
+ end
+
+ def test_accessing_path_in_middle_of_existing_hierarchy_reassociates_children
+ root = Camayoc["foo"]
+ child1 = Camayoc["foo:a:b:c:1"]
+ child2 = Camayoc["foo:a:b:c:2"]
+ middle = Camayoc["foo:a:b"]
+
+ assert_same(root,middle.parent)
+ assert_same(middle,child1.parent)
+ assert_same(middle,child2.parent)
+ end
+
+ def test_all_lists_all_existing_instances
+ root = Camayoc["foo"]
+ child1 = Camayoc["foo:a:b:c:1"]
+ child2 = Camayoc["foo:a:b:c:2"]
+ middle = Camayoc["foo:a:b"]
+
+ all = Camayoc.all
+ all = all.sort_by{|stats| stats.name}
+
+ assert_equal([root,middle,child1,child2],all)
+ end
+
+ def teardown
+ Camayoc.instance_variable_get("@registry").clear
+ end
+end
View
8 test/stat_event_test.rb
@@ -0,0 +1,8 @@
+class StatsTest < Test::Unit::TestCase
+
+ def test_ns_stat_uses_source_for_namespace
+ event = Camayoc::StatEvent.new("foo:bar:baz","stat",10000,{})
+ assert_equal("foo:bar:baz:stat",event.ns_stat)
+ end
+
+end
View
94 test/stats_test.rb
@@ -0,0 +1,94 @@
+class StatsTest < Test::Unit::TestCase
+
+ def setup
+ @stats = Camayoc::Stats.new("foo:bar")
+ @handler = mock("handler1")
+ @stats.add(@handler)
+ end
+
+ def test_count_fires_to_all_handlers
+ h2 = mock("handler2")
+ @stats.add(h2)
+
+ @handler.expects(:count).with(kind_of(Camayoc::StatEvent))
+ h2.expects(:count).with(kind_of(Camayoc::StatEvent))
+ @stats.count("count",500)
+ end
+
+ def test_count_generates_correct_stat_event
+ @handler.expects(:count).with(
+ &stat_event_match("foo:bar","count",500,{:pass_through=>true}))
+ @stats.count("count",500,:pass_through=>true)
+ end
+
+ def test_count_propagates_event_to_parent_after_firing_to_handlers
+ @stats.parent = Camayoc::Stats.new("foo")
+
+ seq = sequence("firing")
+ evt = stat_event_match("foo:bar","count",100,{:pass_through=>true})
+ @handler.expects(:count).with(&evt).in_sequence(seq)
+ @stats.parent.expects(:count_event).with(&evt).in_sequence(seq)
+
+ @stats.count("count",100,:pass_through=>true)
+ end
+
+ def test_increment_delegates_to_count
+ @stats.expects(:count).with("count",1,{})
+ @stats.increment("count")
+ end
+
+ def test_decrement_delegates_to_count
+ @stats.expects(:count).with("count",-1,{})
+ @stats.decrement("count")
+ end
+
+ def test_timing_fires_to_all_handlers
+ h2 = mock("handler2")
+ @stats.add(h2)
+
+ @handler.expects(:timing).with(kind_of(Camayoc::StatEvent))
+ h2.expects(:timing).with(kind_of(Camayoc::StatEvent))
+ @stats.timing("time",500)
+ end
+
+ def test_timing_generates_correct_stat_event
+ @handler.expects(:timing).with(
+ &stat_event_match("foo:bar","time",1,{:pass_through=>true}))
+ @stats.timing("time",1,:pass_through=>true)
+ end
+
+ def test_timing_propagates_event_to_parent_after_firing_to_handlers
+ @stats.parent = Camayoc::Stats.new("foo")
+
+ seq = sequence("firing")
+ evt = stat_event_match("foo:bar","time",100,{:pass_through=>true})
+ @handler.expects(:timing).with(&evt).in_sequence(seq)
+ @stats.parent.expects(:timing_event).with(&evt).in_sequence(seq)
+
+ @stats.timing("time",100,:pass_through=>true)
+ end
+
+ def test_handler_errors_are_swallowed_and_firing_continues
+ h2 = mock("handler2")
+ @stats.add(h2)
+
+ seq = sequence("firing")
+ @handler.expects(:count).raises("FAIL").in_sequence(seq)
+ h2.expects(:count).in_sequence(seq)
+
+ assert_nothing_raised do
+ @stats.count("baz",100)
+ end
+ end
+
+ private
+ def stat_event_match(*args)
+ template = Camayoc::StatEvent.new(*args)
+ Proc.new do |event|
+ event.source == template.source &&
+ event.stat == template.stat &&
+ event.value == template.value &&
+ event.options == template.options
+ end
+ end
+end
View
6 test/test_helper.rb
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'test/unit'
+require 'mocha'
+
+$: << File.join(File.dirname(__FILE__),"..","lib")
+require 'camayoc'
View
33 test/thread_safety_test.rb
@@ -0,0 +1,33 @@
+class ThreadSafetyTest < Test::Unit::TestCase
+
+ class SafeThing
+ include Camayoc::ThreadSafety
+ end
+
+ def setup
+ @thing = SafeThing.new
+ end
+
+ def test_thread_safe_turns_on_lock
+ @thing.thread_safe = true
+ assert_equal(Mutex,@thing.__send__(:lock).class)
+ end
+
+ def test_lock_is_placebo_by_default
+ assert_equal(Camayoc::ThreadSafety::PlaceboLock,
+ @thing.__send__(:lock).class)
+ end
+
+ def test_synchronize_yields_whether_safe_or_not
+ yielded = false
+ @thing.thread_safe = true
+ @thing.synchronize { yielded = true }
+ assert(yielded)
+
+ yielded = false
+ @thing.thread_safe = false
+ @thing.synchronize {yielded = true}
+ assert(yielded)
+ end
+
+end

0 comments on commit f3232cf

Please sign in to comment.