-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathcontext_spec.rb
More file actions
570 lines (456 loc) · 18.1 KB
/
context_spec.rb
File metadata and controls
570 lines (456 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
require 'temporal/activity'
require 'temporal/workflow'
require 'temporal/workflow/context'
require 'temporal/workflow/dispatcher'
require 'temporal/workflow/future'
require 'temporal/workflow/query_registry'
require 'temporal/workflow/stack_trace_tracker'
require 'temporal/metadata/workflow'
require 'time'
class MyTestWorkflow < Temporal::Workflow; end
class MyTestActivity < Temporal::Activity
RETURN_VALUE = 'this-is-a-return-value'.freeze
def execute
RETURN_VALUE
end
end
describe Temporal::Workflow::Context do
let(:state_manager) { instance_double('Temporal::Workflow::StateManager') }
let(:dispatcher) { Temporal::Workflow::Dispatcher.new }
let(:query_registry) do
double = instance_double('Temporal::Workflow::QueryRegistry')
allow(double).to receive(:register)
double
end
let(:metadata_hash) { Fabricate(:workflow_metadata).to_h }
let(:metadata) { Temporal::Metadata::Workflow.new(**metadata_hash) }
let(:config) { Temporal::Configuration.new }
let(:workflow_context) do
Temporal::Workflow::Context.new(
state_manager,
dispatcher,
MyTestWorkflow,
metadata,
config,
query_registry,
track_stack_trace
)
end
let(:child_workflow_execution) { Fabricate(:api_workflow_execution) }
let(:track_stack_trace) { false }
describe '#on_query' do
let(:handler) { Proc.new {} }
it 'registers a query with the query registry' do
workflow_context.on_query('test-query', &handler)
expect(query_registry).to have_received(:register).with('test-query') do |&block|
expect(block).to eq(handler)
end
end
it 'automatically registers stack trace query' do
expect(workflow_context).to_not be(nil) # ensure constructor is called
expect(query_registry).to have_received(:register)
.with(Temporal::Workflow::StackTraceTracker::STACK_TRACE_QUERY_NAME)
end
context 'stack trace' do
let(:track_stack_trace) { true }
let(:query_registry) { Temporal::Workflow::QueryRegistry.new }
it 'cleared to start' do
expect(workflow_context).to_not be(nil) # ensure constructor is called
stack_trace = query_registry.handle(Temporal::Workflow::StackTraceTracker::STACK_TRACE_QUERY_NAME)
expect(stack_trace).to eq("Fiber count: 0\n")
end
end
end
describe '#execute_activity' do
context "with header propagation" do
class TestHeaderPropagator
def inject!(header)
header['test'] = 'asdf'
end
end
it 'propagates the header' do
config.add_header_propagator(TestHeaderPropagator)
expect(state_manager).to receive(:schedule).with(Temporal::Workflow::Command::ScheduleActivity.new(
activity_id: nil,
activity_type: 'MyTestActivity',
input: [],
task_queue: 'default-task-queue',
retry_policy: nil,
timeouts: {execution: 315360000, run: 315360000, task: 10, schedule_to_close: nil, schedule_to_start: nil, start_to_close: 30, heartbeat: nil, default_heartbeat_throttle_interval: 30, max_heartbeat_throttle_interval: 60},
headers: { 'test' => 'asdf' }
))
allow(dispatcher).to receive(:register_handler)
workflow_context.execute_activity(MyTestActivity)
end
end
end
describe '#execute_local_activity' do
it 'executes and schedules command' do
expect(state_manager).to receive(:next_side_effect)
expect(state_manager).to receive(:schedule).with(
Temporal::Workflow::Command::RecordMarker.new(
name: 'SIDE_EFFECT',
details: MyTestActivity::RETURN_VALUE
)
)
return_value = workflow_context.execute_local_activity(MyTestActivity)
expect(return_value).to eq(MyTestActivity::RETURN_VALUE)
end
end
describe '#execute_workflow' do
it 'returns the correct futures when starting a child workflow' do
allow(state_manager).to receive(:schedule)
allow(dispatcher).to receive(:register_handler)
result = workflow_context.execute_workflow(MyTestWorkflow)
expect(result).to be_instance_of(Temporal::Workflow::ChildWorkflowFuture)
expect(result.child_workflow_execution_future).to be_instance_of(Temporal::Workflow::Future)
end
it 'futures behave as expected when events are successful' do
started_proc = nil
completed_proc = nil
allow(state_manager).to receive(:schedule)
allow(dispatcher).to receive(:register_handler) do |target, event_name, &handler|
case event_name
when 'started'
started_proc = handler
when 'completed'
completed_proc = handler
end
end
child_workflow_future = workflow_context.execute_workflow(MyTestWorkflow)
# expect all futures to be false as nothing has happened
expect(child_workflow_future.finished?).to be false
expect(child_workflow_future.child_workflow_execution_future.finished?).to be false
# dispatch the start event and check if the child workflow execution changes to true
started_proc.call(child_workflow_execution)
expect(child_workflow_future.finished?).to be false
expect(child_workflow_future.child_workflow_execution_future.finished?).to be true
expect(child_workflow_future.child_workflow_execution_future.get).to be_instance_of(Temporalio::Api::Common::V1::WorkflowExecution)
# complete the workflow via dispatch and check if the child workflow future is finished
completed_proc.call('finished result')
expect(child_workflow_future.finished?).to be true
expect(child_workflow_future.child_workflow_execution_future.finished?).to be true
end
it 'futures behave as expected when child workflow fails' do
started_proc = nil
failed_proc = nil
allow(state_manager).to receive(:schedule)
allow(dispatcher).to receive(:register_handler) do |target, event_name, &handler|
case event_name
when 'started'
started_proc = handler
when 'failed'
failed_proc = handler
end
end
child_workflow_future = workflow_context.execute_workflow(MyTestWorkflow)
# expect all futures to be false as nothing has happened
expect(child_workflow_future.finished?).to be false
expect(child_workflow_future.child_workflow_execution_future.finished?).to be false
started_proc.call(child_workflow_execution)
# dispatch the failed event and check the child_workflow_future failed but the child_workflow_execution_future finished
failed_proc.call(Temporal::Workflow::Errors.generate_error_for_child_workflow_start("failed to start", "random-workflow-id"))
expect(child_workflow_future.failed?).to be true
expect(child_workflow_future.child_workflow_execution_future.failed?).to be false
end
it 'futures behave as expected when child execution workflow fails to start' do
failed_proc = nil
allow(state_manager).to receive(:schedule)
allow(dispatcher).to receive(:register_handler) do |target, event_name, &handler|
case event_name
when 'failed'
failed_proc = handler
end
end
child_workflow_future = workflow_context.execute_workflow(MyTestWorkflow)
# expect all futures to be false as nothing has happened
expect(child_workflow_future.finished?).to be false
expect(child_workflow_future.child_workflow_execution_future.finished?).to be false
# dispatch the failed event and check what happens
failed_proc.call(Temporal::Workflow::Errors.generate_error_for_child_workflow_start("failed to start", "random-workflow-id"))
expect(child_workflow_future.failed?).to be true
expect(child_workflow_future.child_workflow_execution_future.failed?).to be true
end
end
describe '#execute_workflow!' do
let(:child_workflow_future) do
double = instance_double('Temporal::Workflow::ChildWorkflowFuture')
allow(double).to receive(:get).and_return(result)
double
end
before do
expect(workflow_context).to receive(:execute_workflow).and_return(child_workflow_future)
end
context 'when future fails' do
let(:result) { Temporal::WorkflowRunError }
it 'raises the future result exception' do
expect(child_workflow_future).to receive(:failed?).and_return(true)
expect { workflow_context.execute_workflow!(MyTestWorkflow) }.to raise_error(result)
end
end
context 'when future succeeds' do
let(:result) { 'result' }
it 'returns the future result' do
expect(child_workflow_future).to receive(:failed?).and_return(false)
expect(workflow_context.execute_workflow!(MyTestWorkflow)).to eq(result)
end
end
end
describe '#schedule_workflow' do
let(:cron_schedule) { '* * * * *' }
context 'when given workflow options' do
it 'executes workflow with merged cron_schedule option' do
expect(workflow_context).to receive(:execute_workflow).with(MyTestWorkflow,
options: {
parent_close_policy: :abandon,
cron_schedule: cron_schedule
}
)
workflow_context.schedule_workflow(MyTestWorkflow, cron_schedule, options: { parent_close_policy: :abandon })
end
end
context 'when not given workflow options' do
it 'executes workflow with cron_schedule option' do
expect(workflow_context).to receive(:execute_workflow).with(MyTestWorkflow,
options: {
cron_schedule: cron_schedule
}
)
workflow_context.schedule_workflow(MyTestWorkflow, cron_schedule)
end
end
end
describe '#signal_external_workflow' do
let(:workflow) { 'workflow_type' }
let(:signal_name) { 'my_signal' }
let(:signal_input) { {foo: 'bar'} }
let(:workflow_id) { '12345' }
let(:run_id) { 'abcdef' }
let(:target) do
Temporal::Workflow::History::EventTarget.new(27, Temporal::Workflow::History::EventTarget::EXTERNAL_WORKFLOW_TYPE)
end
def send_signal
expect(state_manager).to receive(:schedule).with(
Temporal::Workflow::Command::SignalExternalWorkflow.new(
namespace: 'default-namespace',
execution: {
workflow_id: workflow_id,
run_id: run_id
},
signal_name: signal_name,
input: signal_input,
child_workflow_only: false
)
).and_return(target)
workflow_context.signal_external_workflow(workflow, signal_name, workflow_id, run_id, signal_input)
end
it 'send signal successfully' do
future = send_signal
expect(future.finished?).to be(false)
expect(future.failed?).to be(false)
dispatcher.dispatch(target, 'completed')
expect(future.finished?).to be(true)
expect(future.failed?).to be(false)
# no waiting for this
future.wait
expect(future.get).to eq(nil)
end
it 'send signal to not found workflow' do
future = send_signal
expect(future.finished?).to be(false)
expect(future.failed?).to be(false)
error = Temporal::ExternalSignalExecutionNotFoundError.new
dispatcher.dispatch(target, 'failed', error)
expect(future.finished?).to be(true)
expect(future.failed?).to be(true)
# no waiting for either of these
future.wait
expect(future.get).to eq(error)
end
end
describe '#upsert_search_attributes' do
it 'does not accept nil' do
expect do
workflow_context.upsert_search_attributes(nil)
end.to raise_error(ArgumentError, 'search_attributes cannot be nil')
end
it 'requires a hash' do
expect do
workflow_context.upsert_search_attributes(['array_not_supported'])
end.to raise_error(ArgumentError, 'for search_attributes, expecting a Hash, not Array')
end
it 'requires a non-empty hash' do
expect do
workflow_context.upsert_search_attributes({})
end.to raise_error(ArgumentError, 'Cannot upsert an empty hash for search_attributes, as this would do nothing.')
end
it 'creates a command to execute the request' do
expect(state_manager).to receive(:schedule)
.with an_instance_of(Temporal::Workflow::Command::UpsertSearchAttributes)
workflow_context.upsert_search_attributes({ 'CustomIntField' => 5 })
end
it 'converts a Time to the ISO8601 UTC format expected by the Temporal server' do
time = Time.now
allow(state_manager).to receive(:schedule)
.with an_instance_of(Temporal::Workflow::Command::UpsertSearchAttributes)
expect(
workflow_context.upsert_search_attributes({'CustomDatetimeField' => time})
).to eq({ 'CustomDatetimeField' => time.utc.iso8601 })
end
it 'gets latest search attributes from state_manager' do
search_attributes = { 'CustomIntField' => 42 }
expect(state_manager).to receive(:search_attributes).and_return(search_attributes)
expect(workflow_context.search_attributes).to eq(search_attributes)
end
end
describe '#name' do
it 'returns the name from the metadata' do
# Set in the :workflow_metadata Fabricator
expect(workflow_context.name).to eq("TestWorkflow")
end
end
describe '#wait_for_all' do
let(:target_1) { 'target1' }
let(:future_1) { Temporal::Workflow::Future.new(target_1, workflow_context) }
let(:target_2) { 'target2' }
let(:future_2) { Temporal::Workflow::Future.new(target_2, workflow_context) }
def wait_for_all
unblocked = false
Fiber.new do
workflow_context.wait_for_all(future_1, future_2)
unblocked = true
end.resume
proc { unblocked }
end
it 'no futures returns immediately' do
workflow_context.wait_for_all
end
it 'futures already finished' do
future_1.set('done')
future_2.set('also done')
check_unblocked = wait_for_all
expect(check_unblocked.call).to be(true)
end
it 'futures finished' do
check_unblocked = wait_for_all
future_1.set('done')
dispatcher.dispatch(target_1, 'foo')
expect(check_unblocked.call).to be(false)
future_2.set('also done')
dispatcher.dispatch(target_2, 'foo')
expect(check_unblocked.call).to be(true)
end
end
describe '#wait_for_any' do
let(:target_1) { 'target1' }
let(:future_1) { Temporal::Workflow::Future.new(target_1, workflow_context) }
let(:target_2) { 'target2' }
let(:future_2) { Temporal::Workflow::Future.new(target_2, workflow_context) }
def wait_for_any
unblocked = false
Fiber.new do
workflow_context.wait_for_any(future_1, future_2)
unblocked = true
end.resume
proc { unblocked }
end
it 'no futures returns immediately' do
workflow_context.wait_for_any
end
it 'one future already finished' do
future_1.set("it's done")
check_unblocked = wait_for_any
expect(check_unblocked.call).to be(true)
end
it 'one future becomes finished' do
check_unblocked = wait_for_any
future_1.set("it's done")
dispatcher.dispatch(target_1, 'foo')
expect(check_unblocked.call).to be(true)
# Dispatch a second time. This should not attempt to
# resume the fiber which by now should already be dead.
dispatcher.dispatch(target_1, 'foo')
end
it 'both futures becomes finished' do
check_unblocked = wait_for_any
future_1.set("it's done")
future_2.set("it's done")
dispatcher.dispatch(target_1, 'foo')
dispatcher.dispatch(target_2, 'foo')
expect(check_unblocked.call).to be(true)
end
it 'one future dispatched but not finished' do
check_unblocked = wait_for_any
dispatcher.dispatch(target_1, 'foo')
expect(check_unblocked.call).to be(false)
end
context 'stack trace' do
let(:track_stack_trace) { true }
let(:query_registry) { Temporal::Workflow::QueryRegistry.new }
it 'is recorded' do
wait_for_any
stack_trace = query_registry.handle(Temporal::Workflow::StackTraceTracker::STACK_TRACE_QUERY_NAME)
expect(stack_trace).to start_with('Fiber count: 1')
expect(stack_trace).to include('block in wait_for_any')
end
it 'cleared after unblocked' do
wait_for_any
future_1.set("it's done")
future_2.set("it's done")
dispatcher.dispatch(target_1, 'foo')
dispatcher.dispatch(target_2, 'foo')
stack_trace = query_registry.handle(Temporal::Workflow::StackTraceTracker::STACK_TRACE_QUERY_NAME)
expect(stack_trace).to eq("Fiber count: 0\n")
end
end
end
describe '#wait_until' do
def wait_until(&blk)
unblocked = false
Fiber.new do
workflow_context.wait_until(&blk)
unblocked = true
end.resume
proc { unblocked }
end
it 'block already true' do
check_unblocked = wait_until { true }
expect(check_unblocked.call).to be(true)
end
it 'block is always false' do
check_unblocked = wait_until { false }
dispatcher.dispatch('target', 'foo')
expect(check_unblocked.call).to be(false)
end
it 'block becomes true' do
value = false
check_unblocked = wait_until { value }
expect(check_unblocked.call).to be(false)
dispatcher.dispatch('target', 'foo')
expect(check_unblocked.call).to be(false)
value = true
dispatcher.dispatch('target', 'foo')
expect(check_unblocked.call).to be(true)
# Can dispatch again safely without resuming dead fiber
dispatcher.dispatch('target', 'foo')
end
context 'stack trace' do
let(:track_stack_trace) { true }
let(:query_registry) { Temporal::Workflow::QueryRegistry.new }
it 'is recorded' do
wait_until { false }
stack_trace = query_registry.handle(Temporal::Workflow::StackTraceTracker::STACK_TRACE_QUERY_NAME)
expect(stack_trace).to start_with('Fiber count: 1')
expect(stack_trace).to include('block in wait_until')
end
it 'cleared after unblocked' do
value = false
wait_until { value }
value = true
dispatcher.dispatch('target', 'foo')
stack_trace = query_registry.handle(Temporal::Workflow::StackTraceTracker::STACK_TRACE_QUERY_NAME)
expect(stack_trace).to eq("Fiber count: 0\n")
end
end
end
end