private OperationContinuationOptions(
     string filePath,
     bool applyToAll,
     OperationContinuationMode mode,
     string newFilePath = null)
 {
     FilePath    = filePath;
     ApplyToAll  = applyToAll;
     Mode        = mode;
     NewFilePath = newFilePath;
 }
Esempio n. 2
0
 private OperationContinuationOptions CreateFrom(OperationContinuationMode mode) =>
 OperationContinuationOptions.CreateContinuationOptions(
     ReplaceWithFileViewModel.FullPath,
     ShouldApplyToAll,
     mode
     );
 public static OperationContinuationOptions CreateContinuationOptions(
     string filePath, bool applyToAll, OperationContinuationMode mode) =>
 new OperationContinuationOptions(filePath, applyToAll, mode);
    public async Task TestBlockedCopyOperationMultiple(bool applyToAll, int expectedCallbackCallsCount,
                                                       OperationContinuationMode mode, int expectedWriteCallsCountFirstFile, int expectedWriteCallsCountSecondFile,
                                                       int expectedWriteCallsCountThirdFile, int expectedWriteCallsCountFourthFile)
    {
        _autoMocker
        .Setup <IFileNameGenerationService, string>(m => m.GenerateFullName(It.IsAny <string>()))
        .Returns <string>(GetNewFileName);

        var taskCompletionSource       = new TaskCompletionSource <bool>();
        var secondTaskCompletionSource = new TaskCompletionSource <bool>();
        var copyOperation = new Mock <IInternalOperation>();

        copyOperation
        .SetupGet(m => m.State)
        .Returns(OperationState.Blocked);
        var blockedOperation = copyOperation.As <ISelfBlockingOperation>();

        blockedOperation
        .Setup(m => m.CurrentBlockedFile)
        .Returns((SourceName, DestinationName));
        blockedOperation
        .Setup(m => m.ContinueAsync(It.Is <OperationContinuationOptions>(
                                        o => o.ApplyToAll == applyToAll &&
                                        o.Mode == mode)))
        .Returns(() =>
        {
            copyOperation
            .SetupGet(m => m.State)
            .Returns(OperationState.Finished);
            copyOperation.Raise(m => m.StateChanged += null,
                                new OperationStateChangedEventArgs(OperationState.Finished));
            taskCompletionSource.SetResult(true);

            return(Task.CompletedTask);
        })
        .Verifiable();
        copyOperation
        .Setup(m => m.RunAsync(It.IsAny <CancellationToken>()))
        .Returns(async() =>
        {
            copyOperation.Raise(m => m.StateChanged += null,
                                new OperationStateChangedEventArgs(OperationState.Blocked));

            await taskCompletionSource.Task;
        });
        var secondCopyOperation = new Mock <IInternalOperation>();

        secondCopyOperation
        .SetupGet(m => m.State)
        .Returns(OperationState.Blocked);
        var secondBlockedOperation = secondCopyOperation.As <ISelfBlockingOperation>();

        secondBlockedOperation
        .Setup(m => m.CurrentBlockedFile)
        .Returns((SecondSourceName, SecondDestinationName));
        secondBlockedOperation
        .Setup(m => m.ContinueAsync(It.Is <OperationContinuationOptions>(
                                        o => o.ApplyToAll == applyToAll &&
                                        o.Mode == mode)))
        .Returns(() =>
        {
            secondCopyOperation
            .SetupGet(m => m.State)
            .Returns(OperationState.Finished);
            secondCopyOperation.Raise(m => m.StateChanged += null,
                                      new OperationStateChangedEventArgs(OperationState.Finished));
            secondTaskCompletionSource.SetResult(true);

            return(Task.CompletedTask);
        })
        .Verifiable();
        secondCopyOperation
        .Setup(m => m.RunAsync(It.IsAny <CancellationToken>()))
        .Returns(async() =>
        {
            secondCopyOperation.Raise(m => m.StateChanged += null,
                                      new OperationStateChangedEventArgs(OperationState.Blocked));

            await secondTaskCompletionSource.Task;
        });
        IReadOnlyList <OperationGroup> operationGroups = new List <OperationGroup>
        {
            new OperationGroup(
                new[] { copyOperation.Object }),
            new OperationGroup(
                new[] { secondCopyOperation.Object })
        };

        _autoMocker.Use(operationGroups);
        var settings = new BinaryFileSystemOperationSettings(
            new string[] { },
            new[] { SourceName, SecondSourceName },
            new string[] { },
            new[] { DestinationName, SecondDestinationName },
            new Dictionary <string, string>
        {
            [SourceName]       = DestinationName,
            [SecondSourceName] = SecondDestinationName
        },
            new string[] { }
            );

        _autoMocker.Use(new OperationInfo(OperationType.Copy, settings));

        var operation = _autoMocker.CreateInstance <CompositeOperation>();

        var callbackCallsCount = 0;

        operation.Blocked += async(sender, args) =>
        {
            callbackCallsCount++;

            var(sourceFilePath, _) = operation.CurrentBlockedFile;
            var options = mode is OperationContinuationMode.Rename
                ? OperationContinuationOptions.CreateRenamingContinuationOptions(sourceFilePath, applyToAll,
                                                                                 GetNewFileName(sourceFilePath))
                : OperationContinuationOptions.CreateContinuationOptions(sourceFilePath, applyToAll, mode);

            await operation.ContinueAsync(options);
        };

        var task      = operation.RunAsync();
        var firstTask = await Task.WhenAny(task, Task.Delay(1000));

        Assert.Equal(task, firstTask);

        Assert.Equal(expectedCallbackCallsCount, callbackCallsCount);

        blockedOperation
        .Verify(m => m.ContinueAsync(It.Is <OperationContinuationOptions>(
                                         o => o.ApplyToAll == applyToAll &&
                                         o.Mode == mode &&
                                         o.FilePath == SourceName &&
                                         o.NewFilePath == null)),
                Times.Exactly(expectedWriteCallsCountFirstFile));
        blockedOperation
        .Verify(m => m.ContinueAsync(It.Is <OperationContinuationOptions>(
                                         o => o.ApplyToAll == applyToAll &&
                                         o.Mode == mode &&
                                         o.FilePath == SourceName &&
                                         o.NewFilePath == ThirdDestinationName)),
                Times.Exactly(expectedWriteCallsCountThirdFile));
        secondBlockedOperation
        .Verify(m => m.ContinueAsync(It.Is <OperationContinuationOptions>(
                                         o => o.ApplyToAll == applyToAll &&
                                         o.Mode == mode &&
                                         o.FilePath == SecondSourceName &&
                                         o.NewFilePath == null)),
                Times.Exactly(expectedWriteCallsCountSecondFile));
        secondBlockedOperation
        .Verify(m => m.ContinueAsync(It.Is <OperationContinuationOptions>(
                                         o => o.ApplyToAll == applyToAll &&
                                         o.Mode == mode &&
                                         o.FilePath == SecondSourceName &&
                                         o.NewFilePath == FourthDestinationName)),
                Times.Exactly(expectedWriteCallsCountFourthFile));
    }
    public async Task TestBlockedCopyOperationSingle(bool applyToAll, OperationContinuationMode mode,
                                                     int expectedWriteCalls, int expectedWriteCallsSecondFile, int offsetHours = -1)
    {
        var now           = DateTime.UtcNow;
        var nowWithOffset = now.AddHours(offsetHours);

        _autoMocker
        .Setup <IFileService, FileModel>(m => m.GetFile(SourceName))
        .Returns(new FileModel {
            LastModifiedDateTime = now
        });
        _autoMocker
        .Setup <IFileService, FileModel>(m => m.GetFile(DestinationName))
        .Returns(new FileModel {
            LastModifiedDateTime = nowWithOffset
        });
        _autoMocker
        .Setup <IFileService>(
            m => m.CopyAsync(SourceName, DestinationName, It.IsAny <CancellationToken>(), false))
        .Verifiable();
        _autoMocker
        .Setup <IFileService, Task <bool> >(m =>
                                            m.CopyAsync(SourceName, DestinationName, It.IsAny <CancellationToken>(), true))
        .ReturnsAsync(true)
        .Verifiable();
        _autoMocker
        .Setup <IFileService, Task <bool> >(m =>
                                            m.CopyAsync(SourceName, SecondDestinationName, It.IsAny <CancellationToken>(), false))
        .ReturnsAsync(true)
        .Verifiable();
        _autoMocker
        .Setup <IFileService, bool>(m => m.CheckIfExists(DestinationName))
        .Returns(true);
        var operationsFactory = _autoMocker.CreateInstance <OperationsFactory>();
        var settings          = new BinaryFileSystemOperationSettings(
            new string[] { },
            new[] { SourceName },
            new string[] { },
            new[] { DestinationName },
            new Dictionary <string, string>
        {
            [SourceName] = DestinationName
        },
            new string[] { }
            );
        var copyOperation = operationsFactory.CreateCopyOperation(settings);

        var callbackCallsCount = 0;

        copyOperation.StateChanged += async(sender, args) =>
        {
            if (args.OperationState != OperationState.Blocked)
            {
                return;
            }

            var operation = (IOperation)sender;
            if (operation is null)
            {
                return;
            }

            callbackCallsCount++;

            var(sourceFilePath, _) = operation.CurrentBlockedFile;
            var options = mode is OperationContinuationMode.Rename
                ? OperationContinuationOptions.CreateRenamingContinuationOptions(sourceFilePath, applyToAll,
                                                                                 SecondDestinationName)
                : OperationContinuationOptions.CreateContinuationOptions(sourceFilePath, applyToAll, mode);

            await copyOperation.ContinueAsync(options);
        };

        await copyOperation.RunAsync();

        Assert.Equal(1, callbackCallsCount);

        Assert.Equal(OperationState.Finished, copyOperation.State);
        _autoMocker
        .Verify <IFileService>(
            m => m.CopyAsync(SourceName, DestinationName, It.IsAny <CancellationToken>(), true),
            Times.Exactly(expectedWriteCalls));
        _autoMocker
        .Verify <IFileService>(
            m => m.CopyAsync(SourceName, SecondDestinationName, It.IsAny <CancellationToken>(), true),
            Times.Exactly(expectedWriteCallsSecondFile));
    }
