Skip to content

Commit 62d6d37

Browse files
committed
Removed synchronous wait to avoid xunit deadlocks
1 parent f7a320a commit 62d6d37

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

src/DotNext.Tests/IO/AsyncBinaryReaderWriterTests.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,7 @@ private sealed class PipeSource : IAsyncBinaryReaderWriterSource, IFlushable
102102
public Task FlushAsync(CancellationToken token)
103103
=> pipe.Writer.FlushAsync(token).AsTask();
104104

105-
void IFlushable.Flush()
106-
{
107-
using var timeoutSource = new CancellationTokenSource(DefaultTimeout);
108-
using var task = FlushAsync(timeoutSource.Token);
109-
task.Wait();
110-
}
105+
void IFlushable.Flush() => throw new NotSupportedException();
111106

112107
public async ValueTask DisposeAsync()
113108
{
@@ -154,13 +149,7 @@ public FileSource(int bufferSize)
154149

155150
public Task FlushAsync(CancellationToken token) => writer.WriteAsync(token).AsTask();
156151

157-
void IFlushable.Flush()
158-
{
159-
using var cts = new CancellationTokenSource();
160-
using var task = FlushAsync(cts.Token);
161-
cts.CancelAfter(DefaultTimeout);
162-
task.Wait();
163-
}
152+
void IFlushable.Flush() => throw new NotSupportedException();
164153

165154
protected override void Dispose(bool disposing)
166155
{

src/DotNext.Tests/Threading/Tasks/SynchronizationTests.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -184,34 +184,40 @@ public static async Task WhenAllWithResult5()
184184
public static void SynchronousWait()
185185
{
186186
var cts = new TaskCompletionSource();
187-
var task = new ValueTask(cts.Task);
188-
ThreadPool.UnsafeQueueUserWorkItem(static cts =>
187+
var thread = new Thread(() =>
189188
{
190-
Thread.Sleep(100);
191-
cts.SetResult();
192-
}, cts, preferLocal: false);
193-
task.Wait();
189+
var task = new ValueTask(cts.Task);
190+
task.Wait();
191+
192+
// ensure that the current thread in not interrupted
193+
Thread.Sleep(0);
194+
});
194195

195-
// ensure that the current thread in not interrupted
196-
Thread.Sleep(0);
196+
thread.Start();
197+
Thread.Sleep(100);
198+
199+
cts.TrySetResult();
200+
thread.Join();
197201
}
198-
202+
199203
[Fact]
200204
public static void SynchronousWaitWithResult()
201205
{
202-
var cts = new TaskCompletionSource<int>();
203-
var task = new ValueTask<int>(cts.Task);
204-
205206
const int expected = 42;
206-
ThreadPool.UnsafeQueueUserWorkItem(static cts =>
207+
var cts = new TaskCompletionSource<int>();
208+
var thread = new Thread(() =>
207209
{
208-
Thread.Sleep(100);
209-
cts.SetResult(expected);
210-
}, cts, preferLocal: false);
211-
212-
Equal(expected, task.Wait());
213-
214-
// ensure that the current thread in not interrupted
215-
Thread.Sleep(0);
210+
var task = new ValueTask<int>(cts.Task);
211+
Equal(expected, task.Wait());
212+
213+
// ensure that the current thread in not interrupted
214+
Thread.Sleep(0);
215+
});
216+
217+
thread.Start();
218+
Thread.Sleep(100);
219+
220+
cts.TrySetResult(42);
221+
thread.Join();
216222
}
217223
}

0 commit comments

Comments
 (0)