|
| 1 | +import inspect |
| 2 | +import time |
| 3 | +import unittest |
| 4 | + |
| 5 | +from iwf.client import Client |
| 6 | +from iwf.iwf_api.models.workflow_stop_type import WorkflowStopType |
| 7 | +from iwf.stop_workflow_options import StopWorkflowOptions |
| 8 | +from iwf.tests import registry |
| 9 | +from iwf.tests.workflows.rpc_memo_workflow import ( |
| 10 | + RpcMemoWorkflow, |
| 11 | + TEST_DATA_OBJECT_KEY, |
| 12 | + TEST_SEARCH_ATTRIBUTE_INT, |
| 13 | + TEST_SEARCH_ATTRIBUTE_KEY, |
| 14 | + RPC_INPUT, |
| 15 | + RPC_OUTPUT, |
| 16 | + TEST_STR, |
| 17 | + TEST_DELAY, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class TestRpcWithMemo(unittest.TestCase): |
| 22 | + @classmethod |
| 23 | + def setUpClass(cls): |
| 24 | + cls.client = Client(registry) |
| 25 | + |
| 26 | + def test_rpc_memo_workflow_func1(self): |
| 27 | + wf_id = f"{inspect.currentframe().f_code.co_name}-{time.time_ns()}" |
| 28 | + run_id = self.client.start_workflow(RpcMemoWorkflow, wf_id, 30, 999) |
| 29 | + |
| 30 | + self.client.invoke_rpc( |
| 31 | + wf_id, RpcMemoWorkflow.test_rpc_set_data_attribute, TEST_STR |
| 32 | + ) |
| 33 | + value = self.client.invoke_rpc( |
| 34 | + wf_id, RpcMemoWorkflow.test_rpc_get_data_attribute_strong_consistency |
| 35 | + ) |
| 36 | + assert value == TEST_STR |
| 37 | + time.sleep(TEST_DELAY) |
| 38 | + value = self.client.invoke_rpc( |
| 39 | + wf_id, RpcMemoWorkflow.test_rpc_get_data_attribute, None, str |
| 40 | + ) |
| 41 | + |
| 42 | + self.client.invoke_rpc(wf_id, RpcMemoWorkflow.test_rpc_set_data_attribute, None) |
| 43 | + value = self.client.invoke_rpc( |
| 44 | + wf_id, RpcMemoWorkflow.test_rpc_get_data_attribute_strong_consistency |
| 45 | + ) |
| 46 | + assert value is None |
| 47 | + time.sleep(TEST_DELAY) |
| 48 | + value = self.client.invoke_rpc( |
| 49 | + wf_id, RpcMemoWorkflow.test_rpc_get_data_attribute |
| 50 | + ) |
| 51 | + assert value is None |
| 52 | + |
| 53 | + self.client.invoke_rpc(wf_id, RpcMemoWorkflow.test_rpc_set_keyword, TEST_STR) |
| 54 | + value = self.client.invoke_rpc( |
| 55 | + wf_id, RpcMemoWorkflow.test_rpc_get_keyword_strong_consistency |
| 56 | + ) |
| 57 | + assert value == TEST_STR |
| 58 | + time.sleep(TEST_DELAY) |
| 59 | + value = self.client.invoke_rpc(wf_id, RpcMemoWorkflow.test_rpc_get_keyword) |
| 60 | + assert value == TEST_STR |
| 61 | + |
| 62 | + self.client.invoke_rpc(wf_id, RpcMemoWorkflow.test_rpc_set_keyword, None) |
| 63 | + value = self.client.invoke_rpc( |
| 64 | + wf_id, RpcMemoWorkflow.test_rpc_get_keyword_strong_consistency |
| 65 | + ) |
| 66 | + assert value is None |
| 67 | + time.sleep(TEST_DELAY) |
| 68 | + value = self.client.invoke_rpc(wf_id, RpcMemoWorkflow.test_rpc_get_keyword) |
| 69 | + assert value is None |
| 70 | + |
| 71 | + rpc_output = self.client.invoke_rpc( |
| 72 | + wf_id, RpcMemoWorkflow.test_rpc_func1, RPC_INPUT |
| 73 | + ) |
| 74 | + assert RPC_OUTPUT == rpc_output |
| 75 | + |
| 76 | + self.client.wait_for_workflow_completion(wf_id) |
| 77 | + |
| 78 | + data_attributes = self.client.get_workflow_data_attributes( |
| 79 | + RpcMemoWorkflow, wf_id, run_id, [TEST_DATA_OBJECT_KEY] |
| 80 | + ) |
| 81 | + assert TEST_DATA_OBJECT_KEY in data_attributes |
| 82 | + assert data_attributes[TEST_DATA_OBJECT_KEY] == RPC_INPUT |
| 83 | + |
| 84 | + search_attributes = self.client.get_workflow_search_attributes( |
| 85 | + RpcMemoWorkflow, |
| 86 | + wf_id, |
| 87 | + [TEST_SEARCH_ATTRIBUTE_KEY, TEST_SEARCH_ATTRIBUTE_INT], |
| 88 | + run_id, |
| 89 | + ) |
| 90 | + assert TEST_SEARCH_ATTRIBUTE_INT in search_attributes |
| 91 | + assert TEST_SEARCH_ATTRIBUTE_KEY in search_attributes |
| 92 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_INT] == RPC_OUTPUT |
| 93 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_KEY] == RPC_INPUT |
| 94 | + |
| 95 | + def test_rpc_memo_workflow_func0(self): |
| 96 | + wf_id = f"{inspect.currentframe().f_code.co_name}-{time.time_ns()}" |
| 97 | + run_id = self.client.start_workflow(RpcMemoWorkflow, wf_id, 30, 999) |
| 98 | + |
| 99 | + rpc_output = self.client.invoke_rpc( |
| 100 | + wf_id, RpcMemoWorkflow.test_rpc_func0, TEST_STR |
| 101 | + ) |
| 102 | + assert RPC_OUTPUT == rpc_output |
| 103 | + |
| 104 | + self.client.wait_for_workflow_completion(wf_id) |
| 105 | + |
| 106 | + data_attributes = self.client.get_workflow_data_attributes( |
| 107 | + RpcMemoWorkflow, wf_id, run_id, [TEST_DATA_OBJECT_KEY] |
| 108 | + ) |
| 109 | + assert TEST_DATA_OBJECT_KEY in data_attributes |
| 110 | + assert data_attributes[TEST_DATA_OBJECT_KEY] == TEST_STR |
| 111 | + |
| 112 | + search_attributes = self.client.get_workflow_search_attributes( |
| 113 | + RpcMemoWorkflow, |
| 114 | + wf_id, |
| 115 | + [TEST_SEARCH_ATTRIBUTE_KEY, TEST_SEARCH_ATTRIBUTE_INT], |
| 116 | + run_id, |
| 117 | + ) |
| 118 | + assert TEST_SEARCH_ATTRIBUTE_INT in search_attributes |
| 119 | + assert TEST_SEARCH_ATTRIBUTE_KEY in search_attributes |
| 120 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_INT] == RPC_OUTPUT |
| 121 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_KEY] == TEST_STR |
| 122 | + |
| 123 | + def test_rpc_memo_workflow_proc1(self): |
| 124 | + wf_id = f"{inspect.currentframe().f_code.co_name}-{time.time_ns()}" |
| 125 | + run_id = self.client.start_workflow(RpcMemoWorkflow, wf_id, 30, 999) |
| 126 | + |
| 127 | + self.client.invoke_rpc(wf_id, RpcMemoWorkflow.test_rpc_proc1, RPC_INPUT) |
| 128 | + self.client.wait_for_workflow_completion(wf_id) |
| 129 | + |
| 130 | + data_attributes = self.client.get_workflow_data_attributes( |
| 131 | + RpcMemoWorkflow, wf_id, run_id, [TEST_DATA_OBJECT_KEY] |
| 132 | + ) |
| 133 | + assert TEST_DATA_OBJECT_KEY in data_attributes |
| 134 | + assert data_attributes[TEST_DATA_OBJECT_KEY] == RPC_INPUT |
| 135 | + |
| 136 | + search_attributes = self.client.get_workflow_search_attributes( |
| 137 | + RpcMemoWorkflow, |
| 138 | + wf_id, |
| 139 | + [TEST_SEARCH_ATTRIBUTE_KEY, TEST_SEARCH_ATTRIBUTE_INT], |
| 140 | + run_id, |
| 141 | + ) |
| 142 | + assert TEST_SEARCH_ATTRIBUTE_INT in search_attributes |
| 143 | + assert TEST_SEARCH_ATTRIBUTE_KEY in search_attributes |
| 144 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_INT] == RPC_OUTPUT |
| 145 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_KEY] == RPC_INPUT |
| 146 | + |
| 147 | + def test_rpc_memo_workflow_proc0(self): |
| 148 | + wf_id = f"{inspect.currentframe().f_code.co_name}-{time.time_ns()}" |
| 149 | + run_id = self.client.start_workflow(RpcMemoWorkflow, wf_id, 30, 999) |
| 150 | + |
| 151 | + self.client.invoke_rpc(wf_id, RpcMemoWorkflow.test_rpc_proc0) |
| 152 | + self.client.wait_for_workflow_completion(wf_id) |
| 153 | + |
| 154 | + data_attributes = self.client.get_workflow_data_attributes( |
| 155 | + RpcMemoWorkflow, wf_id, run_id, [TEST_DATA_OBJECT_KEY] |
| 156 | + ) |
| 157 | + assert TEST_DATA_OBJECT_KEY in data_attributes |
| 158 | + assert data_attributes[TEST_DATA_OBJECT_KEY] == TEST_STR |
| 159 | + |
| 160 | + search_attributes = self.client.get_workflow_search_attributes( |
| 161 | + RpcMemoWorkflow, |
| 162 | + wf_id, |
| 163 | + [TEST_SEARCH_ATTRIBUTE_KEY, TEST_SEARCH_ATTRIBUTE_INT], |
| 164 | + run_id, |
| 165 | + ) |
| 166 | + assert TEST_SEARCH_ATTRIBUTE_INT in search_attributes |
| 167 | + assert TEST_SEARCH_ATTRIBUTE_KEY in search_attributes |
| 168 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_INT] == RPC_OUTPUT |
| 169 | + assert search_attributes[TEST_SEARCH_ATTRIBUTE_KEY] == TEST_STR |
| 170 | + |
| 171 | + def test_rpc_memo_workflow_func1_readonly(self): |
| 172 | + wf_id = f"{inspect.currentframe().f_code.co_name}-{time.time_ns()}" |
| 173 | + self.client.start_workflow(RpcMemoWorkflow, wf_id, 30, 999) |
| 174 | + |
| 175 | + rpc_output = self.client.invoke_rpc( |
| 176 | + wf_id, RpcMemoWorkflow.test_rpc_func1_readonly, RPC_INPUT |
| 177 | + ) |
| 178 | + assert RPC_OUTPUT == rpc_output |
| 179 | + |
| 180 | + self.client.stop_workflow( |
| 181 | + wf_id, |
| 182 | + StopWorkflowOptions( |
| 183 | + workflow_stop_type=WorkflowStopType.FAIL, |
| 184 | + reason=TEST_STR, |
| 185 | + ), |
| 186 | + ) |
0 commit comments