From a60bdd7d2921ca10b0a5ae3f750b402e12981004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Sep 2009 08:59:15 -0300 Subject: Added queue abstraction to Orchestra. --- activesupport/test/orchestra_test.rb | 111 +++++++++++++++-------------------- 1 file changed, 47 insertions(+), 64 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 683cc36f6a..e343d6322b 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -1,5 +1,12 @@ require 'abstract_unit' +# Allow LittleFanout to be cleaned. +class ActiveSupport::Orchestra::LittleFanout + def clear + @listeners.clear + end +end + class OrchestraEventTest < Test::Unit::TestCase def setup @parent = ActiveSupport::Orchestra::Event.new(:parent) @@ -34,12 +41,12 @@ end class OrchestraMainTest < Test::Unit::TestCase def setup - @listener = [] - ActiveSupport::Orchestra.register @listener + @events = [] + ActiveSupport::Orchestra.subscribe { |event| @events << event } end def teardown - ActiveSupport::Orchestra.unregister @listener + ActiveSupport::Orchestra.queue.clear end def test_orchestra_allows_any_action_to_be_instrumented @@ -65,9 +72,9 @@ class OrchestraMainTest < Test::Unit::TestCase 1 + 1 end - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal "orchestra", @events.last.payload end def test_nested_events_can_be_instrumented @@ -76,18 +83,18 @@ class OrchestraMainTest < Test::Unit::TestCase sleep(0.1) end - assert_equal 1, @listener.size - assert_equal :wot, @listener.first.name - assert_equal "child", @listener.first.payload + assert_equal 1, @events.size + assert_equal :wot, @events.first.name + assert_equal "child", @events.first.payload - assert_nil @listener.first.parent.duration - assert_in_delta 100, @listener.first.duration, 30 + assert_nil @events.first.parent.duration + assert_in_delta 100, @events.first.duration, 30 end - assert_equal 2, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload - assert_in_delta 100, @listener.first.parent.duration, 30 + assert_equal 2, @events.size + assert_equal :awesome, @events.last.name + assert_equal "orchestra", @events.last.payload + assert_in_delta 100, @events.first.parent.duration, 30 end def test_event_is_pushed_even_if_block_fails @@ -95,67 +102,43 @@ class OrchestraMainTest < Test::Unit::TestCase raise "OMG" end rescue RuntimeError - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal "orchestra", @events.last.payload end -end - -class OrchestraListenerTest < Test::Unit::TestCase - class MyListener < ActiveSupport::Orchestra::Listener - attr_reader :consumed - def consume(event) - @consumed ||= [] - @consumed << event - end - end + def test_subscriber_with_pattern + @another = [] + ActiveSupport::Orchestra.subscribe(/cache/) { |event| @another << event } - def setup - @listener = MyListener.new - ActiveSupport::Orchestra.register @listener - end + ActiveSupport::Orchestra.instrument(:something){ 0 } + ActiveSupport::Orchestra.instrument(:cache){ 10 } - def teardown - ActiveSupport::Orchestra.unregister @listener - end - - def test_thread_is_exposed_by_listener - assert_kind_of Thread, @listener.thread - end - - def test_event_is_consumed_when_an_action_is_instrumented - ActiveSupport::Orchestra.instrument(:sum) do - 1 + 1 - end sleep 0.1 - assert_equal 1, @listener.consumed.size - assert_equal :sum, @listener.consumed.first.name - assert_equal 2, @listener.consumed.first.result + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 10, @another.first.result end - def test_with_sevaral_consumers_and_several_events - @another = MyListener.new - ActiveSupport::Orchestra.register @another + def test_with_several_consumers_and_several_events + @another = [] + ActiveSupport::Orchestra.subscribe { |event| @another << event } 1.upto(100) do |i| - ActiveSupport::Orchestra.instrument(:value) do - i - end + ActiveSupport::Orchestra.instrument(:value){ i } end sleep 0.1 - assert_equal 100, @listener.consumed.size - assert_equal :value, @listener.consumed.first.name - assert_equal 1, @listener.consumed.first.result - assert_equal 100, @listener.consumed.last.result - - assert_equal 100, @another.consumed.size - assert_equal :value, @another.consumed.first.name - assert_equal 1, @another.consumed.first.result - assert_equal 100, @another.consumed.last.result - ensure - ActiveSupport::Orchestra.unregister @another + assert_equal 100, @events.size + assert_equal :value, @events.first.name + assert_equal 1, @events.first.result + assert_equal 100, @events.last.result + + assert_equal 100, @another.size + assert_equal :value, @another.first.name + assert_equal 1, @another.first.result + assert_equal 100, @another.last.result end end -- cgit v1.2.3 From 7b7796e23d12b526fa35976c514da91169dd2566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 1 Oct 2009 21:14:38 -0300 Subject: Events are created inside threads. --- activesupport/test/orchestra_test.rb | 73 ++++++++++++------------------------ 1 file changed, 24 insertions(+), 49 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index e343d6322b..608531416c 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -8,103 +8,78 @@ class ActiveSupport::Orchestra::LittleFanout end class OrchestraEventTest < Test::Unit::TestCase - def setup - @parent = ActiveSupport::Orchestra::Event.new(:parent) + def test_events_are_initialized_with_name_and_payload + event = ActiveSupport::Orchestra::Event.new(:foo, :payload => :bar) + assert_equal :foo, event.name + assert_equal Hash[:payload => :bar], event.payload end - def test_initialization_with_name_and_parent_and_payload - event = ActiveSupport::Orchestra::Event.new(:awesome, @parent, :payload => "orchestra") - assert_equal(:awesome, event.name) - assert_equal(@parent, event.parent) - assert_equal({ :payload => "orchestra" }, event.payload) - end + def test_events_consumes_information_given_as_payload + event = ActiveSupport::Orchestra::Event.new(:foo, + :time => (time = Time.now), :result => 1, :duration => 10) - def test_thread_id_is_set_on_initialization - event = ActiveSupport::Orchestra::Event.new(:awesome) - assert_equal Thread.current.object_id, event.thread_id - end - - def test_current_time_is_set_on_initialization - previous_time = Time.now.utc - event = ActiveSupport::Orchestra::Event.new(:awesome) - assert_kind_of Time, event.time - assert event.time.to_f >= previous_time.to_f - end - - def test_duration_is_set_when_event_finishes - event = ActiveSupport::Orchestra::Event.new(:awesome) - sleep(0.1) - event.finish! - assert_in_delta 100, event.duration, 30 + assert_equal Hash.new, event.payload + assert_equal time, event.time + assert_equal 1, event.result + assert_equal 10, event.duration end end class OrchestraMainTest < Test::Unit::TestCase def setup @events = [] + Thread.abort_on_exception = true ActiveSupport::Orchestra.subscribe { |event| @events << event } end def teardown + Thread.abort_on_exception = false ActiveSupport::Orchestra.queue.clear end - def test_orchestra_allows_any_action_to_be_instrumented - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - sleep(0.1) - end - - assert_equal :awesome, event.name - assert_equal "orchestra", event.payload - assert_in_delta 100, event.duration, 30 - end - - def test_block_result_is_stored - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + def test_orchestra_returns_action_result + result = ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do 1 + 1 end - assert_equal 2, event.result + assert_equal 2, result end def test_events_are_published_to_a_listener - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do 1 + 1 end assert_equal 1, @events.size assert_equal :awesome, @events.last.name - assert_equal "orchestra", @events.last.payload + assert_equal Hash[:payload => "orchestra"], @events.last.payload end def test_nested_events_can_be_instrumented - ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - ActiveSupport::Orchestra.instrument(:wot, "child") do + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do + ActiveSupport::Orchestra.instrument(:wot, :payload => "child") do sleep(0.1) end assert_equal 1, @events.size assert_equal :wot, @events.first.name - assert_equal "child", @events.first.payload - - assert_nil @events.first.parent.duration + assert_equal Hash[:payload => "child"], @events.first.payload assert_in_delta 100, @events.first.duration, 30 end assert_equal 2, @events.size assert_equal :awesome, @events.last.name - assert_equal "orchestra", @events.last.payload - assert_in_delta 100, @events.first.parent.duration, 30 + assert_equal Hash[:payload => "orchestra"], @events.last.payload end def test_event_is_pushed_even_if_block_fails - ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do raise "OMG" end rescue RuntimeError assert_equal 1, @events.size assert_equal :awesome, @events.last.name - assert_equal "orchestra", @events.last.payload + assert_equal Hash[:payload => "orchestra"], @events.last.payload end def test_subscriber_with_pattern -- cgit v1.2.3 From 5d0f8abc003cc6edfdb471ada05754580725b353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Oct 2009 09:42:42 -0300 Subject: Orchestra listeners have their own queue. --- activesupport/test/orchestra_test.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 608531416c..810d99ebeb 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -50,6 +50,8 @@ class OrchestraMainTest < Test::Unit::TestCase 1 + 1 end + sleep(0.1) + assert_equal 1, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "orchestra"], @events.last.payload @@ -58,18 +60,22 @@ class OrchestraMainTest < Test::Unit::TestCase def test_nested_events_can_be_instrumented ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do ActiveSupport::Orchestra.instrument(:wot, :payload => "child") do - sleep(0.1) + 1 + 1 end + sleep(0.1) + assert_equal 1, @events.size assert_equal :wot, @events.first.name assert_equal Hash[:payload => "child"], @events.first.payload - assert_in_delta 100, @events.first.duration, 30 end + sleep(0.1) + assert_equal 2, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "orchestra"], @events.last.payload + assert_in_delta 100, @events.last.duration, 70 end def test_event_is_pushed_even_if_block_fails @@ -77,6 +83,8 @@ class OrchestraMainTest < Test::Unit::TestCase raise "OMG" end rescue RuntimeError + sleep(0.1) + assert_equal 1, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "orchestra"], @events.last.payload @@ -89,7 +97,7 @@ class OrchestraMainTest < Test::Unit::TestCase ActiveSupport::Orchestra.instrument(:something){ 0 } ActiveSupport::Orchestra.instrument(:cache){ 10 } - sleep 0.1 + sleep(0.1) assert_equal 1, @another.size assert_equal :cache, @another.first.name -- cgit v1.2.3 From af0d1fa8920793a95fae456d1f5debdc50287eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 7 Oct 2009 11:17:50 -0300 Subject: Update Orchestra instrumentations and move part of logging to Orchestra. --- activesupport/test/orchestra_test.rb | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 810d99ebeb..7a6e9208b4 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -90,18 +90,39 @@ class OrchestraMainTest < Test::Unit::TestCase assert_equal Hash[:payload => "orchestra"], @events.last.payload end + def test_event_is_pushed_even_without_block + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "orchestra"], @events.last.payload + end + def test_subscriber_with_pattern @another = [] - ActiveSupport::Orchestra.subscribe(/cache/) { |event| @another << event } + ActiveSupport::Orchestra.subscribe("cache"){ |event| @another << event } + ActiveSupport::Orchestra.instrument(:cache){ 1 } + + sleep(0.1) + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 1, @another.first.result + end + + def test_subscriber_with_pattern_as_regexp + @another = [] + ActiveSupport::Orchestra.subscribe(/cache/){ |event| @another << event } ActiveSupport::Orchestra.instrument(:something){ 0 } - ActiveSupport::Orchestra.instrument(:cache){ 10 } + ActiveSupport::Orchestra.instrument(:cache){ 1 } sleep(0.1) assert_equal 1, @another.size assert_equal :cache, @another.first.name - assert_equal 10, @another.first.result + assert_equal 1, @another.first.result end def test_with_several_consumers_and_several_events -- cgit v1.2.3 From a15e02d44ac2afb27a7e8e652c98a796d271b645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 9 Oct 2009 09:52:25 -0300 Subject: Unify benchmark APIs. --- activesupport/test/benchmarkable_test.rb | 87 ++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 activesupport/test/benchmarkable_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/benchmarkable_test.rb b/activesupport/test/benchmarkable_test.rb new file mode 100644 index 0000000000..e807bcb732 --- /dev/null +++ b/activesupport/test/benchmarkable_test.rb @@ -0,0 +1,87 @@ +require 'abstract_unit' +require 'action_view/helpers/benchmark_helper' + +class BenchmarkableTest < ActiveSupport::TestCase + include ActiveSupport::Benchmarkable + + def teardown + logger.send(:clear_buffer) + end + + def test_without_block + assert_raise(LocalJumpError) { benchmark } + assert buffer.empty? + end + + def test_defaults + i_was_run = false + benchmark { i_was_run = true } + assert i_was_run + assert_last_logged + end + + def test_with_message + i_was_run = false + benchmark('test_run') { i_was_run = true } + assert i_was_run + assert_last_logged 'test_run' + end + + def test_with_message_and_deprecated_level + i_was_run = false + + assert_deprecated do + benchmark('debug_run', :debug) { i_was_run = true } + end + + assert i_was_run + assert_last_logged 'debug_run' + end + + def test_within_level + logger.level = ActiveSupport::BufferedLogger::DEBUG + benchmark('included_debug_run', :level => :debug) { } + assert_last_logged 'included_debug_run' + end + + def test_outside_level + logger.level = ActiveSupport::BufferedLogger::ERROR + benchmark('skipped_debug_run', :level => :debug) { } + assert_no_match(/skipped_debug_run/, buffer.last) + ensure + logger.level = ActiveSupport::BufferedLogger::DEBUG + end + + def test_without_silencing + benchmark('debug_run', :silence => false) do + logger.info "not silenced!" + end + + assert_equal 2, buffer.size + end + + def test_with_silencing + benchmark('debug_run', :silence => true) do + logger.info "silenced!" + end + + assert_equal 1, buffer.size + end + + private + def logger + @logger ||= begin + logger = ActiveSupport::BufferedLogger.new(StringIO.new) + logger.auto_flushing = false + logger + end + end + + def buffer + logger.send(:buffer) + end + + def assert_last_logged(message = 'Benchmarking') + assert_match(/^#{message} \(.*\)$/, buffer.last) + end +end -- cgit v1.2.3 From 5988b87c30eb0ce50c235187f5dfcfcfb98da01b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 14:49:29 -0300 Subject: Added parent_of? method to help tracing events. --- activesupport/test/orchestra_test.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 7a6e9208b4..1b2f98c7dd 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -9,20 +9,36 @@ end class OrchestraEventTest < Test::Unit::TestCase def test_events_are_initialized_with_name_and_payload - event = ActiveSupport::Orchestra::Event.new(:foo, :payload => :bar) + event = event(:foo, :payload => :bar) assert_equal :foo, event.name assert_equal Hash[:payload => :bar], event.payload end def test_events_consumes_information_given_as_payload - event = ActiveSupport::Orchestra::Event.new(:foo, - :time => (time = Time.now), :result => 1, :duration => 10) + event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) assert_equal Hash.new, event.payload assert_equal time, event.time assert_equal 1, event.result assert_equal 10, event.duration end + + def test_event_is_parent_based_on_time_frame + parent = event(:foo, :time => Time.utc(2009), :duration => 10000) + child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) + not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) + + assert parent.parent_of?(child) + assert !child.parent_of?(parent) + assert !parent.parent_of?(not_child) + assert !not_child.parent_of?(parent) + end + + protected + + def event(*args) + ActiveSupport::Orchestra::Event.new(*args) + end end class OrchestraMainTest < Test::Unit::TestCase -- cgit v1.2.3 From 2d7abe245e7a2b1717e48ef550e4083318fd7ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 18:51:51 -0300 Subject: Renamed Orchestra to Notifications once again [#3321 state:resolved] --- activesupport/test/notifications_test.rb | 164 +++++++++++++++++++++++++++++++ activesupport/test/orchestra_test.rb | 164 ------------------------------- 2 files changed, 164 insertions(+), 164 deletions(-) create mode 100644 activesupport/test/notifications_test.rb delete mode 100644 activesupport/test/orchestra_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb new file mode 100644 index 0000000000..561ee2b0ba --- /dev/null +++ b/activesupport/test/notifications_test.rb @@ -0,0 +1,164 @@ +require 'abstract_unit' + +# Allow LittleFanout to be cleaned. +class ActiveSupport::Notifications::LittleFanout + def clear + @listeners.clear + end +end + +class NotificationsEventTest < Test::Unit::TestCase + def test_events_are_initialized_with_name_and_payload + event = event(:foo, :payload => :bar) + assert_equal :foo, event.name + assert_equal Hash[:payload => :bar], event.payload + end + + def test_events_consumes_information_given_as_payload + event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) + + assert_equal Hash.new, event.payload + assert_equal time, event.time + assert_equal 1, event.result + assert_equal 10, event.duration + end + + def test_event_is_parent_based_on_time_frame + parent = event(:foo, :time => Time.utc(2009), :duration => 10000) + child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) + not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) + + assert parent.parent_of?(child) + assert !child.parent_of?(parent) + assert !parent.parent_of?(not_child) + assert !not_child.parent_of?(parent) + end + + protected + + def event(*args) + ActiveSupport::Notifications::Event.new(*args) + end +end + +class NotificationsMainTest < Test::Unit::TestCase + def setup + @events = [] + Thread.abort_on_exception = true + ActiveSupport::Notifications.subscribe { |event| @events << event } + end + + def teardown + Thread.abort_on_exception = false + ActiveSupport::Notifications.queue.clear + end + + def test_notifications_returns_action_result + result = ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + 1 + 1 + end + + assert_equal 2, result + end + + def test_events_are_published_to_a_listener + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + 1 + 1 + end + + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + + def test_nested_events_can_be_instrumented + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + ActiveSupport::Notifications.instrument(:wot, :payload => "child") do + 1 + 1 + end + + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :wot, @events.first.name + assert_equal Hash[:payload => "child"], @events.first.payload + end + + sleep(0.1) + + assert_equal 2, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + assert_in_delta 100, @events.last.duration, 70 + end + + def test_event_is_pushed_even_if_block_fails + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + raise "OMG" + end rescue RuntimeError + + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + + def test_event_is_pushed_even_without_block + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + + def test_subscriber_with_pattern + @another = [] + ActiveSupport::Notifications.subscribe("cache"){ |event| @another << event } + ActiveSupport::Notifications.instrument(:cache){ 1 } + + sleep(0.1) + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 1, @another.first.result + end + + def test_subscriber_with_pattern_as_regexp + @another = [] + ActiveSupport::Notifications.subscribe(/cache/){ |event| @another << event } + + ActiveSupport::Notifications.instrument(:something){ 0 } + ActiveSupport::Notifications.instrument(:cache){ 1 } + + sleep(0.1) + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 1, @another.first.result + end + + def test_with_several_consumers_and_several_events + @another = [] + ActiveSupport::Notifications.subscribe { |event| @another << event } + + 1.upto(100) do |i| + ActiveSupport::Notifications.instrument(:value){ i } + end + + sleep 0.1 + + assert_equal 100, @events.size + assert_equal :value, @events.first.name + assert_equal 1, @events.first.result + assert_equal 100, @events.last.result + + assert_equal 100, @another.size + assert_equal :value, @another.first.name + assert_equal 1, @another.first.result + assert_equal 100, @another.last.result + end +end diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb deleted file mode 100644 index 1b2f98c7dd..0000000000 --- a/activesupport/test/orchestra_test.rb +++ /dev/null @@ -1,164 +0,0 @@ -require 'abstract_unit' - -# Allow LittleFanout to be cleaned. -class ActiveSupport::Orchestra::LittleFanout - def clear - @listeners.clear - end -end - -class OrchestraEventTest < Test::Unit::TestCase - def test_events_are_initialized_with_name_and_payload - event = event(:foo, :payload => :bar) - assert_equal :foo, event.name - assert_equal Hash[:payload => :bar], event.payload - end - - def test_events_consumes_information_given_as_payload - event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) - - assert_equal Hash.new, event.payload - assert_equal time, event.time - assert_equal 1, event.result - assert_equal 10, event.duration - end - - def test_event_is_parent_based_on_time_frame - parent = event(:foo, :time => Time.utc(2009), :duration => 10000) - child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) - not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) - - assert parent.parent_of?(child) - assert !child.parent_of?(parent) - assert !parent.parent_of?(not_child) - assert !not_child.parent_of?(parent) - end - - protected - - def event(*args) - ActiveSupport::Orchestra::Event.new(*args) - end -end - -class OrchestraMainTest < Test::Unit::TestCase - def setup - @events = [] - Thread.abort_on_exception = true - ActiveSupport::Orchestra.subscribe { |event| @events << event } - end - - def teardown - Thread.abort_on_exception = false - ActiveSupport::Orchestra.queue.clear - end - - def test_orchestra_returns_action_result - result = ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - 1 + 1 - end - - assert_equal 2, result - end - - def test_events_are_published_to_a_listener - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - 1 + 1 - end - - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - end - - def test_nested_events_can_be_instrumented - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - ActiveSupport::Orchestra.instrument(:wot, :payload => "child") do - 1 + 1 - end - - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :wot, @events.first.name - assert_equal Hash[:payload => "child"], @events.first.payload - end - - sleep(0.1) - - assert_equal 2, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - assert_in_delta 100, @events.last.duration, 70 - end - - def test_event_is_pushed_even_if_block_fails - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - raise "OMG" - end rescue RuntimeError - - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - end - - def test_event_is_pushed_even_without_block - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - end - - def test_subscriber_with_pattern - @another = [] - ActiveSupport::Orchestra.subscribe("cache"){ |event| @another << event } - ActiveSupport::Orchestra.instrument(:cache){ 1 } - - sleep(0.1) - - assert_equal 1, @another.size - assert_equal :cache, @another.first.name - assert_equal 1, @another.first.result - end - - def test_subscriber_with_pattern_as_regexp - @another = [] - ActiveSupport::Orchestra.subscribe(/cache/){ |event| @another << event } - - ActiveSupport::Orchestra.instrument(:something){ 0 } - ActiveSupport::Orchestra.instrument(:cache){ 1 } - - sleep(0.1) - - assert_equal 1, @another.size - assert_equal :cache, @another.first.name - assert_equal 1, @another.first.result - end - - def test_with_several_consumers_and_several_events - @another = [] - ActiveSupport::Orchestra.subscribe { |event| @another << event } - - 1.upto(100) do |i| - ActiveSupport::Orchestra.instrument(:value){ i } - end - - sleep 0.1 - - assert_equal 100, @events.size - assert_equal :value, @events.first.name - assert_equal 1, @events.first.result - assert_equal 100, @events.last.result - - assert_equal 100, @another.size - assert_equal :value, @another.first.name - assert_equal 1, @another.first.result - assert_equal 100, @another.last.result - end -end -- cgit v1.2.3 From ef75d05829a4bbaeab4edb157194c2bd7f0ef60a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 15 Oct 2009 17:39:19 -0700 Subject: Get rid of stray require again --- activesupport/test/benchmarkable_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/benchmarkable_test.rb b/activesupport/test/benchmarkable_test.rb index e807bcb732..766956f50f 100644 --- a/activesupport/test/benchmarkable_test.rb +++ b/activesupport/test/benchmarkable_test.rb @@ -1,5 +1,4 @@ require 'abstract_unit' -require 'action_view/helpers/benchmark_helper' class BenchmarkableTest < ActiveSupport::TestCase include ActiveSupport::Benchmarkable -- cgit v1.2.3 From a1df2590744ed126981dfd5b5709ff6fd5dc6476 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 19 Oct 2009 23:32:06 -0500 Subject: Replace decaying routing internals w/ rack-mount --- activesupport/test/core_ext/regexp_ext_test.rb | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/regexp_ext_test.rb b/activesupport/test/core_ext/regexp_ext_test.rb index cc3f07d5c5..68b089d5b4 100644 --- a/activesupport/test/core_ext/regexp_ext_test.rb +++ b/activesupport/test/core_ext/regexp_ext_test.rb @@ -2,28 +2,9 @@ require 'abstract_unit' require 'active_support/core_ext/regexp' class RegexpExtAccessTests < Test::Unit::TestCase - def test_number_of_captures - assert_equal 0, //.number_of_captures - assert_equal 1, /.(.)./.number_of_captures - assert_equal 2, /.(.).(?:.).(.)/.number_of_captures - assert_equal 3, /.((.).(?:.).(.))/.number_of_captures - end - def test_multiline assert_equal true, //m.multiline? assert_equal false, //.multiline? assert_equal false, /(?m:)/.multiline? end - - def test_optionalize - assert_equal "a?", Regexp.optionalize("a") - assert_equal "(?:foo)?", Regexp.optionalize("foo") - assert_equal "", Regexp.optionalize("") - end - - def test_unoptionalize - assert_equal "a", Regexp.unoptionalize("a?") - assert_equal "foo", Regexp.unoptionalize("(?:foo)?") - assert_equal "", Regexp.unoptionalize("") - end end -- cgit v1.2.3 From 4f6d6f7031a88b647814fc0154e6b69b636dc912 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Tue, 20 Oct 2009 16:33:54 -0700 Subject: Have all the tests running off a single Gemfile --- activesupport/test/abstract_unit.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index af9656615c..f390c66838 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -1,4 +1,12 @@ ORIG_ARGV = ARGV.dup +root = File.expand_path('../../..', __FILE__) +begin + require "#{root}/vendor/gems/environment" +rescue LoadError + $:.unshift("#{root}/activesupport/lib") + $:.unshift("#{root}/activerecord/lib") +end + require 'test/unit' @@ -11,7 +19,6 @@ rescue LoadError end ENV['NO_RELOAD'] = '1' -$:.unshift "#{File.dirname(__FILE__)}/../lib" require 'active_support' require 'active_support/test_case' -- cgit v1.2.3 From b3a198041befa933a26a597451f84482df268d0f Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 27 Oct 2009 00:07:21 -0700 Subject: Some optimizations on AS::Notifications. This does not change the public-facing API. --- activesupport/test/notifications_test.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 561ee2b0ba..7d2bdf5ccf 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -8,25 +8,28 @@ class ActiveSupport::Notifications::LittleFanout end class NotificationsEventTest < Test::Unit::TestCase - def test_events_are_initialized_with_name_and_payload - event = event(:foo, :payload => :bar) + def test_events_are_initialized_with_details + event = event(:foo, Time.now, Time.now + 1, 1, random_id, :payload => :bar) assert_equal :foo, event.name assert_equal Hash[:payload => :bar], event.payload end def test_events_consumes_information_given_as_payload - event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) + time = Time.now + event = event(:foo, time, time + 0.01, 1, random_id, {}) assert_equal Hash.new, event.payload assert_equal time, event.time assert_equal 1, event.result - assert_equal 10, event.duration + assert_equal 10.0, event.duration end def test_event_is_parent_based_on_time_frame - parent = event(:foo, :time => Time.utc(2009), :duration => 10000) - child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) - not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) + time = Time.utc(2009, 01, 01, 0, 0, 1) + + parent = event(:foo, Time.utc(2009), Time.utc(2009) + 100, nil, random_id, {}) + child = event(:foo, time, time + 10, nil, random_id, {}) + not_child = event(:foo, time, time + 100, nil, random_id, {}) assert parent.parent_of?(child) assert !child.parent_of?(parent) @@ -34,11 +37,15 @@ class NotificationsEventTest < Test::Unit::TestCase assert !not_child.parent_of?(parent) end - protected +protected - def event(*args) - ActiveSupport::Notifications::Event.new(*args) - end + def random_id + @random_id ||= ActiveSupport::SecureRandom.hex(10) + end + + def event(*args) + ActiveSupport::Notifications::Event.new(*args) + end end class NotificationsMainTest < Test::Unit::TestCase -- cgit v1.2.3 From cbcb947b00a7c6992cfe42c6b369e87b4fa4ee23 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 27 Oct 2009 21:01:31 -0700 Subject: AS::Notifications.subscribe blocks are now yielded the arguments to pass to AS::Notifications::Event.new --- activesupport/test/notifications_test.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 7d2bdf5ccf..b763b740af 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -52,7 +52,9 @@ class NotificationsMainTest < Test::Unit::TestCase def setup @events = [] Thread.abort_on_exception = true - ActiveSupport::Notifications.subscribe { |event| @events << event } + ActiveSupport::Notifications.subscribe do |*args| + @events << ActiveSupport::Notifications::Event.new(*args) + end end def teardown @@ -124,7 +126,11 @@ class NotificationsMainTest < Test::Unit::TestCase def test_subscriber_with_pattern @another = [] - ActiveSupport::Notifications.subscribe("cache"){ |event| @another << event } + + ActiveSupport::Notifications.subscribe("cache") do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end + ActiveSupport::Notifications.instrument(:cache){ 1 } sleep(0.1) @@ -136,7 +142,9 @@ class NotificationsMainTest < Test::Unit::TestCase def test_subscriber_with_pattern_as_regexp @another = [] - ActiveSupport::Notifications.subscribe(/cache/){ |event| @another << event } + ActiveSupport::Notifications.subscribe(/cache/) do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end ActiveSupport::Notifications.instrument(:something){ 0 } ActiveSupport::Notifications.instrument(:cache){ 1 } @@ -150,7 +158,9 @@ class NotificationsMainTest < Test::Unit::TestCase def test_with_several_consumers_and_several_events @another = [] - ActiveSupport::Notifications.subscribe { |event| @another << event } + ActiveSupport::Notifications.subscribe do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end 1.upto(100) do |i| ActiveSupport::Notifications.instrument(:value){ i } -- cgit v1.2.3 From c9487ed6aff76693f33ff89e466ba944297681d3 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 28 Oct 2009 01:58:33 -0700 Subject: Change Event#thread_id to #transaction_id. Defaults to one "transaction" per thread but you can explicitly declare the start of a new one. This makes it possible for each request to have it own id. --- activesupport/test/notifications_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index b763b740af..9175c8f26e 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -124,6 +124,26 @@ class NotificationsMainTest < Test::Unit::TestCase assert_equal Hash[:payload => "notifications"], @events.last.payload end + def test_subscribed_in_a_transaction + @another = [] + + ActiveSupport::Notifications.subscribe("cache") do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end + + ActiveSupport::Notifications.instrument(:cache){ 1 } + ActiveSupport::Notifications.transaction do + ActiveSupport::Notifications.instrument(:cache){ 1 } + end + ActiveSupport::Notifications.instrument(:cache){ 1 } + + sleep 0.1 + + before, during, after = @another.map {|e| e.transaction_id } + assert_equal before, after + assert_not_equal before, during + end + def test_subscriber_with_pattern @another = [] -- cgit v1.2.3 From a107103e85a2cc294faedddbb44707fd2bc2e206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 28 Oct 2009 09:50:18 -0200 Subject: Allow :instance_reader to be given to superclass_delegating_accessor as well. --- activesupport/test/core_ext/class/delegating_attributes_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/class/delegating_attributes_test.rb b/activesupport/test/core_ext/class/delegating_attributes_test.rb index b51d68551d..beb55ba17e 100644 --- a/activesupport/test/core_ext/class/delegating_attributes_test.rb +++ b/activesupport/test/core_ext/class/delegating_attributes_test.rb @@ -52,6 +52,13 @@ class DelegatingAttributesTest < Test::Unit::TestCase assert !single_class.public_instance_methods.map(&:to_s).include?("both=") end + def test_simple_accessor_declaration_with_instance_reader_false + single_class.superclass_delegating_accessor :no_instance_reader, :instance_reader => false + assert single_class.respond_to?(:no_instance_reader) + assert single_class.respond_to?(:no_instance_reader=) + assert !single_class.public_instance_methods.map(&:to_s).include?("no_instance_reader") + end + def test_working_with_simple_attributes single_class.superclass_delegating_accessor :both -- cgit v1.2.3