Esempio n. 6
0
        public async Task TestBlockedCopyOperation(bool applyToAll, int expectedCallbackCallsCount,
                                                   OperationContinuationMode mode, int expectedWriteCallsCountFirstFile, int expectedWriteCallsCountSecondFile)
        {
            var now           = DateTime.UtcNow;
            var hourBeforeNow = now.AddHours(-1);

            var directoryServiceMock = new Mock <IDirectoryService>();
            var filesServiceMock     = new Mock <IFileService>();

            filesServiceMock
            .Setup(m => m.GetFile(SourceName))
            .Returns(new FileModel {
                LastModifiedDateTime = now
            });
            filesServiceMock
            .Setup(m => m.GetFile(DestinationName))
            .Returns(new FileModel {
                LastModifiedDateTime = hourBeforeNow
            });
            filesServiceMock
            .Setup(m => m.GetFile(SecondSourceName))
            .Returns(new FileModel {
                LastModifiedDateTime = hourBeforeNow
            });
            filesServiceMock
            .Setup(m => m.GetFile(SecondDestinationName))
            .Returns(new FileModel {
                LastModifiedDateTime = now
            });
            filesServiceMock
            .Setup(m => m.CopyAsync(SourceName, DestinationName, false))
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CopyAsync(SourceName, DestinationName, true))
            .Returns(Task.CompletedTask)
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CopyAsync(SecondSourceName, SecondDestinationName, false))
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CopyAsync(SecondSourceName, SecondDestinationName, true))
            .Returns(Task.CompletedTask)
            .Verifiable();
            filesServiceMock
            .Setup(m => m.CheckIfExists(It.IsAny <string>()))
            .Returns(true);
            var operationsFactory = new OperationsFactory(
                _taskPool,
                directoryServiceMock.Object,
                filesServiceMock.Object,
                _pathService,
                _fileNameGenerationService);
            var settings = new BinaryFileSystemOperationSettings(
                new string[] { },
                new[] { SourceName, SecondSourceName },
                new string[] { },
                new[] { DestinationName, SecondDestinationName },
                new Dictionary <string, string>
            {
                [SourceName]       = DestinationName,
                [SecondSourceName] = SecondDestinationName
            },
                new string[] { }
                );
            var copyOperation = operationsFactory.CreateCopyOperation(settings);

            var callbackCallsCount = 0;

            copyOperation.StateChanged += async(sender, args) =>
            {
                if (args.OperationState != OperationState.Blocked)
                {
                    return;
                }

                var operation = (IOperation)sender;
                if (operation is null)
                {
                    return;
                }

                callbackCallsCount++;

                var(sourceFilePath, _) = operation.CurrentBlockedFile;
                var options = OperationContinuationOptions.CreateContinuationOptions(sourceFilePath, applyToAll, mode);

                await copyOperation.ContinueAsync(options);
            };

            await copyOperation.RunAsync();

            Assert.Equal(expectedCallbackCallsCount, callbackCallsCount);

            Assert.Equal(OperationState.Finished, copyOperation.State);
            filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName, true), Times.Exactly(expectedWriteCallsCountFirstFile));
            filesServiceMock.Verify(m => m.CopyAsync(SecondSourceName, SecondDestinationName, true), Times.Exactly(expectedWriteCallsCountSecondFile));
            filesServiceMock.Verify(m => m.CopyAsync(SourceName, DestinationName, false), Times.Never);
            filesServiceMock.Verify(m => m.CopyAsync(SecondSourceName, SecondDestinationName, false), Times.Never);
        }
