Class: Cadence::Testing::LocalWorkflowContext

Inherits:
Object
  • Object
show all
Defined in:
lib/cadence/testing/local_workflow_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(execution, workflow_id, run_id, disabled_releases, headers = {}) ⇒ LocalWorkflowContext

Returns a new instance of LocalWorkflowContext.



14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



12
13
14
# File 'lib/cadence/testing/local_workflow_context.rb', line 12

def headers
  @headers
end

Instance Method Details

#cancel(target, cancelation_id) ⇒ Object

Raises:

  • (NotImplementedError)


187
188
189
# 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) ⇒ Object

Raises:

  • (NotImplementedError)


183
184
185
# 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) ⇒ Object

Raises:

  • (NotImplementedError)


150
151
152
# 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) ⇒ Object



154
155
156
# File 'lib/cadence/testing/local_workflow_context.rb', line 154

def complete(result = nil)
  result
end

#execute_activity(activity_class, *input, **args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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::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()

  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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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::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()

  activity_class.execute_in_context(context, input)
end

#execute_workflow(workflow_class, *input, **args) ⇒ Object

Raises:

  • (NotImplementedError)


114
115
116
# 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) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Object

Raises:

  • (error_class)


158
159
160
161
162
# 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) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/cadence/testing/local_workflow_context.rb', line 27

def has_release?(change_name)
  !disabled_releases.include?(change_name.to_s)
end

#loggerObject



23
24
25
# File 'lib/cadence/testing/local_workflow_context.rb', line 23

def logger
  Cadence.logger
end

#nowObject



175
176
177
# File 'lib/cadence/testing/local_workflow_context.rb', line 175

def now
  Time.now
end

#on_signal(&block) ⇒ Object

Raises:

  • (NotImplementedError)


179
180
181
# 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) ⇒ Object



133
134
135
# File 'lib/cadence/testing/local_workflow_context.rb', line 133

def side_effect(&block)
  block.call
end

#sleep(timeout) ⇒ Object



137
138
139
# File 'lib/cadence/testing/local_workflow_context.rb', line 137

def sleep(timeout)
  ::Kernel.sleep timeout
end

#sleep_until(end_time) ⇒ Object



141
142
143
144
# 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) ⇒ Object

Raises:

  • (NotImplementedError)


146
147
148
# 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) ⇒ Object



170
171
172
173
# 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) ⇒ Object



164
165
166
167
168
# File 'lib/cadence/testing/local_workflow_context.rb', line 164

def wait_for_all(*futures)
  futures.each(&:wait)

  return
end