|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test' |
| 4 | +require 'patching/my_activities' |
| 5 | +require 'patching/workflow_1_initial' |
| 6 | +require 'patching/workflow_2_patched' |
| 7 | +require 'patching/workflow_3_deprecated' |
| 8 | +require 'patching/workflow_4_complete' |
| 9 | + |
| 10 | +require 'securerandom' |
| 11 | +require 'temporalio/client' |
| 12 | +require 'temporalio/testing' |
| 13 | +require 'temporalio/worker' |
| 14 | + |
| 15 | +module Patching |
| 16 | + class PatchingWorkflowTest < Test |
| 17 | + def setup |
| 18 | + @task_queue = "tq-#{SecureRandom.uuid}" |
| 19 | + end |
| 20 | + |
| 21 | + def with_handle(env, workflow, id) |
| 22 | + Temporalio::Worker.new( |
| 23 | + client: env.client, |
| 24 | + activities: [MyActivities::PrePatch, MyActivities::PostPatch], |
| 25 | + task_queue: @task_queue, |
| 26 | + workflows: [workflow] |
| 27 | + ).run do |
| 28 | + handle = env.client.start_workflow( |
| 29 | + :MyWorkflow, id:, task_queue: @task_queue |
| 30 | + ) |
| 31 | + yield handle |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + def test_workflow |
| 36 | + Temporalio::Testing::WorkflowEnvironment.start_local do |env| |
| 37 | + initial_handle = with_handle(env, MyWorkflow1Initial, 'initial-id') do |handle| |
| 38 | + handle.result |
| 39 | + assert_equal 'pre-patch', handle.query(:result) |
| 40 | + handle |
| 41 | + end |
| 42 | + |
| 43 | + patched_handle = with_handle(env, MyWorkflow2Patched, 'patched-id') do |handle| |
| 44 | + handle.result |
| 45 | + assert_equal 'pre-patch', initial_handle.query(:result) |
| 46 | + assert_equal 'post-patch', handle.query(:result) |
| 47 | + handle |
| 48 | + end |
| 49 | + |
| 50 | + deprecated_handle = with_handle(env, MyWorkflow3Deprecated, 'deprecated-id') do |handle| |
| 51 | + handle.result |
| 52 | + assert_raises(Temporalio::Error::WorkflowQueryFailedError) { initial_handle.query(:result) } |
| 53 | + assert_equal 'post-patch', patched_handle.query(:result) |
| 54 | + assert_equal 'post-patch', handle.query(:result) |
| 55 | + handle |
| 56 | + end |
| 57 | + |
| 58 | + with_handle(env, MyWorkflow4Complete, 'deprecated-id') do |complete_handle| |
| 59 | + complete_handle.result |
| 60 | + assert_raises(Temporalio::Error::WorkflowQueryFailedError) { initial_handle.query(:result) } |
| 61 | + assert_raises(Temporalio::Error::WorkflowQueryFailedError) { patched_handle.query(:result) } |
| 62 | + assert_equal 'post-patch', deprecated_handle.query(:result) |
| 63 | + assert_equal 'post-patch', complete_handle.query(:result) |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | +end |
0 commit comments