22import threading
33import time
44
5- from durabletask import worker
5+ from durabletask . worker import ConcurrencyOptions , TaskHubGrpcWorker
66
77
88class DummyStub :
@@ -15,6 +15,7 @@ def CompleteOrchestratorTask(self, res):
1515 def CompleteActivityTask (self , res ):
1616 self .completed .append (('activity' , res ))
1717
18+
1819class DummyRequest :
1920 def __init__ (self , kind , instance_id ):
2021 self .kind = kind
@@ -33,16 +34,18 @@ def HasField(self, field):
3334 def WhichOneof (self , _ ):
3435 return f'{ self .kind } Request'
3536
37+
3638class DummyCompletionToken :
3739 pass
3840
41+
3942def test_worker_concurrency_loop_sync ():
40- options = worker . ConcurrencyOptions (
43+ options = ConcurrencyOptions (
4144 maximum_concurrent_activity_work_items = 2 ,
4245 maximum_concurrent_orchestration_work_items = 1 ,
4346 maximum_thread_pool_workers = 2 ,
4447 )
45- grpc_worker = worker . TaskHubGrpcWorker (concurrency_options = options )
48+ worker = TaskHubGrpcWorker (concurrency_options = options )
4649 stub = DummyStub ()
4750
4851 def dummy_orchestrator (req , stub , completionToken ):
@@ -54,39 +57,42 @@ def dummy_activity(req, stub, completionToken):
5457 stub .CompleteActivityTask ('ok' )
5558
5659 # Patch the worker's _execute_orchestrator and _execute_activity
57- grpc_worker ._execute_orchestrator = dummy_orchestrator
58- grpc_worker ._execute_activity = dummy_activity
60+ worker ._execute_orchestrator = dummy_orchestrator
61+ worker ._execute_activity = dummy_activity
5962
6063 orchestrator_requests = [DummyRequest ('orchestrator' , f'orch{ i } ' ) for i in range (3 )]
6164 activity_requests = [DummyRequest ('activity' , f'act{ i } ' ) for i in range (4 )]
6265
6366 async def run_test ():
6467 # Start the worker manager's run loop in the background
65- worker_task = asyncio .create_task (grpc_worker ._async_worker_manager .run ())
68+ worker_task = asyncio .create_task (worker ._async_worker_manager .run ())
6669 for req in orchestrator_requests :
67- grpc_worker ._async_worker_manager .submit_orchestration (dummy_orchestrator , req , stub , DummyCompletionToken ())
70+ worker ._async_worker_manager .submit_orchestration (dummy_orchestrator , req , stub , DummyCompletionToken ())
6871 for req in activity_requests :
69- grpc_worker ._async_worker_manager .submit_activity (dummy_activity , req , stub , DummyCompletionToken ())
72+ worker ._async_worker_manager .submit_activity (dummy_activity , req , stub , DummyCompletionToken ())
7073 await asyncio .sleep (1.0 )
7174 orchestrator_count = sum (1 for t , _ in stub .completed if t == 'orchestrator' )
7275 activity_count = sum (1 for t , _ in stub .completed if t == 'activity' )
7376 assert orchestrator_count == 3 , f"Expected 3 orchestrator completions, got { orchestrator_count } "
7477 assert activity_count == 4 , f"Expected 4 activity completions, got { activity_count } "
75- grpc_worker ._async_worker_manager ._shutdown = True
78+ worker ._async_worker_manager ._shutdown = True
7679 await worker_task
7780 asyncio .run (run_test ())
7881
82+
7983# Dummy orchestrator and activity for sync context
8084def dummy_orchestrator (ctx , input ):
8185 # Simulate some work
8286 time .sleep (0.1 )
8387 return "orchestrator-done"
8488
89+
8590def dummy_activity (ctx , input ):
8691 # Simulate some work
8792 time .sleep (0.1 )
8893 return "activity-done"
8994
95+
9096def test_worker_concurrency_sync ():
9197 # Use small concurrency to make test observable
9298 options = ConcurrencyOptions (
@@ -124,16 +130,11 @@ def run_manager():
124130 t = threading .Thread (target = run_manager )
125131 t .start ()
126132 time .sleep (1.5 ) # Let work process
127-
128- # Signal shutdown but don't actually call shutdown() yet
129- manager ._shutdown = True
133+ manager .shutdown ()
130134 # Unblock the consumers by putting dummy items in the queues
131135 manager .activity_queue .put_nowait ((lambda : None , (), {}))
132136 manager .orchestration_queue .put_nowait ((lambda : None , (), {}))
133137 t .join (timeout = 2 )
134138
135- # Now shutdown the thread pool
136- manager .thread_pool .shutdown (wait = True )
137-
138139 # Check that all work items completed
139- assert len (results ) == 10
140+ assert len (results ) == 10
0 commit comments