public void Getting_The_Stream_Should_Lock_File()
        {
            //make the stream big enough so it won't be auto-closed
            //on the server side
            byte[] buffer = new byte[1024 * 1024 * 200];
            File.WriteAllBytes(Context.DownloadFile0Template.FullName, buffer);
            Context.DownloadFile0Template.Refresh();
            FileSystem.WriteFile(Context.DownloadFile0Template.FullName, SourceFileInfo.FullName, true);

            var token = InitToken();

            using (Stream stream = Downloads.DownloadFile(token.TransferId))
            {
                //certain providers may already unlock the file - in this case,
                //we won't be able to complete this test
                var status = Downloads.GetTransferStatus(token.TransferId);
                if (status == TransferStatus.Completed)
                {
                    return;
                }

                try
                {
                    //if we request to delete the file, this should fail
                    SourceFile.Delete();
                    Assert.Fail("Could delete file although it should be locked.");
                }
                catch (ResourceLockedException e)
                {
                }
            }
        }
        private void PauseAndVerifyStatusIsPaused()
        {
            Downloads.PauseTransfer(Token.TransferId);
            var status = Downloads.GetTransferStatus(Token.TransferId);

            Assert.AreEqual(TransferStatus.Paused, status);
        }
        public void Resuming_Reading_Should_Switch_Status_Back_To_Running()
        {
            Downloads.ReadBlock(Token.TransferId, 0);
            PauseAndVerifyStatusIsPaused();

            //read next block
            Downloads.ReadBlock(Token.TransferId, 1);
            var status = Downloads.GetTransferStatus(Token.TransferId);

            Assert.AreEqual(TransferStatus.Running, status);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Cancels the current download, if the <see cref="Token"/>
        /// is set, and resets the <see cref="SystemTime"/>.
        /// </summary>
        protected override void CleanupInternal()
        {
            if (Token != null)
            {
                if (Downloads.GetTransferStatus(Token.TransferId) != TransferStatus.UnknownTransfer)
                {
                    Downloads.CancelTransfer(Token.TransferId, AbortReason.Undefined);
                }
            }

            SystemTime.Reset();
            base.CleanupInternal();
        }
        public void Aborted_Transfer_Should_Cause_Exception_While_Reading_File_Even_If_File_Is_Streamed_At_Once()
        {
            //behind the scenes, many blocks are read, independent of the buffer size. Accordingly,
            //the transfer will abort as soon as we want it

            var token = InitToken();

            using (Stream stream = Downloads.DownloadFile(token.TransferId))
            {
                byte[] buffer = new byte[1234];
                stream.Read(buffer, 0, buffer.Length);
                int r = stream.Read(buffer, 1000, 234);
                Assert.AreEqual(234, r);

                //certain providers may already unlock the file - in this case,
                //we won't be able to complete this test
                var status = Downloads.GetTransferStatus(token.TransferId);
                if (status == TransferStatus.Completed)
                {
                    return;
                }

                //abort the transfer
                Downloads.CancelTransfer(token.TransferId, AbortReason.ClientAbort);

                //try to read again
                try
                {
                    var data = stream.ReadIntoBuffer();
                    Assert.Fail("Expected exception when attempting to read data after cancelling transfer.");
                }
                catch (TransferStatusException expected)
                {
                }
                //stream.Read(buffer, 0, buffer.Length);
            }
        }
 public OperationResult <Wrapped <TransferStatus> > Get(string transferId)
 {
     return(SecureFunc(() => new Wrapped <TransferStatus>(Downloads.GetTransferStatus(transferId))));
 }
 public void Status_Should_Have_Been_Adjusted_After_Reading_First_Block()
 {
     Assert.AreEqual(TransferStatus.Starting, Downloads.GetTransferStatus(Token.TransferId));
     Downloads.ReadBlock(Token.TransferId, 0);
     Assert.AreEqual(TransferStatus.Running, Downloads.GetTransferStatus(Token.TransferId));
 }