class Cadence::Testing::LocalWorkflowContext

Attributes

disabled_releases[R]
execution[R]
headers[R]
run_id[R]
workflow_id[R]

Public Class Methods

new(execution, workflow_id, run_id, disabled_releases, headers = {}) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 14
def initialize(execution, workflow_id, run_id, disabled_releases, headers = {})
  @last_event_id = 0
  @execution = execution
  @run_id = run_id
  @workflow_id = workflow_id
  @disabled_releases = disabled_releases
  @headers = headers
end

Public Instance Methods

cancel(target, cancelation_id) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 187
def cancel(target, cancelation_id)
  raise NotImplementedError, 'not yet available for testing'
end
cancel_activity(activity_id) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 183
def cancel_activity(activity_id)
  raise NotImplementedError, 'not yet available for testing'
end
cancel_timer(timer_id) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 150
def cancel_timer(timer_id)
  raise NotImplementedError, 'not yet available for testing'
end
complete(result = nil) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 154
def complete(result = nil)
  result
end
execute_activity(activity_class, *input, **args) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 31
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(
    domain: execution_options.domain,
    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,
    timeouts: {
      start_to_close: 30,
      schedule_to_close: 60,
      heartbeat: 5
    }
  )
  context = LocalActivityContext.new(metadata)

  result = activity_class.execute_in_context(context, input)

  if context.async?
    execution.register_future(context.async_token, future)
  else
    # Fulfil the future straigt away for non-async activities
    future.set(result)
  end

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

  if future.failed?
    reason, details = result

    error_class = safe_constantize(reason) || Cadence::ActivityException

    raise error_class, details
  end

  result
end
execute_local_activity(activity_class, *input, **args) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 87
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(
    domain: execution_options.domain,
    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,
    timeouts: {
      schedule_to_close: 60,
      start_to_close: 30,
      heartbeat: 5
    }
  )
  context = LocalActivityContext.new(metadata)

  activity_class.execute_in_context(context, input)
end
execute_workflow(workflow_class, *input, **args) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 114
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/cadence/testing/local_workflow_context.rb, line 118
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 = Cadence::Testing::LocalWorkflowContext.new(
    execution, workflow_id, run_id, workflow_class.disabled_releases, execution_options.headers
  )

  workflow_class.execute_in_context(context, input)
end
fail(reason, details = nil) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 158
def fail(reason, details = nil)
  error_class = safe_constantize(reason) || StandardError

  raise error_class, details
end
has_release?(change_name) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 27
def has_release?(change_name)
  !disabled_releases.include?(change_name.to_s)
end
logger() click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 23
def logger
  Cadence.logger
end
now() click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 175
def now
  Time.now
end
on_signal(&block) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 179
def on_signal(&block)
  raise NotImplementedError, 'not yet available for testing'
end
side_effect(&block) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 133
def side_effect(&block)
  block.call
end
sleep(timeout) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 137
def sleep(timeout)
  ::Kernel.sleep timeout
end
sleep_until(end_time) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 141
def sleep_until(end_time)
  delay = (end_time.to_time - now).to_i
  sleep(delay) if delay > 0
end
start_timer(timeout, timer_id = nil) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 146
def start_timer(timeout, timer_id = nil)
  raise NotImplementedError, 'not yet available for testing'
end
wait_for(future) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 170
def wait_for(future)
  # Point of communication
  Fiber.yield while !future.finished?
end
wait_for_all(*futures) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 164
def wait_for_all(*futures)
  futures.each(&:wait)

  return
end

Private Instance Methods

next_event_id() click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 195
def next_event_id
  @last_event_id += 1
  @last_event_id
end
safe_constantize(const) click to toggle source
# File lib/cadence/testing/local_workflow_context.rb, line 200
def safe_constantize(const)
  Object.const_get(const) if Object.const_defined?(const)
rescue NameError
  nil
end