Пример #1
0
        public void CanceledBeforeAndSourceWithoutRights()
        {
            var cts     = new CancellationTokenSource();
            var moveJob = new FileTransferJob(
                new FileTransferArguments(state.ExistentFileWithoutRights, state.NonExistingFiles[0], TransferSettings.DeleteOriginal),
                state.OnProgressDebugPrint
                ,
                cts.Token);

            cts.Cancel();

            var t = Task.Run(moveJob.Run, cts.Token);

            try
            {
                t.Wait();
            }
            catch (AggregateException ae)
            {
                state.ExceptionThrown = true;
                Assert.IsTrue(ae.InnerExceptions.All(e => e is OperationCanceledException));
            }

            Assert.AreEqual(t.Status, TaskStatus.Canceled);
            Assert.IsTrue(state.ExceptionThrown);

            state.ExistentFileWithoutRights.Refresh();
            state.NonExistingFiles[0].Refresh();

            Assert.IsTrue(state.ExistentFileWithoutRights.Exists);
            Assert.IsFalse(state.NonExistingFiles[0].Exists);
        }
Пример #2
0
        public void DestinationWithoutRights()
        {
            var cts     = new CancellationTokenSource();
            var moveJob = new FileTransferJob(
                new FileTransferArguments(state.ExistingFiles[0], state.NonExistentFileWithoutRights, TransferSettings.DeleteOriginal),
                state.OnProgressDebugPrint
                ,
                cts.Token);

            try
            {
                moveJob.Run();
            }
            catch (FileTransferException e)
            {
                state.ExceptionThrown = true;
                Assert.IsTrue(e.InnerException is UnauthorizedAccessException);
            }

            state.ExistingFiles[0].Refresh();
            state.NonExistentFileWithoutRights.Refresh();

            Assert.IsTrue(state.ExceptionThrown);
            Assert.IsTrue(state.ExistingFiles[0].Exists);
        }
Пример #3
0
        public void CanceledBefore()
        {
            bool threwException = false;

            var             ctSource = new CancellationTokenSource();
            FileTransferJob job      = new FileTransferJob(
                new FileTransferArguments(state.ExistingFiles[0], state.NonExistingFiles[0], TransferSettings.None),
                state.OnProgressDebugPrint
                ,
                ctSource.Token);

            ctSource.Cancel();

            var t = Task.Run(job.Run, ctSource.Token);

            try
            {
                t.Wait();
            }
            catch (AggregateException ea)
            {
                threwException = true;
                Assert.IsTrue(ea.InnerExceptions.All(e => e is OperationCanceledException));
            }

            Assert.IsTrue(threwException);
            Assert.IsFalse(state.NonExistingFiles[0].Exists);
            Assert.IsTrue(t.IsCanceled);
        }
        protected async Task RunTransferJob(FileTransferJob job, ProgressRecord record)
        {
            this.SetRequestOptionsInTransferJob(job);
            job.AccessCondition         = this.AccessCondition;
            job.OverwritePromptCallback = this.ConfirmOverwrite;

            try
            {
                await this.transferJobRunner.RunTransferJob(job,
                                                            (percent, speed) =>
                {
                    record.PercentComplete   = (int)percent;
                    record.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.FileTransmitStatus, (int)percent, Util.BytesToHumanReadableSize(speed));
                    this.OutputStream.WriteProgress(record);
                },
                                                            this.CmdletCancellationToken);

                record.PercentComplete   = 100;
                record.StatusDescription = Resources.TransmitSuccessfully;
                this.OutputStream.WriteProgress(record);
            }
            catch (OperationCanceledException)
            {
                record.StatusDescription = Resources.TransmitCancelled;
                this.OutputStream.WriteProgress(record);
            }
            catch (Exception e)
            {
                record.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.TransmitFailed, e.Message);
                this.OutputStream.WriteProgress(record);
                throw;
            }
        }
        protected void SetRequestOptionsInTransferJob(FileTransferJob transferJob)
        {
            var cmdletOptions = this.RequestOptions;

            if (cmdletOptions == null)
            {
                return;
            }

            var requestOptions = transferJob.FileRequestOptions;

            if (cmdletOptions.MaximumExecutionTime != null)
            {
                requestOptions.MaximumExecutionTime = cmdletOptions.MaximumExecutionTime;
            }

            if (cmdletOptions.ServerTimeout != null)
            {
                requestOptions.ServerTimeout = cmdletOptions.ServerTimeout;
            }

            requestOptions.DisableContentMD5Validation = true;

            transferJob.FileRequestOptions = requestOptions;
        }
