Skip to content

Commit a2f05b0

Browse files
committed
Fixed flaky test
1 parent f323fac commit a2f05b0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/DotNext.Tests/Test.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ private protected static ReadOnlySequence<T> ToReadOnlySequence<T>(ReadOnlyMemor
4545
private protected static Action<T> Same<T>(T expected)
4646
where T : class
4747
=> actual => Same(expected, actual);
48+
49+
protected static TResult SuspendContext<TResult>(Func<TResult> func)
50+
{
51+
var currentContext = SynchronizationContext.Current;
52+
SynchronizationContext.SetSynchronizationContext(null);
53+
try
54+
{
55+
return func();
56+
}
57+
finally
58+
{
59+
SynchronizationContext.SetSynchronizationContext(currentContext);
60+
}
61+
}
4862
}

src/DotNext.Tests/Threading/Leases/LeaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static async Task WorkerProtectedWithLease()
132132
await using var consumer = new TestLeaseConsumer(provider);
133133
True(await consumer.TryAcquireAsync());
134134

135-
var result = await consumer.ExecuteAsync(Worker);
135+
var result = await SuspendContext(() => consumer.ExecuteAsync(Worker));
136136
Equal(42, result);
137137

138138
static async Task<int> Worker(CancellationToken token)

0 commit comments

Comments
 (0)