Esempio n. 7
0
        public async Task TestBlockedCopyOperation(bool applyToAll, int expectedCallbackCallsCount,
                                                   OperationContinuationMode mode, int expectedWriteCallsCountFirstFile, int expectedWriteCallsCountSecondFile)
        {
            var now           = DateTime.UtcNow;
            var hourBeforeNow = now.AddHours(-1);

            _autoMocker
            .Setup <IFileService, FileModel>(m => m.GetFile(SourceName))
            .Returns(new FileModel {
                LastModifiedDateTime = now
            });
            _autoMocker
            .Setup <IFileService, FileModel>(m => m.GetFile(DestinationName))
            .Returns(new FileModel {
                LastModifiedDateTime = hourBeforeNow
            });
            _autoMocker
            .Setup <IFileService, FileModel>(m => m.GetFile(SecondSourceName))
            .Returns(new FileModel {
                LastModifiedDateTime = hourBeforeNow
            });
            _autoMocker
            .Setup <IFileService, FileModel>(m => m.GetFile(SecondDestinationName))
            .Returns(new FileModel {
                LastModifiedDateTime = now
            });
            _autoMocker
            .Setup <IFileService>(m => m.CopyAsync(SourceName, DestinationName, false))
            .Verifiable();
            _autoMocker
            .Setup <IFileService, Task <bool> >(m => m.CopyAsync(SourceName, DestinationName, true))
            .ReturnsAsync(true)
            .Verifiable();
            _autoMocker
            .Setup <IFileService>(m => m.CopyAsync(SecondSourceName, SecondDestinationName, false))
            .Verifiable();
            _autoMocker
            .Setup <IFileService, Task <bool> >(m => m.CopyAsync(SecondSourceName, SecondDestinationName, true))
            .ReturnsAsync(true)
            .Verifiable();
            _autoMocker
            .Setup <IFileService, bool>(m => m.CheckIfExists(It.IsAny <string>()))
            .Returns(true);
            var operationsFactory = _autoMocker.CreateInstance <OperationsFactory>();
            var settings          = new BinaryFileSystemOperationSettings(
                new string[] { },
                new[] { SourceName, SecondSourceName },
                new string[] { },
                new[] { DestinationName, SecondDestinationName },
                new Dictionary <string, string>
            {
                [SourceName]       = DestinationName,
                [SecondSourceName] = SecondDestinationName
            },
                new string[] { }
                );
            var copyOperation = operationsFactory.CreateCopyOperation(settings);

            var callbackCallsCount = 0;

            copyOperation.StateChanged += async(sender, args) =>
            {
                if (args.OperationState != OperationState.Blocked)
                {
                    return;
                }

                var operation = (IOperation)sender;
                if (operation is null)
                {
                    return;
                }

                Interlocked.Increment(ref callbackCallsCount);

                var(sourceFilePath, _) = operation.CurrentBlockedFile;
                var options = OperationContinuationOptions.CreateContinuationOptions(sourceFilePath, applyToAll, mode);

                await copyOperation.ContinueAsync(options);
            };

            await copyOperation.RunAsync();

            Assert.Equal(expectedCallbackCallsCount, callbackCallsCount);

            Assert.Equal(OperationState.Finished, copyOperation.State);
            _autoMocker
            .Verify <IFileService>(m => m.CopyAsync(SourceName, DestinationName, true), Times.Exactly(expectedWriteCallsCountFirstFile));
            _autoMocker
            .Verify <IFileService>(m => m.CopyAsync(SecondSourceName, SecondDestinationName, true), Times.Exactly(expectedWriteCallsCountSecondFile));
            _autoMocker
            .Verify <IFileService>(m => m.CopyAsync(SourceName, DestinationName, false), Times.Never);
            _autoMocker
            .Verify <IFileService>(m => m.CopyAsync(SecondSourceName, SecondDestinationName, false), Times.Never);
        }