@@ -283,3 +283,42 @@ def pop_trace(name: str, filter: Optional[Callable[[Any], bool]] = None) -> Any:
283283
284284 # Confirm no unexpected traces
285285 assert not interceptor_traces
286+
287+
288+ class WorkflowInstanceAccessInterceptor (Interceptor ):
289+ def workflow_interceptor_class (
290+ self , input : WorkflowInterceptorClassInput
291+ ) -> Optional [Type [WorkflowInboundInterceptor ]]:
292+ return WorkflowInstanceAccessInboundInterceptor
293+
294+
295+ class WorkflowInstanceAccessInboundInterceptor (WorkflowInboundInterceptor ):
296+ async def execute_workflow (self , input : ExecuteWorkflowInput ) -> int :
297+ # Return integer difference between ids of workflow instance obtained from workflow run method and
298+ # from workflow.instance(). They should be the same, so the difference should be 0.
299+ id_from_workflow_run_method = await super ().execute_workflow (input )
300+ id_from_workflow_instance_api = id (workflow .instance ())
301+ return id_from_workflow_run_method - id_from_workflow_instance_api
302+
303+
304+ @workflow .defn
305+ class WorkflowInstanceAccessWorkflow :
306+ @workflow .run
307+ async def run (self ) -> int :
308+ return id (self )
309+
310+
311+ async def test_workflow_instance_access_from_interceptor (client : Client ):
312+ task_queue = f"task_queue_{ uuid .uuid4 ()} "
313+ async with Worker (
314+ client ,
315+ task_queue = task_queue ,
316+ workflows = [WorkflowInstanceAccessWorkflow ],
317+ interceptors = [WorkflowInstanceAccessInterceptor ()],
318+ ):
319+ difference = await client .execute_workflow (
320+ WorkflowInstanceAccessWorkflow .run ,
321+ id = f"workflow_{ uuid .uuid4 ()} " ,
322+ task_queue = task_queue ,
323+ )
324+ assert difference == 0
0 commit comments