public void SavesTransmissionToTheNewlyCreatedFile()
            {
                string     writtenContents = null;
                StubStream fileStream      = new StubStream();

                fileStream.OnDispose = disposing =>
                {
                    writtenContents = Encoding.UTF8.GetString(fileStream.ToArray());
                };

                var file = new StubPlatformFile {
                    OnOpen = () => fileStream
                };
                var folder = new StubPlatformFolder {
                    OnCreateFile = fileName => file
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage();

                storage.Initialize(provider);

                byte[] contents     = Encoding.UTF8.GetBytes(Path.GetRandomFileName());
                var    transmission = new StubTransmission(contents);

                storage.Enqueue(() => transmission);

                string encodedContent = writtenContents.Split(Environment.NewLine.ToCharArray()).Last();

                AssertEx.AreEqual(contents, Convert.FromBase64String(encodedContent));
            }
            public void ReturnsFalseAndDoesNotSaveTransmissionWhenStorageCapacityIsAlreadyExceeded()
            {
                StubPlatformFile existingFile = CreateTransmissionFile(default(Uri), new byte[42]);

                bool fileCreated          = false;
                StubPlatformFolder folder = CreateFolder(existingFile);

                folder.OnCreateFile = name =>
                {
                    fileCreated = true;
                    return(new StubPlatformFile());
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage {
                    Capacity = 10
                };

                storage.Initialize(provider);

                bool result = storage.Enqueue(() => new StubTransmission());

                Thread.Sleep(20);

                Assert.IsFalse(fileCreated, "file created");
                Assert.IsFalse(result);
            }
            public void SkipsTransmissionFileAlreadyLoadedByAnotherThreadAndTriesLoadingNextFile()
            {
                var files = new List <IPlatformFile>();

                StubPlatformFile loadedFile = CreateTransmissionFile();

                loadedFile.OnRename = newName =>
                {
                    files.Remove(loadedFile);
                    throw new FileNotFoundException();
                };

                StubPlatformFile nextFile = CreateTransmissionFile();

                files.Add(loadedFile);
                files.Add(nextFile);
                var folder = new StubPlatformFolder {
                    OnGetFiles = () => files
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage();

                storage.Initialize(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.IsNotNull(dequeued);
            }
            public void SkipsTransmissionFileBeingCurrentlyLoadedByAnotherThreadAndTriesLoadingNextFile()
            {
                var files = new List <IPlatformFile>();

                StubPlatformFile fileBeingLoadedByAnotherThread = CreateTransmissionFile();

                fileBeingLoadedByAnotherThread.OnRename = newName =>
                {
                    files.Remove(fileBeingLoadedByAnotherThread);
                    throw new UnauthorizedAccessException();
                };

                StubPlatformFile nextFile = CreateTransmissionFile();

                files.Add(fileBeingLoadedByAnotherThread);
                files.Add(nextFile);
                var folder = new StubPlatformFolder {
                    OnGetFiles = () => files
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage();

                storage.Initialize(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.IsNotNull(dequeued);
            }
            public void DoesNotCacheSameFilesTwiceWhenIvokedByMultipleThreads()
            {
                int numberOfGetFilesAsyncCalls = 0;
                var returnFiles = new ManualResetEventSlim();
                var files       = new StubPlatformFile[] { CreateTransmissionFile(), CreateTransmissionFile() };
                var folder      = new StubPlatformFolder();

                folder.OnGetFiles = () =>
                {
                    numberOfGetFilesAsyncCalls += 1;
                    returnFiles.Wait();
                    return(files);
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Task <Transmission> dequeue1 = TaskEx.Run(() => storage.Dequeue());
                Task <Transmission> dequeue2 = TaskEx.Run(() => storage.Dequeue());

                returnFiles.Set();
                TaskEx.WhenAll(dequeue1, dequeue2).GetAwaiter().GetResult();

                Assert.Equal(2, numberOfGetFilesAsyncCalls); // 1 for initializing size and 1 for 1 dequeue
            }
Пример #6
0
            public void DequeueWithFlushAsyncInProcessCounter()
            {
                var address           = new Uri("http://address");
                StubPlatformFile file = CreateTransmissionFile(address);

                var folder   = CreateFolder(file);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage();

                storage.Initialize(provider);

                storage.IncrementFlushAsyncCounter();
                Transmission dequeued = storage.Dequeue();

                // When FlushAsyncCounter is set, dequeue returns null.
                Assert.IsNull(dequeued);

                // DecrementFlushAsyncCounter resets FlushAsyncCounter
                // and allows storage to dequeue transmission.
                storage.DecrementFlushAsyncCounter();
                dequeued = storage.Dequeue();

                Assert.IsNotNull(dequeued);
            }
            public void DoesNotEndlesslyTryToLoadFileTheProcessNoLongerHasAccessTo()
            {
                StubPlatformFile inaccessibleFile = CreateFile("InaccessibleFile.trn");

                inaccessibleFile.OnRename = newName => { throw new UnauthorizedAccessException(); };
                StubPlatformFolder folder = CreateFolder(inaccessibleFile);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Assert.Throws <UnauthorizedAccessException>(() => storage.Dequeue());
            }
            public void SkipsCorruptTransmissionFileAndTriesLoadingFromNextTransmissionFile()
            {
                StubPlatformFile   corruptFile = CreateFile("Corrupt" + TransmissionStorage.TransmissionFileExtension);
                StubPlatformFile   validFile   = CreateTransmissionFile();
                StubPlatformFolder folder      = CreateFolder(corruptFile, validFile);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.NotNull(dequeued);
            }
            public void LoadsTransmissionFromTemporaryFile()
            {
                var expectedAddress       = new Uri("http://" + Guid.NewGuid().ToString("N"));
                StubPlatformFile   file   = CreateTransmissionFile(expectedAddress);
                StubPlatformFolder folder = CreateFolder(file);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.Equal(expectedAddress, dequeued.EndpointAddress);
            }
            public void ReturnsNullWhenFolderContainsSingleCorruptFile()
            {
                StubPlatformFile file = CreateTransmissionFile();

                file.OnOpen = () => new MemoryStream();
                StubPlatformFolder folder = CreateFolder(file);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.Null(dequeued);
            }
            public void LoadsTransmissionOnlyFromFilesWithTransmissionExtension()
            {
                StubPlatformFile unknownFile        = CreateFile("Unknown.file");
                var expectedAddress                 = new Uri("http://" + Guid.NewGuid().ToString("N"));
                StubPlatformFile   transmissionFile = CreateTransmissionFile(expectedAddress);
                StubPlatformFolder folder           = CreateFolder(unknownFile, transmissionFile);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.Equal(expectedAddress, dequeued.EndpointAddress);
            }
            public void HandlesFileNotFoundExceptionThrownWhenCalculatingSizeBecauseTransmissionHasAlreadyBeenDequeued()
            {
                var file = new StubPlatformFile();

                file.OnGetName   = () => "Dequeued" + TransmissionStorage.TransmissionFileExtension;
                file.OnGetLength = () => { throw new FileNotFoundException(); };
                StubPlatformFolder folder = CreateFolder(file);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                bool transmissionEnqueued = storage.Enqueue(() => new StubTransmission());

                Assert.True(transmissionEnqueued);
            }
            public void ChangesTransmissionFileExtensionToTemporaryToPreventConflictsWithOtherThreads()
            {
                string           temporaryFileName = null;
                StubPlatformFile file = CreateTransmissionFile();

                file.OnRename = desiredName => temporaryFileName = desiredName;
                StubPlatformFolder folder = CreateFolder(file);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                storage.Dequeue();

                Assert.True(temporaryFileName.EndsWith(TransmissionStorage.TemporaryFileExtension, StringComparison.OrdinalIgnoreCase));
            }
            public void DeletesTemporaryFileAfterLoadingToFreeDiskSpace()
            {
                bool             fileDeleted = false;
                StubPlatformFile file        = CreateTransmissionFile();

                file.OnDelete = () => fileDeleted = true;
                StubPlatformFolder folder = CreateFolder(file);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                storage.Dequeue();

                Assert.True(fileDeleted);
            }
            public void ReturnsFalseIfProcessHasNoRightToWriteToFilesInApplicationFolder()
            {
                var file = new StubPlatformFile {
                    OnOpen = () => { throw new UnauthorizedAccessException(); }
                };
                var folder = new StubPlatformFolder {
                    OnCreateFile = name => file
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                bool result = storage.Enqueue(() => new StubTransmission());

                Assert.False(result);
            }
            public void ChangesTemporaryExtensionToPermanentToMakeFileAvailableForDequeueAsync()
            {
                string           permanentFileName = null;
                StubPlatformFile file = CreateFile("TemporaryFile");

                file.OnRename = desiredName => permanentFileName = desiredName;
                var folder = new StubPlatformFolder {
                    OnCreateFile = fileName => file
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                storage.Enqueue(() => new StubTransmission());

                Assert.True(permanentFileName.EndsWith(TransmissionStorage.TransmissionFileExtension, StringComparison.OrdinalIgnoreCase));
            }
            public void GetsMultipleFilesFromFolderOnceAndCachesThemToReduceDiskAccess()
            {
                int numberOfGetFilesAsyncCalls = 0;
                var files  = new StubPlatformFile[] { CreateTransmissionFile(), CreateTransmissionFile() };
                var folder = new StubPlatformFolder();

                folder.OnGetFiles = () =>
                {
                    numberOfGetFilesAsyncCalls += 1;
                    return(files);
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Assert.NotNull(storage.Dequeue());
                Assert.NotNull(storage.Dequeue());
                Assert.Equal(2, numberOfGetFilesAsyncCalls); // 1 for initializing size and 1 for 1 dequeue
            }
            public void ClosesFileStreamToEnsureFileCanBeDeleted()
            {
                bool fileStreamDisposed = false;
                var  fileStream         = new StubStream {
                    OnDispose = disposing => fileStreamDisposed = true
                };
                StubPlatformFile file = CreateFile("TestFile" + TransmissionStorage.TransmissionFileExtension, fileStream);
                bool             fileStreamDisposedBeforeDeletion = false;

                file.OnDelete = () => fileStreamDisposedBeforeDeletion = fileStreamDisposed;

                StubPlatformFolder folder = CreateFolder(file);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                storage.Dequeue();

                Assert.True(fileStreamDisposedBeforeDeletion);
            }
            public void ClosesFileStreamToEnsureChangesAreCommittedToDisk()
            {
                bool fileStreamDisposed = false;
                var  fileStream         = new StubStream {
                    OnDispose = disposing => fileStreamDisposed = true
                };
                var file = new StubPlatformFile {
                    OnOpen = () => fileStream
                };
                var folder = new StubPlatformFolder {
                    OnCreateFile = fileName => file
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                storage.Enqueue(() => new StubTransmission());

                Assert.True(fileStreamDisposed);
            }
            public void LoadsTransmissionFromTheOldestTransmissionFileInTheFolder()
            {
                var newestAddress           = new Uri("http://newest");
                StubPlatformFile newestFile = CreateTransmissionFile(newestAddress);

                newestFile.OnGetDateCreated = () => DateTimeOffset.MaxValue;

                var oldestAddress           = new Uri("http://oldest");
                StubPlatformFile oldestFile = CreateTransmissionFile(oldestAddress);

                oldestFile.OnGetDateCreated = () => DateTimeOffset.MinValue;

                var folder   = CreateFolder(newestFile, oldestFile);
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.Equal(oldestAddress, dequeued.EndpointAddress);
            }
            public void DeletesStoredFilesOlderThanTwoDays()
            {
                var files = new List <IPlatformFile>();

                StubPlatformFile oldFile = CreateTransmissionFile();

                oldFile.OnGetDateCreated = () => DateTimeOffset.Now.AddHours(-49);
                oldFile.OnDelete         = () => files.Remove(oldFile);
                files.Add(oldFile);
                var folder = new StubPlatformFolder {
                    OnGetFiles = () => files
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage();

                storage.Initialize(provider);

                Transmission dequeued = storage.Dequeue();

                Assert.IsNull(dequeued);
            }