protected override void SetupMocks() { _seq = new MockSequence(); SftpSessionMock.InSequence(_seq) .Setup(p => p.CreateWaitHandleArray(It.IsNotNull <WaitHandle>(), It.IsNotNull <WaitHandle>())) .Returns <WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) => { _waitHandleArray[0] = disposingWaitHandle; _waitHandleArray[1] = semaphoreAvailableWaitHandle; return(_waitHandleArray); }); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk1, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Callback(() => _exceptionSignaled.Set()) .Throws(_exception); }
protected override void SetupMocks() { _seq = new MockSequence(); SftpSessionMock.InSequence(_seq) .Setup(p => p.CreateWaitHandleArray(It.IsNotNull <WaitHandle>(), It.IsNotNull <WaitHandle>())) .Returns <WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) => { _waitHandleArray[0] = disposingWaitHandle; _waitHandleArray[1] = semaphoreAvailableWaitHandle; return(_waitHandleArray); }); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk1, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { ThreadAbstraction.ExecuteThread(() => { // signal that we're in the read-ahead for chunk2 _readAheadChunk2.Set(); // wait for client to start reading this chunk _readChunk2.WaitOne(TimeSpan.FromSeconds(5)); // sleep a short time to make sure the client is in the blocking wait Thread.Sleep(500); // complete async read of chunk2 with exception var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_exception, false); }); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); }
protected override void SetupData() { var random = new Random(); _handle = CreateByteArray(random, 5); _chunk1 = CreateByteArray(random, ChunkLength); // chunk is less than the requested length, but - together with chunk 1 - contains all data up to the EOF _chunk2 = CreateByteArray(random, ChunkLength - 10); _chunk3 = new byte[0]; _fileSize = _chunk1.Length + _chunk2.Length; _waitHandleArray = new WaitHandle[2]; _operationTimeout = random.Next(10000, 20000); _closeAsyncResult = new SftpCloseAsyncResult(null, null); _readAsyncResultBeyondEof = new SftpReadAsyncResult(null, null); }
protected override void SetupMocks() { _seq = new MockSequence(); SftpSessionMock.InSequence(_seq) .Setup(p => p.CreateWaitHandleArray(It.IsNotNull <WaitHandle>(), It.IsNotNull <WaitHandle>())) .Returns <WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) => { _waitHandleArray[0] = disposingWaitHandle; _waitHandleArray[1] = semaphoreAvailableWaitHandle; return(_waitHandleArray); }); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { _chunk1BeginRead.Set(); var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk1, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { _chunk2BeginRead.Set(); var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk2, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { _chunk3BeginRead.Set(); var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk3, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 3 * ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { _chunk4BeginRead.Set(); var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk4, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 4 * ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { _chunk5BeginRead.Set(); var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk5, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Callback(() => _waitBeforeChunk6.Set()) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.RequestRead(_handle, 2 * ChunkLength - 17, 17)) .Returns(_chunk2CatchUp1); SftpSessionMock.InSequence(_seq) .Setup(p => p.RequestRead(_handle, 2 * ChunkLength - 7, 7)) .Returns(_chunk2CatchUp2); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 5 * ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { _chunk6BeginRead.Set(); var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk6, false); }) .Returns((SftpReadAsyncResult)null); }
protected override void SetupMocks() { _seq = new MockSequence(); SftpSessionMock.InSequence(_seq) .Setup(p => p.CreateWaitHandleArray(It.IsNotNull <WaitHandle>(), It.IsNotNull <WaitHandle>())) .Returns <WaitHandle, WaitHandle>((disposingWaitHandle, semaphoreAvailableWaitHandle) => { _waitHandleArray[0] = disposingWaitHandle; _waitHandleArray[1] = semaphoreAvailableWaitHandle; return(_waitHandleArray); }); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 0, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk1, false); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { ThreadAbstraction.ExecuteThread(() => { // wait until the read-ahead for chunk3 has completed; this should allow // the read-ahead of chunk4 to start _readAheadChunk3Completed.WaitOne(TimeSpan.FromSeconds(3)); // wait until the semaphore wait to start with chunk4 has started _waitingForSemaphoreAfterCompletingChunk3.WaitOne(TimeSpan.FromSeconds(7)); // complete async read of chunk2 with exception var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_exception, false); // signal that read-ahead of chunk 2 has completed _readAheadChunk2Completed.Set(); }); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); SftpSessionMock.InSequence(_seq) .Setup(p => p.BeginRead(_handle, 2 * ChunkLength, ChunkLength, It.IsNotNull <AsyncCallback>(), It.IsAny <BufferedRead>())) .Callback <byte[], ulong, uint, AsyncCallback, object>((handle, offset, length, callback, state) => { var asyncResult = new SftpReadAsyncResult(callback, state); asyncResult.SetAsCompleted(_chunk3, false); // signal that we've completed the read-ahead for chunk3 _readAheadChunk3Completed.Set(); }) .Returns((SftpReadAsyncResult)null); SftpSessionMock.InSequence(_seq).Setup(p => p.OperationTimeout).Returns(_operationTimeout); SftpSessionMock.InSequence(_seq) .Setup(p => p.WaitAny(_waitHandleArray, _operationTimeout)) .Callback(() => _waitingForSemaphoreAfterCompletingChunk3.Set()) .Returns(() => WaitAny(_waitHandleArray, _operationTimeout)); }