diff options
author | José Valim <jose.valim@gmail.com> | 2009-10-01 21:14:38 -0300 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-10-15 18:18:44 -0300 |
commit | 7b7796e23d12b526fa35976c514da91169dd2566 (patch) | |
tree | 62075031fa620c0f96bc8b796b2d317b9c163db6 /activesupport/test | |
parent | 7b5225a529cb9693f3bed8e6023de0d5348efca5 (diff) | |
download | rails-7b7796e23d12b526fa35976c514da91169dd2566.tar.gz rails-7b7796e23d12b526fa35976c514da91169dd2566.tar.bz2 rails-7b7796e23d12b526fa35976c514da91169dd2566.zip |
Events are created inside threads.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/orchestra_test.rb | 73 |
1 files changed, 24 insertions, 49 deletions
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 |