Ejemplo n.º 1
0
            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);
            }
Ejemplo n.º 2
0
            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.False(fileCreated, "filecreated");
                Assert.False(result);
            }
Ejemplo n.º 3
0
            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();

                Assert.Equal(contents, Convert.FromBase64String(encodedContent));
            }
Ejemplo n.º 4
0
            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);
            }
Ejemplo n.º 5
0
            public void DoesNotCacheSameFilesTwiceWhenIvokedByMultipleThreads()
            {
                int numberOfGetFilesAsyncCalls = 0;
                var returnFiles = new ManualResetEventSlim();
                var files       = new [] { CreateTransmissionFile(), CreateTransmissionFile() };
                var folder      = new StubPlatformFolder();

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

                storage.Initialize(provider);

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

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

                Assert.AreEqual(2, numberOfGetFilesAsyncCalls); // 1 for initializing size and 1 for 1 dequeue
            }
            public void ReturnsFalseIfApplicationFolderIsNotAvailable()
            {
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => null
                };
                var storage = new TransmissionStorage(provider);

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

                Assert.False(result);
            }
            public void ReturnsNullIfApplicationFolderIsNotAvailable()
            {
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => null
                };
                var storage = new TransmissionStorage(provider);

                Transmission result = storage.Dequeue();

                Assert.Null(result);
            }
            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 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 ReturnsFalseIfProcessHasNoRightToCreateFilesInApplicationFolder()
            {
                var folder = new StubPlatformFolder {
                    OnCreateFile = name => { throw new UnauthorizedAccessException(); }
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

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

                Assert.False(result);
            }
            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 ReturnsNullIfApplicationFolderIsNotAccessible()
            {
                var folder = new StubPlatformFolder {
                    OnGetFiles = () => { throw new UnauthorizedAccessException(); }
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

                Transmission result = storage.Dequeue();

                Assert.Null(result);
            }
            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 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 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 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 DoesNotRemoveTransmissionFromBufferIfApplicationFolderIsNotAvailableToAvoidDroppingData()
            {
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => null
                };
                var  storage = new TransmissionStorage(provider);
                bool transmissionRemovedFromBuffer = false;

                storage.Enqueue(() =>
                {
                    transmissionRemovedFromBuffer = true;
                    return(new StubTransmission());
                });

                Assert.False(transmissionRemovedFromBuffer);
            }
            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 ReturnsFalseWhenPreviousTransmissionExceedsCapacity()
            {
                StubPlatformFolder folder = CreateFolder();
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider)
                {
                    Capacity = 10
                };

                bool firstTransmissionSaved  = storage.Enqueue(() => new Transmission(new Uri("any://address"), new byte[42], string.Empty, string.Empty));
                bool secondTransmissionSaved = storage.Enqueue(() => new Transmission(new Uri("any://address"), new byte[42], string.Empty, string.Empty));

                Assert.True(firstTransmissionSaved);
                Assert.False(secondTransmissionSaved);
            }
            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 CreatesNewFileWithTemporaryExtensionToPreventConflictsWithDequeueAsync()
            {
                string temporaryFileName = null;
                var    folder            = new StubPlatformFolder
                {
                    OnCreateFile = fileName =>
                    {
                        temporaryFileName = fileName;
                        return(new StubPlatformFile());
                    }
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

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

                Assert.True(temporaryFileName.EndsWith(TransmissionStorage.TemporaryFileExtension, 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 CreatesFilesWithUniqueNamesToPreventConflictsWithOtherThreads()
            {
                var actualFileNames = new List <string>();
                var folder          = new StubPlatformFolder
                {
                    OnCreateFile = fileName =>
                    {
                        actualFileNames.Add(fileName);
                        return(new StubPlatformFile());
                    }
                };
                var provider = new StubApplicationFolderProvider {
                    OnGetApplicationFolder = () => folder
                };
                var storage = new TransmissionStorage(provider);

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

                Assert.NotEqual(actualFileNames[0], actualFileNames[1]);
            }
            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 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 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);
            }