@@ -283,3 +283,44 @@ 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+ from_workflow_instance_api = workflow .instance ()
300+ assert from_workflow_instance_api is not None
301+ id_from_workflow_instance_api = id (from_workflow_instance_api )
302+ id_from_workflow_run_method = await super ().execute_workflow (input )
303+ return id_from_workflow_run_method - id_from_workflow_instance_api
304+
305+
306+ @workflow .defn
307+ class WorkflowInstanceAccessWorkflow :
308+ @workflow .run
309+ async def run (self ) -> int :
310+ return id (self )
311+
312+
313+ async def test_workflow_instance_access_from_interceptor (client : Client ):
314+ task_queue = f"task_queue_{ uuid .uuid4 ()} "
315+ async with Worker (
316+ client ,
317+ task_queue = task_queue ,
318+ workflows = [WorkflowInstanceAccessWorkflow ],
319+ interceptors = [WorkflowInstanceAccessInterceptor ()],
320+ ):
321+ difference = await client .execute_workflow (
322+ WorkflowInstanceAccessWorkflow .run ,
323+ id = f"workflow_{ uuid .uuid4 ()} " ,
324+ task_queue = task_queue ,
325+ )
326+ assert difference == 0
0 commit comments