Skip to content

Commit 21dd66c

Browse files
committed
Update test as PipeStream.Flush is no longer invoked after every write, and as such PipeStream.ReadByte is blocking (unless stream is closed).
PipeStream.ReadByte returning -1 is now considered an error (stream closed).
1 parent 240e742 commit 21dd66c

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/Renci.SshNet.Tests/Classes/ScpClientTest_Upload_FileInfoAndPath_Success.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public void Cleanup()
4545
}
4646
}
4747

48-
protected void Arrange()
48+
private void SetupData()
4949
{
5050
var random = new Random();
51+
5152
_bufferSize = random.Next(5, 15);
5253
_fileSize = _bufferSize + 2; //force uploading 2 chunks
5354
_fileContent = CreateContent(_fileSize);
@@ -56,12 +57,18 @@ protected void Arrange()
5657
_fileInfo = new FileInfo(_fileName);
5758
_path = random.Next().ToString(CultureInfo.InvariantCulture);
5859
_uploadingRegister = new List<ScpUploadEventArgs>();
60+
}
5961

62+
private void CreateMocks()
63+
{
6064
_serviceFactoryMock = new Mock<IServiceFactory>(MockBehavior.Strict);
6165
_sessionMock = new Mock<ISession>(MockBehavior.Strict);
6266
_channelSessionMock = new Mock<IChannelSession>(MockBehavior.Strict);
6367
_pipeStreamMock = new Mock<PipeStream>(MockBehavior.Strict);
68+
}
6469

70+
private void SetupMocks()
71+
{
6572
var sequence = new MockSequence();
6673
_serviceFactoryMock.InSequence(sequence)
6774
.Setup(p => p.CreateSession(_connectionInfo))
@@ -73,22 +80,16 @@ protected void Arrange()
7380
_channelSessionMock.InSequence(sequence)
7481
.Setup(
7582
p => p.SendExecRequest(string.Format("scp -t \"{0}\"", _path))).Returns(true);
76-
for (var i = 0; i < random.Next(1, 3); i++)
77-
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(-1);
7883
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
7984
_channelSessionMock.InSequence(sequence).Setup(p => p.SendData(It.IsAny<byte[]>()));
80-
for (var i = 0; i < random.Next(1, 3); i++)
81-
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(-1);
8285
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
8386
_channelSessionMock.InSequence(sequence)
8487
.Setup(p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(CreateData(
85-
string.Format("C0644 {0} {1}\n",
86-
_fileInfo.Length,
87-
Path.GetFileName(_fileName)
88-
)
89-
)))));
90-
for (var i = 0; i < random.Next(1, 3); i++)
91-
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(-1);
88+
string.Format("C0644 {0} {1}\n",
89+
_fileInfo.Length,
90+
Path.GetFileName(_fileName)
91+
)
92+
)))));
9293
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
9394
_channelSessionMock.InSequence(sequence)
9495
.Setup(
@@ -98,13 +99,18 @@ protected void Arrange()
9899
p => p.SendData(It.Is<byte[]>(b => b.Take(0, _fileContent.Length - _bufferSize).SequenceEqual(_fileContent.Take(_bufferSize, _fileContent.Length - _bufferSize))), 0, _fileContent.Length - _bufferSize));
99100
_channelSessionMock.InSequence(sequence)
100101
.Setup(
101-
p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(new byte[] { 0 }))));
102-
for (var i = 0; i < random.Next(1, 3); i++)
103-
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(-1);
102+
p => p.SendData(It.Is<byte[]>(b => b.SequenceEqual(new byte[] {0}))));
104103
_pipeStreamMock.InSequence(sequence).Setup(p => p.ReadByte()).Returns(0);
105104
_channelSessionMock.InSequence(sequence).Setup(p => p.Close());
106105
_channelSessionMock.InSequence(sequence).Setup(p => p.Dispose());
107106
_pipeStreamMock.As<IDisposable>().InSequence(sequence).Setup(p => p.Dispose());
107+
}
108+
109+
protected void Arrange()
110+
{
111+
SetupData();
112+
CreateMocks();
113+
SetupMocks();
108114

109115
_scpClient = new ScpClient(_connectionInfo, false, _serviceFactoryMock.Object)
110116
{
@@ -161,12 +167,12 @@ public void UploadingShouldHaveFiredTwice()
161167
Assert.AreEqual(_fileSize, uploading.Uploaded);
162168
}
163169

164-
private IEnumerable<byte> CreateData(string command)
170+
private static IEnumerable<byte> CreateData(string command)
165171
{
166172
return Encoding.Default.GetBytes(command);
167173
}
168174

169-
private byte[] CreateContent(int length)
175+
private static byte[] CreateContent(int length)
170176
{
171177
var random = new Random();
172178
var content = new byte[length];
@@ -176,7 +182,7 @@ private byte[] CreateContent(int length)
176182
return content;
177183
}
178184

179-
private string CreateTemporaryFile(byte[] content)
185+
private static string CreateTemporaryFile(byte[] content)
180186
{
181187
var tempFile = Path.GetTempFileName();
182188
using (var fs = File.OpenWrite(tempFile))

0 commit comments

Comments
 (0)