Class: Dynflow::Testing::InThreadExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/dynflow/testing/in_thread_executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(world) ⇒ InThreadExecutor

Returns a new instance of InThreadExecutor.



5
6
7
8
9
# File 'lib/dynflow/testing/in_thread_executor.rb', line 5

def initialize(world)
  @world = world
  @director = Director.new(@world)
  @work_items = Queue.new
end

Instance Method Details

#clock_tickObject



52
53
54
# File 'lib/dynflow/testing/in_thread_executor.rb', line 52

def clock_tick
  @world.clock.progress_all([:periodic_check_inbox])
end

#delayed_event(director_event) ⇒ Object



45
46
47
48
49
50
# File 'lib/dynflow/testing/in_thread_executor.rb', line 45

def delayed_event(director_event)
  @director.handle_event(director_event).each do |work_item|
    @work_items << work_item
  end
  director_event.result
end

#event(execution_plan_id, step_id, event, future = Concurrent::Promises.resolvable_future, optional: false) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/dynflow/testing/in_thread_executor.rb', line 37

def event(execution_plan_id, step_id, event, future = Concurrent::Promises.resolvable_future, optional: false)
  event = (Director::Event[SecureRandom.uuid, execution_plan_id, step_id, event, future, optional])
  @director.handle_event(event).each do |work_item|
    @work_items << work_item
  end
  future
end

#execute(execution_plan_id, finished = Concurrent::Promises.resolvable_future, _wait_for_acceptance = true) ⇒ Object



11
12
13
14
15
# File 'lib/dynflow/testing/in_thread_executor.rb', line 11

def execute(execution_plan_id, finished = Concurrent::Promises.resolvable_future, _wait_for_acceptance = true)
  feed_queue(@director.start_execution(execution_plan_id, finished))
  process_work_items
  finished
end

#feed_queue(work_items) ⇒ Object



56
57
58
59
60
61
# File 'lib/dynflow/testing/in_thread_executor.rb', line 56

def feed_queue(work_items)
  work_items.each do |work_item|
    work_item.world = @world
    @work_items.push(work_item)
  end
end

#handle_work(work_item) ⇒ Object



30
31
32
33
34
35
# File 'lib/dynflow/testing/in_thread_executor.rb', line 30

def handle_work(work_item)
  work_item.execute
  step = work_item.step if work_item.is_a?(Director::StepWorkItem)
  plan_events(step && step.delayed_events) if step && step.delayed_events
  @director.work_finished(work_item)
end

#plan_events(delayed_events) ⇒ Object



24
25
26
27
28
# File 'lib/dynflow/testing/in_thread_executor.rb', line 24

def plan_events(delayed_events)
  delayed_events.each do |event|
    @world.plan_event(event.execution_plan_id, event.step_id, event.event, event.time)
  end
end

#process_work_itemsObject



17
18
19
20
21
22
# File 'lib/dynflow/testing/in_thread_executor.rb', line 17

def process_work_items
  until @work_items.empty?
    feed_queue(handle_work(@work_items.pop))
    clock_tick
  end
end

#terminate(future = Concurrent::Promises.resolvable_future) ⇒ Object



63
64
65
66
67
68
# File 'lib/dynflow/testing/in_thread_executor.rb', line 63

def terminate(future = Concurrent::Promises.resolvable_future)
  @director.terminate
  future.fulfill true
rescue => e
  future.reject e
end