Exemplo n.º 1
0
        private void When_buffer_overflows_it_must_raise_error_event()
        {
            // Arrange
            const string directoryToWatch = @"c:\some";

            string pathToFileToUpdate = Path.Combine(directoryToWatch, "file.txt");

            FakeFileSystem fileSystem = new FakeFileSystemBuilder()
                                        .IncludingEmptyFile(pathToFileToUpdate)
                                        .Build();

            DateTime startTime = DateTime.UtcNow;

            var            lockObject     = new object();
            ErrorEventArgs firstErrorArgs = null;

            using (FakeFileSystemWatcher watcher = fileSystem.ConstructFileSystemWatcher(directoryToWatch))
            {
                watcher.NotifyFilter       = TestNotifyFilters.All;
                watcher.InternalBufferSize = 1;
                watcher.Changed           += (sender, args) => { Thread.Sleep(100); };
                watcher.Error += (sender, args) =>
                {
                    lock (lockObject)
                    {
                        if (firstErrorArgs == null)
                        {
                            firstErrorArgs = args;
                        }
                    }
                };
                watcher.EnableRaisingEvents = true;

                // Act
                while (startTime.AddMilliseconds(MaxTestDurationInMilliseconds) > DateTime.UtcNow)
                {
                    fileSystem.File.SetCreationTimeUtc(pathToFileToUpdate, 1.January(2001));

                    lock (lockObject)
                    {
                        if (firstErrorArgs != null)
                        {
                            break;
                        }
                    }
                }

                lock (lockObject)
                {
                    // Assert
                    firstErrorArgs.Should().NotBeNull();
                    Exception exception = firstErrorArgs.GetException();

                    exception.Should().BeOfType <InternalBufferOverflowException>()
                    .Subject.Message.Should().Be(@"Too many changes at once in directory:c:\some.");
                }
            }
        }