-
-
Notifications
You must be signed in to change notification settings - Fork 947
Add unit tests for task-based asynchronous API #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
7d5ccb8
POC unit tests
IgorMilavec 2a47c9e
Modify AppVeyor test script to only run net472 and net5.0
IgorMilavec ba7ecbb
Fix ScpClientTest_*.DisposeOnPipeStreamShouldBeInvokedOnce in modern …
IgorMilavec 81b3a16
Fix runtime and culture dependant tests. Should we test this at all?
IgorMilavec be97986
Make ForwardedPortDynamic exception visible
IgorMilavec f91c5f1
Fix PipeStream_Close_BlockingWrite synchronization
IgorMilavec 8c66267
Removed CountdownEventTest for FEATURE_THREAD_COUNTDOWNEVENT
IgorMilavec 5304296
Remove Thread.Abort()
IgorMilavec a7a9474
Fix another runtime dependant string
IgorMilavec de9faec
Raise AppVeyor number of ports to avoid dynamic port exhaustion
IgorMilavec 5bc16c8
Fix previous "speeedup" of ChannelForwardedTcpipTest_Dispose_SessionI…
IgorMilavec 36b751b
Speedup tests
IgorMilavec 362910f
Ignore SocketError.TryAgain when expecting SocketError.HostNotFound
IgorMilavec c1a657a
Better ObjectDisposedHandling in AsyncSocketListener
IgorMilavec bae6c54
Handle SocketError.ConnectionAborted in ForwardedPortDynamic
IgorMilavec 48e1af3
Rename test to match implementation
IgorMilavec f0a8f5f
Fix BaseClientTest_Connected_KeepAliveInterval_NotNegativeOne timing
IgorMilavec 382649b
Set C# 7.3 in Tests.csproj to limit intellisense's suggestions under …
IgorMilavec 07c1535
Add SftpClientTest.*Async
IgorMilavec 0030b35
Add SftpFileStreamTest_OpenAsync_*
IgorMilavec 2fbdb46
Add SftpFileStreamTest_WriteAsync_*
IgorMilavec 3547504
Add SftpFileStreamTest_ReadAsync_*
IgorMilavec 3506707
Remove unrelated changes
IgorMilavec 9f45e7f
Align AppVeyor script with Test project target frameworks
IgorMilavec a53f35b
Revert unrelated changes
IgorMilavec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,11 +54,11 @@ protected override void SetupMocks() | |
.Setup(p => p.SendExecRequest(string.Format("scp -prf {0}", _transformedPath))) | ||
.Returns(false); | ||
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose()); | ||
#if NET35 | ||
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose()); | ||
|
||
// On .NET Core, Dispose() in turn invokes Close() and since we're not mocking | ||
// an interface, we need to expect this call as well | ||
_pipeStreamMock.Setup(p => p.Close()); | ||
#else | ||
_pipeStreamMock.InSequence(sequence).Setup(p => p.Close()); | ||
#endif | ||
} | ||
|
||
protected override void Arrange() | ||
|
@@ -106,7 +106,11 @@ public void DisposeOnChannelShouldBeInvokedOnce() | |
[TestMethod] | ||
public void DisposeOnPipeStreamShouldBeInvokedOnce() | ||
{ | ||
#if NET35 | ||
_pipeStreamMock.As<IDisposable>().Verify(p => p.Dispose(), Times.Once); | ||
#else | ||
_pipeStreamMock.Verify(p => p.Close(), Times.Once); | ||
#endif | ||
Comment on lines
+109
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @IgorMilavec No need to move these changes (here and elsewhere) out of this PR. |
||
} | ||
|
||
[TestMethod] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamAsyncTestBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#if FEATURE_TAP | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Renci.SshNet.Sftp; | ||
|
||
namespace Renci.SshNet.Tests.Classes.Sftp | ||
{ | ||
public abstract class SftpFileStreamAsyncTestBase | ||
{ | ||
internal Mock<ISftpSession> SftpSessionMock; | ||
protected MockSequence MockSequence; | ||
|
||
protected virtual Task ArrangeAsync() | ||
{ | ||
SetupData(); | ||
CreateMocks(); | ||
SetupMocks(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
protected virtual void SetupData() | ||
{ | ||
MockSequence = new MockSequence(); | ||
} | ||
|
||
protected abstract void SetupMocks(); | ||
|
||
private void CreateMocks() | ||
{ | ||
SftpSessionMock = new Mock<ISftpSession>(MockBehavior.Strict); | ||
} | ||
|
||
[TestInitialize] | ||
public async Task SetUpAsync() | ||
{ | ||
await ArrangeAsync(); | ||
await ActAsync(); | ||
} | ||
|
||
protected abstract Task ActAsync(); | ||
|
||
protected byte[] GenerateRandom(int length) | ||
{ | ||
return GenerateRandom(length, new Random()); | ||
} | ||
|
||
protected byte[] GenerateRandom(int length, Random random) | ||
{ | ||
var buffer = new byte[length]; | ||
random.NextBytes(buffer); | ||
return buffer; | ||
} | ||
|
||
protected byte[] GenerateRandom(uint length) | ||
{ | ||
return GenerateRandom(length, new Random()); | ||
} | ||
|
||
protected byte[] GenerateRandom(uint length, Random random) | ||
{ | ||
var buffer = new byte[length]; | ||
random.NextBytes(buffer); | ||
return buffer; | ||
} | ||
} | ||
} | ||
#endif |
57 changes: 57 additions & 0 deletions
57
src/Renci.SshNet.Tests/Classes/Sftp/SftpFileStreamTest_OpenAsync_FileAccessInvalid.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#if FEATURE_TAP | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Renci.SshNet.Sftp; | ||
|
||
namespace Renci.SshNet.Tests.Classes.Sftp | ||
{ | ||
[TestClass] | ||
public class SftpFileStreamTest_OpenAsync_FileAccessInvalid : SftpFileStreamAsyncTestBase | ||
{ | ||
private Random _random; | ||
private string _path; | ||
private FileMode _fileMode; | ||
private FileAccess _fileAccess; | ||
private int _bufferSize; | ||
private ArgumentOutOfRangeException _actualException; | ||
|
||
protected override void SetupData() | ||
{ | ||
base.SetupData(); | ||
|
||
_random = new Random(); | ||
_path = _random.Next().ToString(); | ||
_fileMode = FileMode.Open; | ||
_fileAccess = 0; | ||
_bufferSize = _random.Next(5, 1000); | ||
} | ||
|
||
protected override void SetupMocks() | ||
{ | ||
} | ||
|
||
protected override async Task ActAsync() | ||
{ | ||
try | ||
{ | ||
await SftpFileStream.OpenAsync(SftpSessionMock.Object, _path, _fileMode, _fileAccess, _bufferSize, default); | ||
Assert.Fail(); | ||
} | ||
catch (ArgumentOutOfRangeException ex) | ||
{ | ||
_actualException = ex; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void CtorShouldHaveThrownArgumentException() | ||
{ | ||
Assert.IsNotNull(_actualException); | ||
Assert.IsNull(_actualException.InnerException); | ||
Assert.AreEqual("access", _actualException.ParamName); | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd first like to discuss dropping .NET 3.5 support.
Perhaps we'll have a final release supporting the legacy frameworks.
As I said, let's discuss this first.