Skip to content

Commit 8918a31

Browse files
authored
Properly restore auto-time-skipping after disabled (#475)
Fixes #471
1 parent b94084b commit 8918a31

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Temporalio/Testing/WorkflowEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public async override Task<DateTime> GetCurrentTimeAsync()
322322
/// <inheritdoc/>
323323
public override async Task<T> WithAutoTimeSkippingDisabledAsync<T>(Func<Task<T>> func)
324324
{
325-
var alreadyDisabled = autoTimeSkipping;
325+
var alreadyDisabled = !autoTimeSkipping;
326326
autoTimeSkipping = false;
327327
try
328328
{

tests/Temporalio.Tests/Testing/WorkflowEnvironmentTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,48 @@ await env.Client.ExecuteWorkflowAsync(
199199
});
200200
}
201201

202+
[Workflow]
203+
public class LongSleepWorkflow
204+
{
205+
[WorkflowRun]
206+
public async Task<string> RunAsync()
207+
{
208+
await Workflow.DelayAsync(TimeSpan.FromSeconds(30));
209+
return "all done";
210+
}
211+
212+
[WorkflowSignal]
213+
public async Task SomeSignalAsync()
214+
{
215+
}
216+
}
217+
218+
[OnlyIntelFact]
219+
public async Task StartTimeSkippingAsync_AutoTimeSkippingDisabled_RestoresAfterCall()
220+
{
221+
await using var env = await WorkflowEnvironment.StartTimeSkippingAsync();
222+
using var worker = new TemporalWorker(
223+
env.Client,
224+
new TemporalWorkerOptions($"tq-{Guid.NewGuid()}").AddWorkflow<LongSleepWorkflow>());
225+
await worker.ExecuteAsync(async () =>
226+
{
227+
var watch = Stopwatch.StartNew();
228+
var handle = await env.Client.StartWorkflowAsync(
229+
(LongSleepWorkflow wf) => wf.RunAsync(),
230+
new(id: $"workflow-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!));
231+
232+
// Do a signal and confirm that it starts skipping again after the signal. This used to
233+
// not restore auto-skipping when we didn't properly restore behavior.
234+
await env.WithAutoTimeSkippingDisabledAsync(async () =>
235+
{
236+
await handle.SignalAsync(wf => wf.SomeSignalAsync());
237+
});
238+
// Wait for result
239+
await handle.GetResultAsync();
240+
Assert.True(watch.Elapsed < TimeSpan.FromSeconds(5));
241+
});
242+
}
243+
202244
[Fact]
203245
public async Task StartLocal_SearchAttributes_ProperlyRegistered()
204246
{

0 commit comments

Comments
 (0)