Beispiel #1
0
            public async Task WillThrowIfFileExistsAndOverwriteFalseAndRealFileSystemIsUsed()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    var directory = Path.Combine(testDirectory, FolderName);
                    var filePath  = Path.Combine(directory, FileName);
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(filePath, FileContent);

                    // Act
                    var exception = await Assert.ThrowsAsync <InvalidOperationException>(() => service.SaveFileAsync(
                                                                                             FolderName,
                                                                                             FileName,
                                                                                             new MemoryStream(),
                                                                                             overwrite: false));

                    Assert.True(File.Exists(filePath), $"The file at path {filePath} should exist, but does not.");
                    Assert.Equal(FileContent, File.ReadAllText(filePath));
                }
            }
Beispiel #2
0
            public async Task WillOverwriteFileIfOverwriteTrueAndRealFileSystemIsUsed()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    var directory = Path.Combine(testDirectory, FolderName);
                    var filePath  = Path.Combine(directory, FileName);
                    Directory.CreateDirectory(directory);
                    File.WriteAllText(filePath, string.Empty);

                    // Act
                    await service.SaveFileAsync(
                        FolderName,
                        FileName,
                        new MemoryStream(Encoding.ASCII.GetBytes(FileContent)),
                        overwrite : true);

                    Assert.True(File.Exists(filePath), $"The file at path {filePath} should exist, but does not.");
                    Assert.Equal(FileContent, File.ReadAllText(filePath));
                }
            }
Beispiel #3
0
            public async Task WillThrowIfFileExistsWhenManyThreadsAreTryingToSaveWithoutOverwriting()
            {
                // Arrange
                using (var testDirectory = TestDirectory.Create())
                {
                    var fileSystemService = new FileSystemService();

                    var configuration = new Mock <IAppConfiguration>();
                    configuration
                    .Setup(x => x.FileStorageDirectory)
                    .Returns(testDirectory);

                    var service = new FileSystemFileStorageService(
                        configuration.Object,
                        fileSystemService);

                    for (var i = 0; i < 10; i++)
                    {
                        var fileName = FileName + i;
                        var barrier  = new Barrier(TaskCount);
                        var tasks    = new List <Task <bool> >();

                        // Act
                        for (var taskIndex = 0; taskIndex < TaskCount; taskIndex++)
                        {
                            var task = SaveFileAsync(service, fileName, barrier);
                            tasks.Add(task);
                        }

                        var results = await Task.WhenAll(tasks);

                        // Assert
                        // One task should succeed. One should fail.
                        Assert.Equal(1, results.Count(success => success));
                        Assert.Equal(TaskCount - 1, results.Count(success => !success));
                    }
                }
            }