public void Dispose_CacheOnDiskAndCacheIsNotUpToDateAndParsingNotFinished_CacheIsSerializedAsInactive()
        {
            //Set up parsing delay at 1 minute, so it asynchronous parsing will not have been finished when creating folders
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 60);

            DateTime oldDateTime = DateTime.Now.AddDays(-1);

            SetUpCache(oldDateTime);

            CreateCachedParser();

            //Wait while async parsing is finished
            Thread.Sleep(200);

            _cachedFileSystemParser.Dispose();

            FileSystemCache updatedCache = _serializer.DeserializeCache();

            Assert.That(oldDateTime - updatedCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 2)));

            Assert.That(GetFileSystemItemNames(updatedCache), Is.EquivalentTo(new List <string> {
                "Cache1", "Cache2"
            }));
        }
        public void GetSubFolders_CacheOnDiskAndCacheIsNotUpToDateAndFileSystemUpdatedEnoughTimesAfterParsing_CacheIsSerializedAsActive()
        {
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 0);

            SetUpInactiveCache();

            _updatesCountToWrite = 1;
            CreateCachedParser();

            //Wait while async parsing is finished
            Thread.Sleep(200);

            Directory.CreateDirectory("Folder\\Temp2");
            Directory.CreateDirectory("Folder\\Temp3");

            //Wait while folder creation is handled
            Thread.Sleep(200);

            FileSystemCache updatedCache = _serializer.DeserializeCache();

            //Updated cache is close to oldDateTime
            Assert.That(DateTime.Now - updatedCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 2)));

            Assert.That(GetFileSystemItemNames(updatedCache), Is.EquivalentTo(new List <string> {
                "Folder", "Temp", "Temp2", "Temp3"
            }));
        }
        public void GetSubFolders_CacheOnDiskExistsAndIsNotUpToDateAndFilterUpdatedWhileParsing_ParsedItemsReturnedAfterParsingFilteredCorrectly()
        {
            //Set up parsing delay at 1 minute, so it asynchronous parsing will not have been finished when calling GetSubFolders
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 0);

            SetUpInactiveCache();

            CreateCachedParser();

            //Wait until parsing is finished
            Thread.Sleep(200);
            _cachedFileSystemParser.ExcludeFolderTemplates = new List <string> {
                "Temp"
            };

            List <FileSystemItem> subfolders = _cachedFileSystemParser.GetSubFolders();

            Assert.That(subfolders.Count, Is.EqualTo(1));
            Assert.That(subfolders[0].Name, Is.EqualTo("Folder"));

            _cachedFileSystemParser.FoldersToParse = new List <string> {
                "Folder\\Temp\\"
            };
            subfolders = _cachedFileSystemParser.GetSubFolders();
            Assert.That(subfolders.Count, Is.EqualTo(0));
        }
        public void GetSubFolders_CacheOnDiskAndCacheIsNotUpToDateAndFileSystemUpdatedEnoughTimesWhileParsing_CacheIsSerializedAsInactive()
        {
            //Set up parsing delay at 1 minute, so it asynchronous parsing will not have been finished when creating folders
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 60);

            DateTime oldDateTime = DateTime.Now.AddDays(-1);

            SetUpCache(oldDateTime);

            _updatesCountToWrite = 1;
            CreateCachedParser();

            Directory.CreateDirectory("Folder\\Temp2");
            Directory.CreateDirectory("Folder\\Temp3");
            Thread.Sleep(200);

            FileSystemCache updatedCache = _serializer.DeserializeCache();

            //Updated cache is close to oldDateTime
            Assert.That(oldDateTime - updatedCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 2)));

            Assert.That(GetFileSystemItemNames(updatedCache), Is.EquivalentTo(new List <string> {
                "Cache1", "Cache2", "Temp2", "Temp3"
            }));
        }
        public void GetSubFolders_CacheOnDiskExistsAndIsNotUpToDateAndFileSystemUpdatedWhileParsing_ParsedItemsReturnedAfterParsingContainUpdate()
        {
            FileSystemParserWithAction parser = new FileSystemParserWithAction(new FileSystemListener(), new List <string> {
                FolderName
            });

            parser.Action = (() => Directory.CreateDirectory(FolderName + "\\Temp2"));
            parser.DelayInMilliseconds = 200;
            _asyncParser = new AsyncFileSystemParser(parser, 0);

            SetUpInactiveCache();

            _fileSystemParser.Action = (() => Directory.CreateDirectory(FolderName + "\\Temp2"));
            CreateCachedParser();

            //To wait for folder creation
            Thread.Sleep(400);
            List <FileSystemItem> subfolders = _cachedFileSystemParser.GetSubFolders();

            List <string> expectedSubfolderNames = new List <string> {
                "Folder", "Temp", "Temp2"
            };

            Assert.That(GetFileSystemItemNames(subfolders), Is.EquivalentTo(expectedSubfolderNames));
        }
        public void GetSubFolders_CacheOnDiskExistsAndIsNotUpToDate_CachedItemsReturnedBeforeParsing()
        {
            //Set up parsing delay at 1 minute, so it asynchronous parsing will not have been finished when calling GetSubFolders
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 60);

            SetUpInactiveCache();

            CreateCachedParser();

            List <FileSystemItem> subfolders = _cachedFileSystemParser.GetSubFolders();

            Assert.That(GetFileSystemItemNames(subfolders), Is.EquivalentTo(new List <string> {
                "Cache1", "Cache2"
            }));
        }
        public void Dispose_CacheOnDiskAndCacheIsNotUpToDateAndParsingFinished_CacheIsSerializedAsActive()
        {
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 0);

            SetUpInactiveCache();

            CreateCachedParser();

            //Wait while async parsing is finished
            Thread.Sleep(200);

            _cachedFileSystemParser.Dispose();

            FileSystemCache updatedCache = _serializer.DeserializeCache();

            Assert.That(DateTime.Now - updatedCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 2)));

            Assert.That(GetFileSystemItemNames(updatedCache), Is.EquivalentTo(new List <string> {
                "Folder", "Temp"
            }));
        }
        public void SetUp()
        {
            _listener   = new FileSystemListener();
            _serializer = new CacheSerializer(TempFileName);
            //Don't use the same listener for parser and cached file parser, as they will operate on different threads
            _fileSystemParser = new FileSystemParserWithAction(new FileSystemListener(), new List <string> {
                FolderName
            });
            _registryService = new FakeRegistryService();
            _asyncParser     = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }));
            _appRunOnStartup     = true;
            _updatesCountToWrite = -1;

            DirectoryUtility.EnsureClearFolder(FolderName);

            if (Directory.Exists(FolderName))
            {
                DeleteFolder();
            }
            Directory.CreateDirectory(FolderName);
        }
        public void GetSubFolders_CacheOnDiskExistsAndIsNotUpToDate_ParsedItemsReturned()
        {
            //Start asynchronous parsing at once
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 0);

            SetUpInactiveCache();

            CreateCachedParser();

            //To wait for asynchronous parsing
            Thread.Sleep(200);
            List <FileSystemItem> subfolders = _cachedFileSystemParser.GetSubFolders();

            Assert.That(subfolders.Count, Is.EqualTo(2));

            List <string> expectedSubfolderNames = new List <string> {
                "Folder", "Temp"
            };

            Assert.That(GetFileSystemItemNames(subfolders), Is.EquivalentTo(expectedSubfolderNames));
        }
        public void GetSubFolders_CacheOnDiskExistsAndIsNotUpToDate_CacheSerializedAsActiveAfterParsing()
        {
            //Start asynchronous parsing at once
            _asyncParser = new AsyncFileSystemParser(new FileSystemParser(new FileSystemListener(), new List <string> {
                FolderName
            }), 0);

            SetUpInactiveCache();

            CreateCachedParser();

            //To wait for asynchronous parsing
            Thread.Sleep(200);
            FileSystemCache updatedCache = _serializer.DeserializeCache();

            //Updated cache is not older than 2 seconds
            Assert.That(DateTime.Now - updatedCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 2)));

            List <string> expectedSubfolderNames = new List <string> {
                "Folder", "Temp"
            };

            Assert.That(GetFileSystemItemNames(updatedCache), Is.EquivalentTo(expectedSubfolderNames));
        }