@@ -93,18 +93,24 @@ public async Task TemporalWorkerService_ExecuteAsync_SimpleWorker()
9393
9494 // Start the host
9595 using var tokenSource = new CancellationTokenSource ( ) ;
96- var hostTask = Task . Run ( ( ) => host . RunAsync ( tokenSource . Token ) ) ;
97-
98- // Execute the workflow
99- var result = await Client . ExecuteWorkflowAsync (
100- ( DatabaseWorkflow wf ) => wf . RunAsync ( ) ,
101- new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
102- // Single activity calls use the same client but different calls use different clients
103- Assert . Equal (
104- new List < string > { "something-6" , "something-7" , "something-6" , "something-7" , "something-static" } ,
105- result ) ;
106- // Confirm the log appeared
107- Assert . Contains ( loggerFactory . Logs , e => e . Formatted == "Running database workflow" ) ;
96+ await host . StartAsync ( tokenSource . Token ) ;
97+ try
98+ {
99+ // Execute the workflow
100+ var result = await Client . ExecuteWorkflowAsync (
101+ ( DatabaseWorkflow wf ) => wf . RunAsync ( ) ,
102+ new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
103+ // Single activity calls use the same client but different calls use different clients
104+ Assert . Equal (
105+ new List < string > { "something-6" , "something-7" , "something-6" , "something-7" , "something-static" } ,
106+ result ) ;
107+ // Confirm the log appeared
108+ Assert . Contains ( loggerFactory . Logs , e => e . Formatted == "Running database workflow" ) ;
109+ }
110+ finally
111+ {
112+ await host . StopAsync ( tokenSource . Token ) ;
113+ }
108114 }
109115
110116 public class SingletonCounter
@@ -186,27 +192,33 @@ public async Task TemporalWorkerService_ExecuteAsync_MultipleWorkers()
186192 // Start the host
187193 using var tokenSource = new CancellationTokenSource ( ) ;
188194 using var host = bld . Build ( ) ;
189- var hostTask = Task . Run ( ( ) => host . RunAsync ( tokenSource . Token ) ) ;
190-
191- // Execute the workflow
192- var result = await Client . ExecuteWorkflowAsync (
193- ( MultiTaskQueueWorkflow wf ) => wf . RunAsync ( taskQueue2 ) ,
194- new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue1 ) ) ;
195- Assert . Equal (
196- new Dictionary < string , string > ( )
197- {
198- // Singletons share
199- [ "singleton1" ] = $ "tq: { taskQueue1 } , counter: 6",
200- [ "singleton2" ] = $ "tq: { taskQueue1 } , counter: 7",
201- [ "singleton-other1" ] = $ "tq: { taskQueue2 } , counter: 8",
202- [ "singleton-other2" ] = $ "tq: { taskQueue2 } , counter: 9",
203- // Scoped do not share
204- [ "scoped1" ] = $ "tq: { taskQueue1 } , counter: 6",
205- [ "scoped2" ] = $ "tq: { taskQueue1 } , counter: 6",
206- [ "scoped-other1" ] = $ "tq: { taskQueue2 } , counter: 6",
207- [ "scoped-other2" ] = $ "tq: { taskQueue2 } , counter: 6",
208- } ,
209- result ) ;
195+ await host . StartAsync ( tokenSource . Token ) ;
196+ try
197+ {
198+ // Execute the workflow
199+ var result = await Client . ExecuteWorkflowAsync (
200+ ( MultiTaskQueueWorkflow wf ) => wf . RunAsync ( taskQueue2 ) ,
201+ new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue1 ) ) ;
202+ Assert . Equal (
203+ new Dictionary < string , string > ( )
204+ {
205+ // Singletons share
206+ [ "singleton1" ] = $ "tq: { taskQueue1 } , counter: 6",
207+ [ "singleton2" ] = $ "tq: { taskQueue1 } , counter: 7",
208+ [ "singleton-other1" ] = $ "tq: { taskQueue2 } , counter: 8",
209+ [ "singleton-other2" ] = $ "tq: { taskQueue2 } , counter: 9",
210+ // Scoped do not share
211+ [ "scoped1" ] = $ "tq: { taskQueue1 } , counter: 6",
212+ [ "scoped2" ] = $ "tq: { taskQueue1 } , counter: 6",
213+ [ "scoped-other1" ] = $ "tq: { taskQueue2 } , counter: 6",
214+ [ "scoped-other2" ] = $ "tq: { taskQueue2 } , counter: 6",
215+ } ,
216+ result ) ;
217+ }
218+ finally
219+ {
220+ await host . StopAsync ( tokenSource . Token ) ;
221+ }
210222 }
211223
212224 [ Workflow ]
@@ -266,25 +278,31 @@ public async Task TemporalWorkerService_WorkerClientReplacement_UsesNewClient()
266278 // Start the host
267279 using var tokenSource = new CancellationTokenSource ( ) ;
268280 using var host = bld . Build ( ) ;
269- var hostTask = Task . Run ( ( ) => host . RunAsync ( tokenSource . Token ) ) ;
270-
271- // Confirm the first ticking workflow has completed a task but not the second workflow
272- await AssertMore . HasEventEventuallyAsync ( handle1 , e => e . WorkflowTaskCompletedEventAttributes != null ) ;
273- await foreach ( var evt in handle2 . FetchHistoryEventsAsync ( ) )
281+ await host . StartAsync ( tokenSource . Token ) ;
282+ try
274283 {
275- Assert . Null ( evt . WorkflowTaskCompletedEventAttributes ) ;
276- }
284+ // Confirm the first ticking workflow has completed a task but not the second workflow
285+ await AssertMore . HasEventEventuallyAsync ( handle1 , e => e . WorkflowTaskCompletedEventAttributes != null ) ;
286+ await foreach ( var evt in handle2 . FetchHistoryEventsAsync ( ) )
287+ {
288+ Assert . Null ( evt . WorkflowTaskCompletedEventAttributes ) ;
289+ }
277290
278- // Now replace the client, which should be used fairly quickly because we should have
279- // timer-done poll completions every 100ms
280- workerClientUpdater . UpdateClient ( otherEnv . Client ) ;
291+ // Now replace the client, which should be used fairly quickly because we should have
292+ // timer-done poll completions every 100ms
293+ workerClientUpdater . UpdateClient ( otherEnv . Client ) ;
281294
282- // Now confirm the other workflow has started
283- await AssertMore . HasEventEventuallyAsync ( handle2 , e => e . WorkflowTaskCompletedEventAttributes != null ) ;
295+ // Now confirm the other workflow has started
296+ await AssertMore . HasEventEventuallyAsync ( handle2 , e => e . WorkflowTaskCompletedEventAttributes != null ) ;
284297
285- // Terminate both
286- await handle1 . TerminateAsync ( ) ;
287- await handle2 . TerminateAsync ( ) ;
298+ // Terminate both
299+ await handle1 . TerminateAsync ( ) ;
300+ await handle2 . TerminateAsync ( ) ;
301+ }
302+ finally
303+ {
304+ await host . StopAsync ( tokenSource . Token ) ;
305+ }
288306 }
289307
290308 [ Workflow ( "DeploymentVersioningWorkflow" , VersioningBehavior = VersioningBehavior . Pinned ) ]
@@ -323,23 +341,29 @@ public async Task TemporalWorkerService_ExecuteAsync_MultipleVersionsSameQueue()
323341 // Start the host
324342 using var tokenSource = new CancellationTokenSource ( ) ;
325343 using var host = bld . Build ( ) ;
326- var hostTask = Task . Run ( ( ) => host . RunAsync ( tokenSource . Token ) ) ;
327-
328- // Set 1.0 as default and run
329- await Env . Client . UpdateWorkerBuildIdCompatibilityAsync (
330- taskQueue , new BuildIdOp . AddNewDefault ( "1.0" ) ) ;
331- var res = await Client . ExecuteWorkflowAsync (
332- ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
333- new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
334- Assert . Equal ( "done-v1" , res ) ;
335-
336- // Update default and run again
337- await Env . Client . UpdateWorkerBuildIdCompatibilityAsync (
338- taskQueue , new BuildIdOp . AddNewDefault ( "2.0" ) ) ;
339- res = await Client . ExecuteWorkflowAsync (
340- ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
341- new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
342- Assert . Equal ( "done-v2" , res ) ;
344+ await host . StartAsync ( tokenSource . Token ) ;
345+ try
346+ {
347+ // Set 1.0 as default and run
348+ await Env . Client . UpdateWorkerBuildIdCompatibilityAsync (
349+ taskQueue , new BuildIdOp . AddNewDefault ( "1.0" ) ) ;
350+ var res = await Client . ExecuteWorkflowAsync (
351+ ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
352+ new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
353+ Assert . Equal ( "done-v1" , res ) ;
354+
355+ // Update default and run again
356+ await Env . Client . UpdateWorkerBuildIdCompatibilityAsync (
357+ taskQueue , new BuildIdOp . AddNewDefault ( "2.0" ) ) ;
358+ res = await Client . ExecuteWorkflowAsync (
359+ ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
360+ new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
361+ Assert . Equal ( "done-v2" , res ) ;
362+ }
363+ finally
364+ {
365+ await host . StopAsync ( tokenSource . Token ) ;
366+ }
343367#pragma warning restore CS0618
344368 }
345369
@@ -362,23 +386,29 @@ public async Task TemporalWorkerService_ExecuteAsync_MultipleDeploymentVersionsS
362386 // Start the host
363387 using var tokenSource = new CancellationTokenSource ( ) ;
364388 using var host = bld . Build ( ) ;
365- var hostTask = Task . Run ( ( ) => host . RunAsync ( tokenSource . Token ) ) ;
366-
367- // Set 1.0 as default and run
368- var describe1 = await TestUtils . WaitUntilWorkerDeploymentVisibleAsync ( Client , workerV1 ) ;
369- await TestUtils . SetCurrentDeploymentVersionAsync ( Client , describe1 . ConflictToken , workerV1 ) ;
370- var res = await Client . ExecuteWorkflowAsync (
371- ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
372- new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
373- Assert . Equal ( "done-v1" , res ) ;
374-
375- // Update default and run again
376- var describe2 = await TestUtils . WaitUntilWorkerDeploymentVisibleAsync ( Client , workerV2 ) ;
377- await TestUtils . SetCurrentDeploymentVersionAsync ( Client , describe2 . ConflictToken , workerV2 ) ;
378- res = await Client . ExecuteWorkflowAsync (
379- ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
380- new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
381- Assert . Equal ( "done-v2" , res ) ;
389+ await host . StartAsync ( tokenSource . Token ) ;
390+ try
391+ {
392+ // Set 1.0 as default and run
393+ var describe1 = await TestUtils . WaitUntilWorkerDeploymentVisibleAsync ( Client , workerV1 ) ;
394+ await TestUtils . SetCurrentDeploymentVersionAsync ( Client , describe1 . ConflictToken , workerV1 ) ;
395+ var res = await Client . ExecuteWorkflowAsync (
396+ ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
397+ new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
398+ Assert . Equal ( "done-v1" , res ) ;
399+
400+ // Update default and run again
401+ var describe2 = await TestUtils . WaitUntilWorkerDeploymentVisibleAsync ( Client , workerV2 ) ;
402+ await TestUtils . SetCurrentDeploymentVersionAsync ( Client , describe2 . ConflictToken , workerV2 ) ;
403+ res = await Client . ExecuteWorkflowAsync (
404+ ( WorkflowV1 wf ) => wf . RunAsync ( ) ,
405+ new ( $ "wf-{ Guid . NewGuid ( ) } ", taskQueue ) ) ;
406+ Assert . Equal ( "done-v2" , res ) ;
407+ }
408+ finally
409+ {
410+ await host . StopAsync ( tokenSource . Token ) ;
411+ }
382412 }
383413
384414 [ Fact ]
0 commit comments