Пример #6
0
        public void CanceledBeforeAndDestFileExists()
        {
            File.WriteAllText(state.NonExistingFiles[0].FullName, "Oh this suddenly exists..");
            var             ctSource = new CancellationTokenSource();
            FileTransferJob job      = new FileTransferJob(
                new FileTransferArguments(state.ExistingFiles[0], state.NonExistingFiles[0], TransferSettings.None),
                state.OnProgressDebugPrint
                ,
                ctSource.Token);

            ctSource.Cancel();

            var t = Task.Run(job.Run, ctSource.Token);

            try
            {
                t.Wait();
            }
            catch (AggregateException ea)
            {
                state.ExceptionThrown = true;
                Assert.IsTrue(ea.InnerExceptions.All(e => e is OperationCanceledException));
            }

            Assert.IsTrue(state.ExceptionThrown);
            Assert.IsFalse(state.NonExistingFiles[0].Exists);
            Assert.IsTrue(t.IsCanceled);
        }
        protected async Task RunTransferJob(FileTransferJob job, ProgressRecord record)
        {
            this.SetRequestOptionsInTransferJob(job);
            job.AccessCondition = this.AccessCondition;
            job.OverwritePromptCallback = this.ConfirmOverwrite;

            try
            {
                await this.transferJobRunner.RunTransferJob(job,
                        (percent, speed) =>
                        {
                            record.PercentComplete = (int)percent;
                            record.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.FileTransmitStatus, (int)percent, Util.BytesToHumanReadableSize(speed));
                            this.OutputStream.WriteProgress(record);
                        },
                        this.CmdletCancellationToken);

                record.PercentComplete = 100;
                record.StatusDescription = Resources.TransmitSuccessfully;
                this.OutputStream.WriteProgress(record);
            }
            catch (OperationCanceledException)
            {
                record.StatusDescription = Resources.TransmitCancelled;
                this.OutputStream.WriteProgress(record);
            }
            catch (Exception e)
            {
                record.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.TransmitFailed, e.Message);
                this.OutputStream.WriteProgress(record);
                throw;
            }
        }
Пример #8
0
        public void RunAsynchronously()
        {
            FileTransferJob[] jobs = new FileTransferJob[state.NonExistingFiles.Length];
            var ctSource           = new CancellationTokenSource();

            for (int i = 0; i < state.NonExistingFiles.Length; i++)
            {
                jobs[i] = new FileTransferJob(
                    new FileTransferArguments(state.ExistingFiles[0], state.NonExistingFiles[i], TransferSettings.None),
                    state.OnProgressDebugPrint
                    ,
                    ctSource.Token);
            }

            Task[] jobTasks = new Task[state.NonExistingFiles.Length];

            for (int i = 0; i < state.NonExistingFiles.Length; i++)
            {
                jobTasks[i] = Task.Run(jobs[i].Run);
            }

            Task.WaitAll(jobTasks);
            foreach (var dest in state.NonExistingFiles)
            {
                dest.Refresh();
                Assert.IsTrue(dest.Exists);
                Assert.AreEqual(File.ReadAllText(state.ExistingFiles[0].FullName), File.ReadAllText(dest.FullName));
            }
        }
Пример #9
0
        public void OpenedHandle()
        {
            using (var fs = new FileStream(state.ExistingFiles[0].FullName, FileMode.Append))
            {
                var ctSource = new CancellationTokenSource();
                var cpJob    = new FileTransferJob(
                    new FileTransferArguments(state.ExistingFiles[0], state.NonExistingFiles[0], TransferSettings.None),
                    state.OnProgressDebugPrint
                    ,
                    ctSource.Token);

                try
                {
                    cpJob.Run();
                }
                catch (FileTransferException e)
                {
                    state.ExceptionThrown = true;
                    Assert.IsTrue(e.InnerException is IOException);
                }

                Assert.IsTrue(state.ExceptionThrown);

                state.NonExistingFiles[0].Refresh();
                Assert.IsFalse(state.NonExistingFiles[0].Exists);
            }
        }
Пример #10
0
        public void DestinationAlreadyExists()
        {
            var cts     = new CancellationTokenSource();
            var moveJob = new FileTransferJob(
                new FileTransferArguments(state.ExistingFiles[0], state.ExistingFiles[1], TransferSettings.DeleteOriginal),
                state.OnProgressDebugPrint
                ,
                cts.Token);

            try
            {
                moveJob.Run();
            }
            catch (FileTransferException e)
            {
                state.ExceptionThrown = true;
                Assert.IsTrue(e.InnerException is IOException);
            }

            state.ExistingFiles[0].Refresh();
            state.ExistingFiles[1].Refresh();

            Assert.IsTrue(state.ExceptionThrown);
            Assert.IsTrue(state.ExistingFiles[0].Exists);
            Assert.IsTrue(state.ExistingFiles[1].Exists);
            Assert.AreNotEqual(File.ReadAllText(state.ExistingFiles[0].FullName), File.ReadAllText(state.ExistingFiles[1].FullName));
        }
