public void SetUp() { _delayIntervalInSeconds = 0; _parsedResult = null; DirectoryUtility.EnsureClearFolder(FolderName); }
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 DeserializeCache_FromSerializedCache_CacheIsCorrect() { //Arrange List <FileSystemItem> items = new List <FileSystemItem> { new FileSystemItem("name1", "folder1"), new FileSystemItem("C:\\") }; DateTime dateTime = DateTime.Now; FileSystemCache cache = new FileSystemCache(items, dateTime); _serializer.SerializeCache(cache); //Act FileSystemCache actualCache = _serializer.DeserializeCache(); //Assert //Serialization does not preserve milliseconds, so we use epsilon check. Assert.That(dateTime - actualCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 1))); Assert.That(actualCache.Items.Count, Is.EqualTo(2)); Assert.That(actualCache.Items[0].FullPath, Is.EqualTo(Path.GetFullPath("folder1"))); Assert.That(actualCache.Items[0].Name, Is.EqualTo("name1")); Assert.That(actualCache.Items[1].FullPath, Is.EqualTo(Path.GetFullPath("C:\\"))); Assert.That(actualCache.Items[1].Name, Is.EqualTo("")); }
public async Task BasicTest() { var cacheDir = GetCacheDir(); if (Directory.Exists(cacheDir)) { Directory.Delete(cacheDir, true); } var cache = new FileSystemCache <int, int>(cacheDir); (await cache.TryGetAsync(1)).Should().Be(Option.None <int>()); await Assert.ThrowsAsync <KeyNotFoundException>(async() => await cache.GetAsync(1)); await cache.SetAsync(1, 1); (await cache.TryGetAsync(1)).Should().Be(Option.Some <int>(1)); (await cache.GetAsync(1)).Should().Be(1); (await cache.TryGetAsync(2)).Should().Be(Option.None <int>()); await Assert.ThrowsAsync <KeyNotFoundException>(async() => await cache.GetAsync(2)); await cache.InvalidateAsync(1); (await cache.TryGetAsync(1)).Should().Be(Option.None <int>()); await Assert.ThrowsAsync <KeyNotFoundException>(async() => await cache.GetAsync(1)); }
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 TTLReachedEventArgs(string path, FileSystemCache cache, double ttl, bool ttlUpdated = false) : base() { Path = path; Cache = cache; TTLUpdated = ttlUpdated; TTL = ttl; }
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 async Task BasicTest() { var cacheDir = GetCacheDir(); if (Directory.Exists(cacheDir)) { Directory.Delete(cacheDir, true); } var cache = new FileSystemCache <int, int>(cacheDir); (await cache.TryGet(1)).Should().Be(Option.None <int>()); (await cache.Get(1)).Should().Be(0); await cache.Set(1, 1); (await cache.TryGet(1)).Should().Be(Option.Some <int>(1)); (await cache.Get(1)).Should().Be(1); (await cache.TryGet(2)).Should().Be(Option.None <int>()); (await cache.Get(2)).Should().Be(0); await cache.Remove(1); (await cache.TryGet(1)).Should().Be(Option.None <int>()); (await cache.Get(1)).Should().Be(0); }
private void ParseFileSystem() { //Don't set any restrictions on this parsing, as want to grab the entire system. List <FileSystemItem> fileSystemItems = _fileSystemParser.GetSubFolders(); _fileSystem = new FileSystemCache(fileSystemItems, DateTime.Now); }
public void GetSubFolders_CacheOnDiskAndActiveAndFileSystemUpdatedEnoughTimes_CacheIsSerializedAsActive() { DateTime initialCacheDateTime = DateTime.Now; SetUpCache(initialCacheDateTime); _updatesCountToWrite = 1; CreateCachedParser(); //To maintain significant time difference for timestamp Thread.Sleep(1000); //Temp folder will be created while initial cache serialization. Directory.CreateDirectory("Folder\\Temp2"); Directory.CreateDirectory("Folder\\Temp3"); //To let events be handled Thread.Sleep(200); FileSystemCache updatedCache = _serializer.DeserializeCache(); //Updated cache is not older than 2 seconds Assert.That(updatedCache.LastFullScanTime > initialCacheDateTime, Is.True); Assert.That(GetFileSystemItemNames(updatedCache), Is.EquivalentTo(new List <string> { "Cache1", "Cache2", "Temp2", "Temp3" })); }
public void SerializeCache(FileSystemCache cache) { if (cache == null || cache.Items == null) { throw new ArgumentNullException("cache"); } //File system path can not contain ?, so this format is not ambiguous List <string> lines = cache.Items.Select(GetCacheItemLine).ToList(); lines.Insert(0, cache.LastFullScanTime.ToString(CultureInfo.InvariantCulture)); lock (CacheSync) { DirectoryUtility.EnsureFolder(Path.GetDirectoryName(_cacheFilePath)); File.WriteAllLines(_cacheFilePath, lines.ToArray()); if (File.Exists(_cacheChangesFilePath)) { File.Delete(_cacheChangesFilePath); } } GC.Collect(); }
public override void done() { this.Logger.LogInfo(true, "Clearing File System Cache..." + Environment.NewLine + Environment.NewLine + UtilsSystem.DebugTable(FileSystemCache.GetFileSystemCacheBytes())); FileSystemCache.ClearFileSystemCache(true); this.Logger.LogInfo(true, "Finished clearing File System Cache..." + Environment.NewLine + Environment.NewLine + UtilsSystem.DebugTable(FileSystemCache.GetFileSystemCacheBytes())); }
private void HandleParsingFinished(object sender, ItemEventArgs <FileSystemCache> e) { lock (_parsingSync) { _parsedResult = e.Item; _parsingFinish = DateTime.Now; } }
public void TestInit() { _fs = new IsolatedStorageFileSystem(IsolatedStorageFile.GetUserStoreForAssembly()); _cache = new FileSystemCache(_fs); _cache.Empty(); _cleaner = new LRUFileCacheCleaner(_fs); }
private FileSystemCache ReadCache() { string[] lines = File.ReadAllLines(_cacheFilePath); List <FileSystemItem> items = lines.Skip(1).Select(ParseCacheItem).Where(i => i != null).ToList(); DateTime lastFullScanTime = DateTime.Parse(lines[0], CultureInfo.InvariantCulture); FileSystemCache cache = new FileSystemCache(items, lastFullScanTime); return(cache); }
//public static void Main(string[] args) //{ // var path = @"C:\Users\jtl86\source\repos\RefBotCompare\RefBotCompare\config.json"; // var config = LoadConfig(path); // var client = CreateRefBotClient(config); // var extractor = new ProjectExtractor(new HtmlParser()); // foreach (var p in config.Projects) // { // var html = client.FetchProjectHtml(p.RefBotId).GetAwaiter().GetResult(); // var project = extractor.ExtractProject(html); // } //} public static IRefBotClient CreateRefBotClient(Config config) { var cache = new FileSystemCache(config.CacheDir); var client = new RefBotClient(config.SessionCookie); var rateLimited = new RateLimitedRefBotClient(client, config.MaxRefBotRequests); return(new CachedRefBotClient(rateLimited, cache)); }
private bool CacheUpToDate(FileSystemCache cache) { DateTime lastShutDownTime = _registryService.GetLastSystemShutDownTime(); DateTime lastCacheWriteTime = cache.LastFullScanTime; TimeSpan timeDifference = lastShutDownTime - lastCacheWriteTime; bool cacheValid = timeDifference.TotalSeconds < CacheValidityPeriodInSeconds; return(cacheValid); }
public void DeserializeCache_WithExistingCacheAndDoubleSavedChanges_CacheIsCorrect() { //Arrange List <FileSystemItem> cacheItems = new List <FileSystemItem> { new FileSystemItem("name1", "folder1"), new FileSystemItem("name1", "folder2"), new FileSystemItem("C:\\") }; FileSystemCache cache = new FileSystemCache(cacheItems, DateTime.Now.AddDays(-2)); _serializer.SerializeCache(cache); List <FileSystemChangeEventArgs> changeItems = new List <FileSystemChangeEventArgs> { new FileSystemChangeEventArgs("folder1", null), new FileSystemChangeEventArgs(null, "folder3"), }; FileSystemChanges changes = new FileSystemChanges { Changes = changeItems, LastFullScanTime = DateTime.Now.AddDays(-1) }; _serializer.SerializeCacheChanges(changes); changeItems = new List <FileSystemChangeEventArgs> { new FileSystemChangeEventArgs("folder2", "folder4") }; DateTime dateTime = DateTime.Now; changes = new FileSystemChanges { Changes = changeItems, LastFullScanTime = dateTime }; _serializer.SerializeCacheChanges(changes); //Act FileSystemCache actualCache = _serializer.DeserializeCache(); //Assert //Serialization does not preserve milliseconds, so we use epsilon check. Assert.That(dateTime - actualCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 1))); Assert.That(actualCache.Items.Count, Is.EqualTo(3)); Assert.That(actualCache.Items[0].FullPath, Is.EqualTo(Path.GetFullPath("C:\\"))); Assert.That(actualCache.Items[0].Name, Is.EqualTo("")); Assert.That(actualCache.Items[1].FullPath, Is.EqualTo(Path.GetFullPath("folder3"))); Assert.That(actualCache.Items[1].Name, Is.EqualTo("folder3")); Assert.That(actualCache.Items[2].FullPath, Is.EqualTo(Path.GetFullPath("folder4"))); Assert.That(actualCache.Items[2].Name, Is.EqualTo("folder4")); }
protected override IAsyncCache <string, string> CreateCache() { if (!UseCache) { _log.LogDebug($"Cache isn't used."); return(new EmptyCache <string, string>()); } var cache = new FileSystemCache <string, string>(GetCacheDir()); _log.LogDebug($"Cache directory: {cache.CacheDirectory}"); return(cache); }
private void ResetFilteredCacheItems() { lock (_cacheSync) { SerializeChanges(); //_cacheUpToDate will be the same. If it's up to date, it should be up to date; otherwise, not. FileSystemCache fullCache = _cacheSerializer.DeserializeCache(); _filteredCacheItems = FilterCache(fullCache); fullCache = null; GC.Collect(); } }
public void ParagraphShouldParseCorrectly() { FileSystemCache.AddFile("templates/template/tag_templates/p.html", new MockFileData("<p>{{}}</p>")); FileSystemCache.AddFile("templates/template/site_template.html", new MockFileData("<html>{{}}</html>")); FileSystemCache.AddDirectory("output"); FileSystemCache.AddFile("input/file1.md", new MockFileData("This is some text!")); ServiceProvider.GetService <Generator>().Start(); const string expectedContent = @"<html><p>This is some text!</p></html>"; const string expectedName = "/output/file1.html"; Assert.True(this.FileExists(expectedName)); Assert.Equal(expectedContent, this.ReadFileContents(expectedName)); }
private void SetUpCache(DateTime cacheSaveTime) { List <FileSystemItem> items = new List <FileSystemItem> { new FileSystemItem(FolderName + "\\Cache1"), new FileSystemItem(FolderName + "\\Cache2"), }; // Cache is one day older than the last shutdown FileSystemCache cache = new FileSystemCache(items, cacheSaveTime); _serializer.SerializeCache(cache); _registryService.LastSystemShutDownTime = DateTime.Now; }
public void Test() { FileSystemCache.AddDirectory("input"); FileSystemCache.AddFile("input/file1.txt", new MockFileData(string.Empty)); FileSystemCache.AddFile("input/file2.txt", new MockFileData(string.Empty)); FileSystemCache.AddDirectory("input/Folder1"); FileSystemCache.AddFile("input/Folder1/file1.txt", new MockFileData(string.Empty)); FileSystemCache.AddFile("input/Folder1/file2.txt", new MockFileData(string.Empty)); FileSystemCache.AddDirectory("output"); ServiceProvider.GetService <Generator>().Start(); Assert.Contains("/output/file1.txt", FileSystemCache.AllFiles); Assert.Contains("/output/file2.txt", FileSystemCache.AllFiles); Assert.Contains("/output/Folder1/file1.txt", FileSystemCache.AllFiles); Assert.Contains("/output/Folder1/file2.txt", FileSystemCache.AllFiles); }
public void ShouldCopySubDirectories() { FileSystemCache.AddFile("templates/template/tag_templates/h1.html", new MockFileData("<h1>{{}}</h1>")); FileSystemCache.AddFile("templates/template/tag_templates/p.html", new MockFileData("<p>{{}}</p>")); FileSystemCache.AddFile("templates/template/site_template.html", new MockFileData("<html>{{}}</html>")); FileSystemCache.AddDirectory("output"); FileSystemCache.AddDirectory("input/Folder1"); FileSystemCache.AddDirectory("input/Folder1/SubFolder1"); FileSystemCache.AddDirectory("input/Folder1/SubFolder2"); FileSystemCache.AddDirectory("input/Folder2"); ServiceProvider.GetService <Generator>().Start(); Assert.Contains("/output/Folder1/SubFolder1", FileSystemCache.AllDirectories); Assert.Contains("/output/Folder1/SubFolder2", FileSystemCache.AllDirectories); Assert.Contains("/output/Folder2", FileSystemCache.AllDirectories); }
private void HandleParsingFinished(object sender, ItemEventArgs <FileSystemCache> e) { lock (_cacheSync) { FileSystemCache fullCache = e.Item; fullCache.LastFullScanTime = DateTime.Now; _cacheSerializer.SerializeCache(fullCache); _lastFullScanTime = fullCache.LastFullScanTime; _filteredCacheItems = FilterCache(fullCache); _fileSystemChanges = new FileSystemChanges(); fullCache = null; GC.Collect(); _asyncFileSystemParser.ParsingFinished -= HandleParsingFinished; } }
public void AddGetRemove_Success() { // Arrange Customer customer = TestData.CreateCustomer(); ICache cache = new FileSystemCache(); // Add, Get and Assert cache.Add(customer.Id.ToString(), customer); object customerObject = cache.Get(customer.Id.ToString()); // Assert Assert.IsNotNull(customerObject); Assert.IsInstanceOfType(customerObject, typeof(Customer)); // Remove and Assert cache.Remove(customer.Id.ToString()); customerObject = cache.Get(customer.Id.ToString()); Assert.IsNull(customerObject); }
private List <FileSystemItem> ReadCache(bool appRunOnStartup, out bool fullCacheUpToDate, out bool cacheFolderCreated, out DateTime lastFullScanTime) { cacheFolderCreated = false; //Parse file system FileSystemCache fullCache = _cacheSerializer.DeserializeCache(); if (fullCache != null) { //The cache file can be up to date only if the current Navigation Assistant has been run on startup //and if it had been closed just on system shutdown and the current parser is created at application start. //In this case no additional folders can be created during NavAssistant being inactive. fullCacheUpToDate = CacheUpToDate(fullCache) && appRunOnStartup; } else { //The application is loaded for the first time (no cache stored on disk). string cacheFolder = GetCacheFolder(); bool cacheFolderExisted = Directory.Exists(cacheFolder); //Run this method in the main thread, thus freezing it. //Don't set any restrictions on this parsing, as want to grab the entire system. fullCache = new FileSystemCache(_fileSystemParser.GetSubFolders(), DateTime.Now); _cacheSerializer.SerializeCache(fullCache); //Updating the cache if cache folder has been created bool cacheFolderExists = Directory.Exists(cacheFolder); cacheFolderCreated = !cacheFolderExisted && cacheFolderExists; fullCacheUpToDate = true; } List <FileSystemItem> filteredCache = FilterCache(fullCache); lastFullScanTime = fullCache.LastFullScanTime; fullCache = null; GC.Collect(); return(filteredCache); }
public void DeserializeCache_WithExistingChangesAndAbsentCache_CacheIsNull() { //Arrange List <FileSystemChangeEventArgs> changeItems = new List <FileSystemChangeEventArgs> { new FileSystemChangeEventArgs("folder1", null), new FileSystemChangeEventArgs(null, "folder3"), new FileSystemChangeEventArgs("folder2", "folder4") }; DateTime dateTime = DateTime.Now; FileSystemChanges changes = new FileSystemChanges { Changes = changeItems, LastFullScanTime = DateTime.Now }; _serializer.SerializeCacheChanges(changes); //Act FileSystemCache actualCache = _serializer.DeserializeCache(); //Assert Assert.That(actualCache, Is.Null); }
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 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)); }
public void TestInit() { _cache = new FileSystemCache(new StandardFileSystem()); _cache.Empty(); }