diff --git a/activesupport/lib/active_support/orchestra.rb b/activesupport/lib/active_support/orchestra.rb index 96e6ce886534a8c77bc9507c50ebef8775607800..5f571274017f8af49f1d7621024e371d69a378e2 100644 --- a/activesupport/lib/active_support/orchestra.rb +++ b/activesupport/lib/active_support/orchestra.rb @@ -112,6 +112,11 @@ def initialize(name, payload) @result = @payload.delete(:result) @duration = @payload.delete(:duration) end + + def parent_of?(event) + start = (self.time - event.time) * 1000 + start <= 0 && (start + self.duration >= event.duration) + end end # This is a default queue implementation that ships with Orchestra. It diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 7a6e9208b4368eac0a0070ceb519c31fa96b5048..1b2f98c7dd6f8577fc244232402939bdf3a62683 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -9,20 +9,36 @@ def clear 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