diff --git a/src/Renci.SshNet/Abstractions/CancellationTokenSourceExtensions.cs b/src/Renci.SshNet/Abstractions/CancellationTokenSourceExtensions.cs new file mode 100644 index 00000000..93ad8e28 --- /dev/null +++ b/src/Renci.SshNet/Abstractions/CancellationTokenSourceExtensions.cs @@ -0,0 +1,16 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace Renci.SshNet.Abstractions +{ + internal static class CancellationTokenSourceExtensions + { +#if !NET8_OR_GREATER + public static Task CancelAsync(this CancellationTokenSource cancellationTokenSource) + { + cancellationTokenSource.Cancel(); + return Task.CompletedTask; + } +#endif + } +} diff --git a/src/Renci.SshNet/Abstractions/StreamExtensions.cs b/src/Renci.SshNet/Abstractions/StreamExtensions.cs new file mode 100644 index 00000000..cbc85c7d --- /dev/null +++ b/src/Renci.SshNet/Abstractions/StreamExtensions.cs @@ -0,0 +1,18 @@ +#if !NET && !NETSTANDARD2_1_OR_GREATER +using System.IO; +using System.Threading.Tasks; +#endif + +namespace Renci.SshNet.Abstractions +{ + internal static class StreamExtensions + { +#if !NET && !NETSTANDARD2_1_OR_GREATER + public static ValueTask DisposeAsync(this Stream stream) + { + stream.Dispose(); + return default; + } +#endif + } +} diff --git a/test/Renci.SshNet.IntegrationTests/OldIntegrationTests/SshCommandTest.cs b/test/Renci.SshNet.IntegrationTests/OldIntegrationTests/SshCommandTest.cs index fa7e8335..46b5e5dd 100644 --- a/test/Renci.SshNet.IntegrationTests/OldIntegrationTests/SshCommandTest.cs +++ b/test/Renci.SshNet.IntegrationTests/OldIntegrationTests/SshCommandTest.cs @@ -1,5 +1,8 @@ using System.Diagnostics; +#if !NET8_0_OR_GREATER +using Renci.SshNet.Abstractions; +#endif using Renci.SshNet.Common; namespace Renci.SshNet.IntegrationTests.OldIntegrationTests @@ -130,7 +133,7 @@ namespace Renci.SshNet.IntegrationTests.OldIntegrationTests Task executeTask = cmd.ExecuteAsync(cts.Token); - cts.Cancel(); + await cts.CancelAsync(); var tce = await Assert.ThrowsExceptionAsync(() => executeTask); Assert.AreSame(executeTask, tce.Task); diff --git a/test/Renci.SshNet.IntegrationTests/SshTests.cs b/test/Renci.SshNet.IntegrationTests/SshTests.cs index 2888e74e..762a6ba5 100644 --- a/test/Renci.SshNet.IntegrationTests/SshTests.cs +++ b/test/Renci.SshNet.IntegrationTests/SshTests.cs @@ -318,7 +318,7 @@ namespace Renci.SshNet.IntegrationTests { using (var cmd = sshClient.CreateCommand("chmod 777 " + remoteFile)) { - cmd.Execute(); + await cmd.ExecuteAsync(); Assert.AreEqual(0, cmd.ExitStatus, cmd.Error); } @@ -333,7 +333,7 @@ namespace Renci.SshNet.IntegrationTests { var lines = new List(); string line = null; - while ((line = reader.ReadLine()) != null) + while ((line = await reader.ReadLineAsync()) != null) { lines.Add(line); } diff --git a/test/Renci.SshNet.IntegrationTests/TestsFixtures/InfrastructureFixture.cs b/test/Renci.SshNet.IntegrationTests/TestsFixtures/InfrastructureFixture.cs index aaefb81e..3b598bf2 100644 --- a/test/Renci.SshNet.IntegrationTests/TestsFixtures/InfrastructureFixture.cs +++ b/test/Renci.SshNet.IntegrationTests/TestsFixtures/InfrastructureFixture.cs @@ -2,6 +2,10 @@ using DotNet.Testcontainers.Containers; using DotNet.Testcontainers.Images; +#if !NET && !NETSTANDARD2_1_OR_GREATER +using Renci.SshNet.Abstractions; +#endif + namespace Renci.SshNet.IntegrationTests.TestsFixtures { public sealed class InfrastructureFixture : IDisposable @@ -80,8 +84,8 @@ namespace Renci.SshNet.IntegrationTests.TestsFixtures await _sshServerImage.DisposeAsync(); } - _fsOut.Dispose(); - _fsErr.Dispose(); + await _fsOut.DisposeAsync(); + await _fsErr.DisposeAsync(); } public void Dispose() diff --git a/test/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs b/test/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs index 30685e98..dd84c898 100644 --- a/test/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs +++ b/test/Renci.SshNet.Tests/Classes/Common/PipeStreamTest.cs @@ -4,6 +4,9 @@ using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; +#if !NET && !NETSTANDARD2_1_OR_GREATER +using Renci.SshNet.Abstractions; +#endif using Renci.SshNet.Common; using Renci.SshNet.Tests.Common; @@ -93,7 +96,7 @@ namespace Renci.SshNet.Tests.Classes.Common Assert.IsFalse(readTask.IsCompleted); - pipeStream.Dispose(); + await pipeStream.DisposeAsync(); Assert.AreEqual(0, await readTask); } @@ -111,7 +114,7 @@ namespace Renci.SshNet.Tests.Classes.Common Assert.IsFalse(readTask.IsCompleted); - pipeStream.Dispose(); + await pipeStream.DisposeAsync(); Assert.AreEqual(0, await readTask); } @@ -130,7 +133,10 @@ namespace Renci.SshNet.Tests.Classes.Common Assert.IsFalse(readTask.IsCompleted); + // not using WriteAsync here because it deadlocks the test +#pragma warning disable S6966 // Awaitable method should be used pipeStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4); +#pragma warning restore S6966 // Awaitable method should be used Assert.AreEqual(0, await readTask); }