Skip to content

Commit

Permalink
Detached cancellation test (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
cretz authored Oct 8, 2024
1 parent 5141c2c commit d505adc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,3 @@ jobs:
dotnet-repo-path: ${{github.event.pull_request.head.repo.full_name}}
version: ${{github.event.pull_request.head.ref}}
version-is-repo-ref: true
features-repo-ref: "dotnet-after-1.3.0"
84 changes: 84 additions & 0 deletions tests/Temporalio.Tests/Worker/WorkflowWorkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5792,6 +5792,90 @@ await ExecuteWorkerAsync<NullWithCodecWorkflow>(
client);
}

[Workflow]
public class DetachedCancellationWorkflow
{
public class Activities
{
public TaskCompletionSource WaitingForCancel { get; } = new();

public bool CleanupCalled { get; set; }

[Activity]
public async Task WaitForCancel()
{
WaitingForCancel.SetResult();
await Task.Delay(
Timeout.Infinite,
ActivityExecutionContext.Current.CancellationToken);
}

[Activity]
public void Cleanup() => CleanupCalled = true;
}

[WorkflowRun]
public async Task RunAsync()
{
// Wait forever for cancellation, then cleanup
try
{
await Workflow.ExecuteActivityAsync(
(Activities acts) => acts.WaitForCancel(),
new() { StartToCloseTimeout = TimeSpan.FromMinutes(10) });
}
catch (Exception e) when (TemporalException.IsCanceledException(e))
{
// Run cleanup with another token
using var detachedCancelSource = new CancellationTokenSource();
await Workflow.ExecuteActivityAsync(
(Activities acts) => acts.Cleanup(),
new()
{
StartToCloseTimeout = TimeSpan.FromMinutes(10),
CancellationToken = detachedCancelSource.Token,
});
// Rethrow
throw;
}
}
}

[Fact]
public async Task ExecuteWorkflowAsync_DetachedCancellation_WorksProperly()
{
var activities = new DetachedCancellationWorkflow.Activities();
await ExecuteWorkerAsync<DetachedCancellationWorkflow>(
async worker =>
{
// Start workflow
var handle = await Client.StartWorkflowAsync(
(DetachedCancellationWorkflow wf) => wf.RunAsync(),
new(id: $"workflow-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!));

// Wait until waiting for cancel
await activities.WaitingForCancel.Task;

// Send workflow cancel
await handle.CancelAsync();

// Confirm canceled
var exc = await Assert.ThrowsAsync<WorkflowFailedException>(
() => handle.GetResultAsync());
Assert.IsType<CanceledFailureException>(exc.InnerException);

// Confirm cleanup called
Assert.True(activities.CleanupCalled);

// Run through replayer to confirm deterministic on replay
var history = await handle.FetchHistoryAsync();
var replayer = new WorkflowReplayer(
new WorkflowReplayerOptions().AddWorkflow<DetachedCancellationWorkflow>());
await replayer.ReplayWorkflowAsync(history);
},
new TemporalWorkerOptions().AddAllActivities(activities));
}

internal static Task AssertTaskFailureContainsEventuallyAsync(
WorkflowHandle handle, string messageContains)
{
Expand Down

0 comments on commit d505adc

Please sign in to comment.