Exemple #1
0
        public void RealFile_FileCreationIsTracked()
        {
            // Arrange
            var testDir = CreateTestSpecificDirectory();
            var filePathToMonitor = Path.Combine(testDir, "settingsFile.txt");

            using (var singleFileMonitor = new SingleFileMonitor(filePathToMonitor, new TestLogger()))
            {
                var testWrapper = new WaitableFileMonitor(singleFileMonitor);
                testWrapper.EventCount.Should().Be(0);

                // Act: create the file
                File.WriteAllText(filePathToMonitor, "initial text");
                testWrapper.WaitForEventAndThrowIfMissing();
                testWrapper.EventCount.Should().Be(1);
            }
        }
Exemple #2
0
        public void Real_File_ChangesToOtherFiles_AreIgnored()
        {
            // Arrange
            var testDir = CreateTestSpecificDirectory();
            var filePathToMonitor = Path.Combine(testDir, "settingsFile.txt");
            var otherFileInDir = Path.Combine(testDir, "otherSettingsFile.txt");

            using (var firstMonitor = new SingleFileMonitor(filePathToMonitor, new TestLogger()))
            {
                var waitableFileMonitor = new WaitableFileMonitor(firstMonitor);
                waitableFileMonitor.EventCount.Should().Be(0);

                // 1. Create the other file
                File.WriteAllText(otherFileInDir, "initial other text");
                System.Threading.Thread.Sleep(1000);
                waitableFileMonitor.EventCount.Should().Be(0);
            }
        }
Exemple #3
0
        public void RealFile_FileDeletionIsTracked()
        {
            // Arrange
            var testLogger        = new TestLogger(logToConsole: true, logThreadId: true);
            var testDir           = CreateTestSpecificDirectory();
            var filePathToMonitor = Path.Combine(testDir, "settingsFile.txt");

            File.WriteAllText(filePathToMonitor, "contents");

            using (var singleFileMonitor = new SingleFileMonitor(filePathToMonitor, testLogger))
            {
                var testWrapper = new WaitableFileMonitor(singleFileMonitor);
                testWrapper.EventCount.Should().Be(0);

                // Act: delete the file
                File.Delete(filePathToMonitor);
                testWrapper.WaitForEventAndThrowIfMissing();
                testWrapper.EventCount.Should().Be(1);
            }
        }
Exemple #4
0
        public void RealFile_RenameToTrackedFileName_IsTracked()
        {
            // Arrange
            var testDir = CreateTestSpecificDirectory();
            var filePathToMonitor = Path.Combine(testDir, "settingsFile.txt");
            var otherFilePath = Path.ChangeExtension(filePathToMonitor, "other");
            File.WriteAllText(otherFilePath, "contents");

            using (var singleFileMonitor = new SingleFileMonitor(filePathToMonitor, new TestLogger()))
            {
                var testWrapper = new WaitableFileMonitor(singleFileMonitor);
                testWrapper.EventCount.Should().Be(0);

                // Act: rename the file to the tracked name
                File.Move(otherFilePath, filePathToMonitor);

                testWrapper.WaitForEventAndThrowIfMissing();
                testWrapper.EventCount.Should().Be(1);
            }
        }
Exemple #5
0
        public void RealFile_RenameFromTrackedFileName_IsTracked()
        {
            // Arrange
            var testLogger        = new TestLogger(logToConsole: true, logThreadId: true);
            var testDir           = CreateTestSpecificDirectory();
            var filePathToMonitor = Path.Combine(testDir, "settingsFile.txt");

            File.WriteAllText(filePathToMonitor, "contents");

            using (var singleFileMonitor = new SingleFileMonitor(filePathToMonitor, testLogger))
            {
                var testWrapper = new WaitableFileMonitor(singleFileMonitor);
                testWrapper.EventCount.Should().Be(0);

                // Act: rename the file from the tracked name
                var renamedFile = Path.ChangeExtension(filePathToMonitor, "moved");
                File.Move(filePathToMonitor, renamedFile);

                testWrapper.WaitForEventAndThrowIfMissing();
                testWrapper.EventCount.Should().Be(1);
            }
        }
Exemple #6
0
        public void RealFile_MultipleOperations_NoDuplicates()
        {
            // Repeated file operations will raise lots of events.
            // We don't want duplicates.

            // Arrange
            var testLogger        = new TestLogger(logToConsole: true, logThreadId: true);
            var testDir           = CreateTestSpecificDirectory();
            var filePathToMonitor = Path.Combine(testDir, "settingsFile.txt");

            int totalEventCount = 0;

            for (int i = 0; i < 100; i++)
            {
                TestContext.WriteLine($"Iteration: {i}");

                // Cleanup
                if (File.Exists(filePathToMonitor))
                {
                    File.Delete(filePathToMonitor);
                }
                testBody();
            }

            // We might lose some events if they happen too close together, but we should never
            // have more events than there are logical file operations.
            // There are 5 operations per iteration -> expecting close to 500.

            TestContext.WriteLine($"Number of recorded events: {totalEventCount}");
            totalEventCount.Should().BeInRange(340, 500);

            void testBody()
            {
                const int pauseBetweenOpsInMs = 125;

                using (var singleFileMonitor = new SingleFileMonitor(filePathToMonitor, testLogger))
                {
                    var testWrapper = new WaitableFileMonitor(singleFileMonitor);
                    testWrapper.EventCount.Should().Be(0);

                    // 1. Create the file
                    File.WriteAllText(filePathToMonitor, "initial text");
                    testWrapper.PauseForEvent(pauseBetweenOpsInMs);

                    // 2. Amend the file
                    File.AppendAllText(filePathToMonitor, " more text");
                    testWrapper.PauseForEvent(pauseBetweenOpsInMs);

                    // 3. Rename from the tracked name
                    var renamedFile = Path.ChangeExtension(filePathToMonitor, "moved");
                    File.Move(filePathToMonitor, renamedFile);
                    testWrapper.PauseForEvent(pauseBetweenOpsInMs);

                    // 4. Rename back to the tracked name
                    File.Move(renamedFile, filePathToMonitor);
                    testWrapper.PauseForEvent(pauseBetweenOpsInMs);

                    // 5. Delete
                    File.Delete(filePathToMonitor);
                    testWrapper.PauseForEvent(pauseBetweenOpsInMs);

                    totalEventCount += testWrapper.EventCount;
                }
            };
        }