Пример #11
0
        public void DestinationFileExists()
        {
            File.WriteAllText(state.NonExistingFiles[0].FullName, "Oh this suddenly exists..");

            var ctSource = new CancellationTokenSource();
            var cpJob    = new FileTransferJob(
                new FileTransferArguments(state.ExistingFiles[0], state.NonExistingFiles[0], TransferSettings.None),
                state.OnProgressDebugPrint
                ,
                ctSource.Token);

            try
            {
                cpJob.Run();
            }
            catch (FileTransferException e)
            {
                state.ExceptionThrown = true;
                Assert.IsTrue(e.InnerException is IOException);
            }

            Assert.IsTrue(state.ExceptionThrown);
            Assert.IsTrue(state.NonExistingFiles[0].Exists);
            Assert.AreNotEqual(File.ReadAllText(state.ExistingFiles[0].FullName), File.ReadAllText(state.NonExistingFiles[0].FullName));
        }
Пример #12
0
        public void SourceDoesNotExist()
        {
            var cts     = new CancellationTokenSource();
            var moveJob = new FileTransferJob(
                new FileTransferArguments(state.NonExistingFiles[0], state.NonExistingFiles[1], TransferSettings.DeleteOriginal),
                state.OnProgressDebugPrint
                ,
                cts.Token);

            try
            {
                moveJob.Run();
            }
            catch (FileTransferException e)
            {
                state.ExceptionThrown = true;
                Assert.IsTrue(e.InnerException is FileNotFoundException);
            }


            state.NonExistingFiles[1].Refresh();

            Assert.IsFalse(state.NonExistingFiles[1].Exists);
            Assert.IsTrue(state.ExceptionThrown);
        }
Пример #13
0
        public void RunAsynchornously()
        {
            var cts   = new CancellationTokenSource();
            var tasks = new Task[state.NonExistingFiles.Length];

            Assert.AreEqual(state.NonExistingFiles.Length, state.ExistingFiles.Length);
            for (int i = 0; i < state.NonExistingFiles.Length; i++)
            {
                var moveJob = new FileTransferJob(
                    new FileTransferArguments(state.ExistingFiles[i], state.NonExistingFiles[i], TransferSettings.DeleteOriginal),

                    state.OnProgressDebugPrint
                    ,
                    cts.Token);

                tasks[i] = Task.Run(moveJob.Run);
            }

            Task.WaitAll(tasks);

            for (int i = 0; i < state.NonExistingFiles.Length; i++)
            {
                Assert.IsTrue(tasks[i].IsCompleted);

                state.NonExistingFiles[i].Refresh();
                state.ExistingFiles[i].Refresh();
                Assert.IsFalse(state.ExistingFiles[i].Exists);

                Assert.IsTrue(state.NonExistingFiles[i].Exists);
            }
        }
Пример #14
0
        public void RunSynchronously()
        {
            var ctSource = new CancellationTokenSource();
            var cpJob    = new FileTransferJob(
                new FileTransferArguments(state.ExistingFiles[0], state.NonExistingFiles[0], TransferSettings.None),
                state.OnProgressDebugPrint
                ,
                ctSource.Token);

            cpJob.Run();

            state.NonExistingFiles[0].Refresh();
            Assert.IsTrue(state.NonExistingFiles[0].Exists);
            Assert.AreEqual(File.ReadAllText(state.ExistingFiles[0].FullName), File.ReadAllText(state.NonExistingFiles[0].FullName));
        }
Пример #15
0
        public void RunSynchornously()
        {
            string existingFileContent = File.ReadAllText(state.ExistingFiles[0].FullName);

            var cts     = new CancellationTokenSource();
            var moveJob = new FileTransferJob(
                new FileTransferArguments(state.ExistingFiles[0], state.NonExistingFiles[0], TransferSettings.DeleteOriginal),
                state.OnProgressDebugPrint
                ,
                cts.Token);

            moveJob.Run();

            state.NonExistingFiles[0].Refresh();
            state.ExistingFiles[0].Refresh();

            Assert.IsTrue(state.NonExistingFiles[0].Exists);
            Assert.IsFalse(state.ExistingFiles[0].Exists);

            Assert.AreEqual(existingFileContent, File.ReadAllText(state.NonExistingFiles[0].FullName));
        }
Пример #16
0
 void IJobVisitor.Visit(FileTransferJob job)
 {
     FileTransferArguments = job.Args;
 }
        protected void SetRequestOptionsInTransferJob(FileTransferJob transferJob)
        {
            var cmdletOptions = this.RequestOptions;

            if (cmdletOptions == null)
            {
                return;
            }

            var requestOptions = transferJob.FileRequestOptions;

            if (cmdletOptions.MaximumExecutionTime != null)
            {
                requestOptions.MaximumExecutionTime = cmdletOptions.MaximumExecutionTime;
            }

            if (cmdletOptions.ServerTimeout != null)
            {
                requestOptions.ServerTimeout = cmdletOptions.ServerTimeout;
            }

            requestOptions.DisableContentMD5Validation = true;

            transferJob.FileRequestOptions = requestOptions;
        }