class Temporal::Testing::LocalWorkflowContext

Attributes

disabled_releases[R]
execution[R]
metadata[R]
run_id[R]
workflow_id[R]

Public Class Methods

new(execution, workflow_id, run_id, disabled_releases, metadata) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 14
def initialize(execution, workflow_id, run_id, disabled_releases, metadata)
  @last_event_id = 0
  @execution = execution
  @run_id = run_id
  @workflow_id = workflow_id
  @disabled_releases = disabled_releases
  @metadata = metadata
  @completed = false
end

Public Instance Methods

cancel(target, cancelation_id) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 184
def cancel(target, cancelation_id)
  raise NotImplementedError, 'not yet available for testing'
end
cancel_activity(activity_id) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 180
def cancel_activity(activity_id)
  raise NotImplementedError, 'not yet available for testing'
end
cancel_timer(timer_id) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 147
def cancel_timer(timer_id)
  raise NotImplementedError, 'not yet available for testing'
end
complete(result = nil) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 151
def complete(result = nil)
  completed!
  result
end
completed?() click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 24
def completed?
  @completed
end
execute_activity(activity_class, *input, **args) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 40
def execute_activity(activity_class, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  event_id = next_event_id
  activity_id = options[:activity_id] || event_id

  target = Workflow::History::EventTarget.new(event_id, Workflow::History::EventTarget::ACTIVITY_TYPE)
  future = Workflow::Future.new(target, self, cancelation_id: activity_id)

  execution_options = ExecutionOptions.new(activity_class, options)
  metadata = Metadata::Activity.new(
    namespace: execution_options.namespace,
    id: activity_id,
    name: execution_options.name,
    task_token: nil,
    attempt: 1,
    workflow_run_id: run_id,
    workflow_id: workflow_id,
    workflow_name: nil, # not yet used, but will be in the future
    headers: execution_options.headers,
    heartbeat_details: nil
  )
  context = LocalActivityContext.new(metadata)

  begin
    result = activity_class.execute_in_context(context, input)
  rescue StandardError => e
    # Capture any failure from running the activity into the future
    # instead of raising immediately in order to match the behavior of
    # running against a Temporal server.
    future.fail(e)
  else
    if context.async?
      execution.register_future(context.async_token, future)
    else
      # Fulfill the future straight away for non-async activities
      future.set(result)
    end
  end

  future
end
execute_activity!(activity_class, *input, **args) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 84
def execute_activity!(activity_class, *input, **args)
  future = execute_activity(activity_class, *input, **args)
  result_or_exception = future.get

  raise result_or_exception if future.failed?

  result_or_exception
end
execute_local_activity(activity_class, *input, **args) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 93
def execute_local_activity(activity_class, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(activity_class, options)
  activity_id = options[:activity_id] || SecureRandom.uuid
  metadata = Metadata::Activity.new(
    namespace: execution_options.namespace,
    id: activity_id,
    name: execution_options.name,
    task_token: nil,
    attempt: 1,
    workflow_run_id: run_id,
    workflow_id: workflow_id,
    workflow_name: nil, # not yet used, but will be in the future
    headers: execution_options.headers,
    heartbeat_details: nil
  )
  context = LocalActivityContext.new(metadata)

  activity_class.execute_in_context(context, input)
end
execute_workflow(workflow_class, *input, **args) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 116
def execute_workflow(workflow_class, *input, **args)
  raise NotImplementedError, 'not yet available for testing'
end
execute_workflow!(workflow_class, *input, **args) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 120
def execute_workflow!(workflow_class, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution = WorkflowExecution.new
  workflow_id = SecureRandom.uuid
  run_id = SecureRandom.uuid
  execution_options = ExecutionOptions.new(workflow_class, options)
  context = Temporal::Testing::LocalWorkflowContext.new(
    execution, workflow_id, run_id, workflow_class.disabled_releases, execution_options.headers
  )

  workflow_class.execute_in_context(context, input)
end
fail(exception) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 156
def fail(exception)
  completed!
  raise exception
end
has_release?(change_name) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 36
def has_release?(change_name)
  !disabled_releases.include?(change_name.to_s)
end
headers() click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 32
def headers
  metadata.headers
end
logger() click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 28
def logger
  Temporal.logger
end
now() click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 172
def now
  Time.now
end
on_signal(&block) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 176
def on_signal(&block)
  raise NotImplementedError, 'not yet available for testing'
end
side_effect(&block) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 135
def side_effect(&block)
  block.call
end
sleep(timeout) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 139
def sleep(timeout)
  ::Kernel.sleep timeout
end
start_timer(timeout, timer_id = nil) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 143
def start_timer(timeout, timer_id = nil)
  raise NotImplementedError, 'not yet available for testing'
end
wait_for(future) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 167
def wait_for(future)
  # Point of communication
  Fiber.yield while !future.finished?
end
wait_for_all(*futures) click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 161
def wait_for_all(*futures)
  futures.each(&:wait)

  return
end

Private Instance Methods

completed!() click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 192
def completed!
  @completed = true
end
next_event_id() click to toggle source
# File lib/temporal/testing/local_workflow_context.rb, line 196
def next_event_id
  @last_event_id += 1
  @last_event_id
end