From 1f66c155e8070ac0cd4687f5e98b032070ba7346 Mon Sep 17 00:00:00 2001 From: Marius Thesing Date: Fri, 17 May 2024 15:56:09 +0200 Subject: [PATCH 1/2] fix flaky Sftp_BeginUploadFile test this test can randomly fail because it assumes that the callback has been called when the AsyncWaitHandle was set. But this is not necessarily the case because AsyncResult.SetAsCompleted does it the other way around. example: https://ci.appveyor.com/project/drieseng/ssh-net/builds/49831002/job/1237d4lg46j22pf0 --- test/Renci.SshNet.IntegrationTests/SftpTests.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/Renci.SshNet.IntegrationTests/SftpTests.cs b/test/Renci.SshNet.IntegrationTests/SftpTests.cs index 87b59c7a8..fd242c364 100644 --- a/test/Renci.SshNet.IntegrationTests/SftpTests.cs +++ b/test/Renci.SshNet.IntegrationTests/SftpTests.cs @@ -132,8 +132,13 @@ public void Sftp_BeginUploadFile() using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(content))) { IAsyncResult asyncResultCallback = null; + var callbackCalled = new ManualResetEvent(false); - var asyncResult = client.BeginUploadFile(memoryStream, remoteFile, ar => asyncResultCallback = ar); + var asyncResult = client.BeginUploadFile(memoryStream, remoteFile, ar => + { + asyncResultCallback = ar; + callbackCalled.Set(); + }); Assert.IsTrue(asyncResult.AsyncWaitHandle.WaitOne(10000)); @@ -145,6 +150,8 @@ public void Sftp_BeginUploadFile() Assert.IsFalse(sftpUploadAsyncResult.CompletedSynchronously); Assert.AreEqual(expectedByteCount, sftpUploadAsyncResult.UploadedBytes); + Assert.IsTrue(callbackCalled.WaitOne(10000)); + // check async result callback var sftpUploadAsyncResultCallback = asyncResultCallback as SftpUploadAsyncResult; Assert.IsNotNull(sftpUploadAsyncResultCallback); From 70d03fd4aef586e2c41909c501e656ff66822120 Mon Sep 17 00:00:00 2001 From: Marius Thesing Date: Fri, 17 May 2024 22:38:21 +0200 Subject: [PATCH 2/2] use ManualResetEventSlim --- test/Renci.SshNet.IntegrationTests/SftpTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Renci.SshNet.IntegrationTests/SftpTests.cs b/test/Renci.SshNet.IntegrationTests/SftpTests.cs index fd242c364..5ebddad7c 100644 --- a/test/Renci.SshNet.IntegrationTests/SftpTests.cs +++ b/test/Renci.SshNet.IntegrationTests/SftpTests.cs @@ -132,7 +132,7 @@ public void Sftp_BeginUploadFile() using (var memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(content))) { IAsyncResult asyncResultCallback = null; - var callbackCalled = new ManualResetEvent(false); + using var callbackCalled = new ManualResetEventSlim(false); var asyncResult = client.BeginUploadFile(memoryStream, remoteFile, ar => { @@ -150,7 +150,7 @@ public void Sftp_BeginUploadFile() Assert.IsFalse(sftpUploadAsyncResult.CompletedSynchronously); Assert.AreEqual(expectedByteCount, sftpUploadAsyncResult.UploadedBytes); - Assert.IsTrue(callbackCalled.WaitOne(10000)); + Assert.IsTrue(callbackCalled.Wait(10000)); // check async result callback var sftpUploadAsyncResultCallback = asyncResultCallback as SftpUploadAsyncResult;