From 4ba4a8d73329754f6865aa49d2f456045e396c9b Mon Sep 17 00:00:00 2001 From: Cole Brown Date: Mon, 23 Aug 2021 16:56:51 -0700 Subject: [PATCH] Fixed 3 unit tests. Fixed: - FileCacheTest.CacheSizeTest - FileCacheTest.ShrinkCacheTest - HashedFileCacheTest.CacheSizeTest Moved the calculation of the new policy file's size to be outside the 'using' blocks for writing it to disk so that it could read the actual file size instead of always getting 0. - It is moved entirely out of the using blocks to mirror the cache item's code. Removed a duplicated line of code in FileCache.WriteHelper(). - There was an `if` line that was there twice. - This has no effect on anything other than my sanity. --- src/FileCache/FileCache.cs | 10 +++++----- src/FileCache/FileCacheManager.cs | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/FileCache/FileCache.cs b/src/FileCache/FileCache.cs index fbe9bda..34ad22e 100644 --- a/src/FileCache/FileCache.cs +++ b/src/FileCache/FileCache.cs @@ -608,12 +608,14 @@ private long CacheSizeHelper(DirectoryInfo root) { size += fi.Length; } + // Add subdirectory sizes. var dis = root.EnumerateDirectories(); foreach (DirectoryInfo di in dis) { size += CacheSizeHelper(di); } + return size; } @@ -743,10 +745,9 @@ private void WriteHelper(PayloadMode mode, string key, FileCachePayload data, st //check to see if limit was reached if (CurrentCacheSize > MaxCacheSize) - if (CurrentCacheSize > MaxCacheSize) - { - MaxCacheSizeReached(this, new FileCacheEventArgs(CurrentCacheSize, MaxCacheSize)); - } + { + MaxCacheSizeReached(this, new FileCacheEventArgs(CurrentCacheSize, MaxCacheSize)); + } } #endregion @@ -955,7 +956,6 @@ public override object Remove(string key, string regionName = null) { object valueToDelete = null; - if (Contains(key, regionName) == true) { diff --git a/src/FileCache/FileCacheManager.cs b/src/FileCache/FileCacheManager.cs index 5361cc4..2d8bc09 100644 --- a/src/FileCache/FileCacheManager.cs +++ b/src/FileCache/FileCacheManager.cs @@ -284,12 +284,12 @@ public virtual long WriteFile(FileCache.PayloadMode mode, string key, FileCacheP using (BinaryWriter writer = new BinaryWriter(stream)) { data.Policy.Serialize(writer); - - // adjust cache size - cacheSizeDelta += new FileInfo(cachedPolicy).Length; } } + // Adjust cache size outside of the using blocks to ensure it's after the data is written. + cacheSizeDelta += new FileInfo(cachedPolicy).Length; + return cacheSizeDelta; }