Sha256: 8c3a8e4a571e2fc2a73507516ef879fc89f0a095fa9d383195c9a1b4486089ed

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

require_relative 'test_helper'

module Dynflow
  class ExecutionPlan
    describe Hooks do
      include PlanAssertions

      let(:world) { WorldFactory.create_world }

      class Flag
        class << self
          def raise!
            @raised = true
          end

          def raised?
            @raised
          end

          def lower!
            @raised = false
          end
        end
      end

      module FlagHook
        def raise_flag(_execution_plan)
          Flag.raise!
        end

        def controlled_failure(_execution_plan)
          Flag.raise!
          raise "A controlled failure"
        end
      end

      class ActionWithHooks < ::Dynflow::Action
        include FlagHook

        execution_plan_hooks.use :raise_flag, :on => :success
      end

      class ActionOnStop < ::Dynflow::Action
        include FlagHook

        execution_plan_hooks.use :controlled_failure, :on => :stopped
      end

      class Inherited < ActionWithHooks; end
      class Overriden < ActionWithHooks
        execution_plan_hooks.do_not_use :raise_flag
      end

      before { Flag.lower! }

      it 'runs the on_success hook' do
        refute Flag.raised?
        plan = world.trigger(ActionWithHooks)
        plan.finished.wait!
        assert Flag.raised?
      end

      it 'does not alter the execution plan when exception happens in the hook' do
        refute Flag.raised?
        plan = world.plan(ActionOnStop)
        plan = world.execute(plan.id).wait!.value
        assert Flag.raised?
        plan.result.must_equal :success
      end

      it 'inherits the hooks when subclassing' do
        refute Flag.raised?
        plan = world.trigger(Inherited)
        plan.finished.wait!
        assert Flag.raised?
      end

      it 'can override the hooks from the child' do
        refute Flag.raised?
        plan = world.trigger(Overriden)
        plan.finished.wait!
        refute Flag.raised?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dynflow-1.0.2 test/execution_plan_hooks_test.rb
dynflow-1.0.1 test/execution_plan_hooks_test.rb
dynflow-1.0.0 test/execution_plan_hooks_test.rb
dynflow-0.8.37 test/execution_plan_hooks_test.rb
dynflow-0.8.36 test/execution_plan_hooks_test.rb