diff --git a/src/GameServiceWarden.Core/Module/LRUCache.cs b/src/GameServiceWarden.Core/Collection/LRUCache.cs similarity index 75% rename from src/GameServiceWarden.Core/Module/LRUCache.cs rename to src/GameServiceWarden.Core/Collection/LRUCache.cs index 2a227e1..c61490f 100644 --- a/src/GameServiceWarden.Core/Module/LRUCache.cs +++ b/src/GameServiceWarden.Core/Collection/LRUCache.cs @@ -1,9 +1,10 @@ using System; +using System.Collections; using System.Collections.Generic; -namespace GameServiceWarden.Core.Module +namespace GameServiceWarden.Core.Collection { - public class LRUCache + public class LRUCache : IEnumerable { private class Node { @@ -14,7 +15,7 @@ namespace GameServiceWarden.Core.Module } public int Size {get { return size; } } - public int Length {get { return valueDictionary.Count; } } + public int Count {get { return valueDictionary.Count; } } private readonly int size; private Node top; private Node bottom; @@ -49,7 +50,7 @@ namespace GameServiceWarden.Core.Module bottom = node; } else if (valueDictionary.Count == Size) { valueDictionary.Remove(bottom.key); - cleanupAction(bottom.value); + cleanupAction?.Invoke(bottom.value); bottom = bottom.front; } valueDictionary[key] = node; @@ -68,10 +69,27 @@ namespace GameServiceWarden.Core.Module return value.value; } + public bool IsCached(K key) { + return valueDictionary.ContainsKey(key); + } + public void Clear() { top = null; bottom = null; valueDictionary.Clear(); } + + public IEnumerator GetEnumerator() + { + foreach (Node node in valueDictionary.Values) + { + yield return node.value; + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/src/GameServiceWarden.Core/Module/ServiceManager.cs b/src/GameServiceWarden.Core/Module/ServiceManager.cs index 8685a5e..027376c 100644 --- a/src/GameServiceWarden.Core/Module/ServiceManager.cs +++ b/src/GameServiceWarden.Core/Module/ServiceManager.cs @@ -2,14 +2,10 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; -using System.Collections.ObjectModel; -using System.IO; -using System.IO.Pipes; -using System.Linq; using GameServiceWarden.API.Games; using GameServiceWarden.Core.Persistence; using GameServiceWarden.API.Module; -using System.Text; +using GameServiceWarden.Core.Collection; namespace GameServiceWarden.Core.Module { diff --git a/tests/GameServiceWarden.Core.Tests/Collection/FakeDisposable.cs b/tests/GameServiceWarden.Core.Tests/Collection/FakeDisposable.cs new file mode 100644 index 0000000..3c17145 --- /dev/null +++ b/tests/GameServiceWarden.Core.Tests/Collection/FakeDisposable.cs @@ -0,0 +1,36 @@ +using System; + +namespace GameServiceWarden.Core.Tests.Collection +{ + public class FakeDisposable : IDisposable + { + private string value; + private bool disposedValue; + + public FakeDisposable(string value) + { + this.value = value; + } + + public bool IsDisposed() { + return disposedValue; + } + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + } + disposedValue = true; + } + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + } +} \ No newline at end of file diff --git a/tests/GameServiceWarden.Core.Tests/Collection/LRUCacheTest.cs b/tests/GameServiceWarden.Core.Tests/Collection/LRUCacheTest.cs new file mode 100644 index 0000000..433ca8b --- /dev/null +++ b/tests/GameServiceWarden.Core.Tests/Collection/LRUCacheTest.cs @@ -0,0 +1,72 @@ +using GameServiceWarden.Core.Collection; +using Xunit; + +namespace GameServiceWarden.Core.Tests.Collection +{ + public class LRUCacheTest + { + [Fact] + public void Use_SufficientSpace_StoredUsed() + { + //Given + string data = "data"; + LRUCache cache = new LRUCache(10); + //When + cache.Use(0, () => data); + //Then + Assert.Same(data, cache.Use(0, () => "other")); + } + + [Fact] + public void Use_InsufficientSpace_LastUsedRemoved() + { + //Given + string[] data = new string[] { "a", "b", "c" }; + LRUCache cache = new LRUCache(2); + //When + for (int i = 0; i < data.Length; i++) + { + cache.Use(i, () => data[i]); + } + //Then + Assert.Contains("c", cache); + Assert.Contains("b", cache); + Assert.DoesNotContain("a", cache); + } + + [Fact] + public void IsCached_CachedData_True() + { + //Given + string[] data = new string[] { "a", "b", "c" }; + LRUCache cache = new LRUCache(2); + //When + for (int i = 0; i < data.Length; i++) + { + cache.Use(i, () => data[i]); + } + //Then + Assert.True(cache.IsCached(2)); + Assert.True(cache.IsCached(1)); + Assert.False(cache.IsCached(0)); + } + + [Fact] + public void Use_CleanupDelSet_DataChanged() + { + //Given + FakeDisposable[] data = new FakeDisposable[] { new FakeDisposable("a"), new FakeDisposable("b"), new FakeDisposable("c")}; + LRUCache cache = new LRUCache(2, (d) => d.Dispose()); + //When + for (int i = 0; i < data.Length; i++) + { + cache.Use(i, () => data[i]); + } + //Then + Assert.True(data[0].IsDisposed()); + Assert.False(data[1].IsDisposed()); + Assert.False(data[2].IsDisposed()); + } + + } +} \ No newline at end of file diff --git a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakePersistence.cs b/tests/GameServiceWarden.Core.Tests/Modules/FakePersistence.cs similarity index 100% rename from tests/GameServiceWarden.Core.Tests/Modules/Games/FakePersistence.cs rename to tests/GameServiceWarden.Core.Tests/Modules/FakePersistence.cs diff --git a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeService.cs b/tests/GameServiceWarden.Core.Tests/Modules/FakeService.cs similarity index 97% rename from tests/GameServiceWarden.Core.Tests/Modules/Games/FakeService.cs rename to tests/GameServiceWarden.Core.Tests/Modules/FakeService.cs index 9c2a169..f14059c 100644 --- a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeService.cs +++ b/tests/GameServiceWarden.Core.Tests/Modules/FakeService.cs @@ -4,7 +4,7 @@ using System.IO; using System.Threading.Tasks; using GameServiceWarden.API.Module; -namespace GameServiceWarden.Core.Tests.Modules.Games +namespace GameServiceWarden.Core.Tests.Modules { public class FakeService : IService { diff --git a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceConfigurable.cs b/tests/GameServiceWarden.Core.Tests/Modules/FakeServiceConfigurable.cs similarity index 90% rename from tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceConfigurable.cs rename to tests/GameServiceWarden.Core.Tests/Modules/FakeServiceConfigurable.cs index 23482d1..dd34bd3 100644 --- a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceConfigurable.cs +++ b/tests/GameServiceWarden.Core.Tests/Modules/FakeServiceConfigurable.cs @@ -1,6 +1,6 @@ using GameServiceWarden.API.Module; -namespace GameServiceWarden.Core.Tests.Modules.Games +namespace GameServiceWarden.Core.Tests.Modules { public class FakeServiceConfigurable : IServiceConfigurable { diff --git a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceManagerMonitor.cs b/tests/GameServiceWarden.Core.Tests/Modules/FakeServiceManagerMonitor.cs similarity index 92% rename from tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceManagerMonitor.cs rename to tests/GameServiceWarden.Core.Tests/Modules/FakeServiceManagerMonitor.cs index 35faa5c..55d205b 100644 --- a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceManagerMonitor.cs +++ b/tests/GameServiceWarden.Core.Tests/Modules/FakeServiceManagerMonitor.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using GameServiceWarden.API.Games; using GameServiceWarden.Core.Module; -namespace GameServiceWarden.Core.Tests.Modules.Games +namespace GameServiceWarden.Core.Tests.Modules { public class FakeServiceManagerMonitor : IServiceManagerMonitor { diff --git a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceModule.cs b/tests/GameServiceWarden.Core.Tests/Modules/FakeServiceModule.cs similarity index 92% rename from tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceModule.cs rename to tests/GameServiceWarden.Core.Tests/Modules/FakeServiceModule.cs index 2e0da7f..4593586 100644 --- a/tests/GameServiceWarden.Core.Tests/Modules/Games/FakeServiceModule.cs +++ b/tests/GameServiceWarden.Core.Tests/Modules/FakeServiceModule.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using GameServiceWarden.API.Module; -namespace GameServiceWarden.Core.Tests.Modules.Games +namespace GameServiceWarden.Core.Tests.Modules { public class FakeServiceModule : IServiceModule { diff --git a/tests/GameServiceWarden.Core.Tests/Modules/Games/ServiceDescriptorTest.cs b/tests/GameServiceWarden.Core.Tests/Modules/ServiceDescriptorTest.cs similarity index 99% rename from tests/GameServiceWarden.Core.Tests/Modules/Games/ServiceDescriptorTest.cs rename to tests/GameServiceWarden.Core.Tests/Modules/ServiceDescriptorTest.cs index 5dd11c1..94df858 100644 --- a/tests/GameServiceWarden.Core.Tests/Modules/Games/ServiceDescriptorTest.cs +++ b/tests/GameServiceWarden.Core.Tests/Modules/ServiceDescriptorTest.cs @@ -7,7 +7,7 @@ using Xunit; using Xunit.Abstractions; using System.Text; -namespace GameServiceWarden.Core.Tests.Modules.Games +namespace GameServiceWarden.Core.Tests.Modules { // Testing convention from: https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices // fakes are generic test objects, diff --git a/tests/GameServiceWarden.Core.Tests/Modules/Games/ServiceManagerTest.cs b/tests/GameServiceWarden.Core.Tests/Modules/ServiceManagerTest.cs similarity index 99% rename from tests/GameServiceWarden.Core.Tests/Modules/Games/ServiceManagerTest.cs rename to tests/GameServiceWarden.Core.Tests/Modules/ServiceManagerTest.cs index a553bfc..14d9f98 100644 --- a/tests/GameServiceWarden.Core.Tests/Modules/Games/ServiceManagerTest.cs +++ b/tests/GameServiceWarden.Core.Tests/Modules/ServiceManagerTest.cs @@ -12,7 +12,7 @@ using Xunit.Abstractions; using System.Text; [assembly: CollectionBehavior(DisableTestParallelization = true)] -namespace GameServiceWarden.Core.Tests.Modules.Games +namespace GameServiceWarden.Core.Tests.Modules { [CollectionDefinition("Service")] public class ServiceManagerTest diff --git a/tests/GameServiceWarden.Core.Tests/Persistence/ServiceDescriptorPersistenceTest.cs b/tests/GameServiceWarden.Core.Tests/Persistence/ServiceDescriptorPersistenceTest.cs index eb5630e..11a86ea 100644 --- a/tests/GameServiceWarden.Core.Tests/Persistence/ServiceDescriptorPersistenceTest.cs +++ b/tests/GameServiceWarden.Core.Tests/Persistence/ServiceDescriptorPersistenceTest.cs @@ -1,10 +1,8 @@ -using System.Collections; using System.Collections.Generic; using System.IO; using GameServiceWarden.Core.Module; using GameServiceWarden.Core.Persistence; using GameServiceWarden.Core.Tests.Modules; -using GameServiceWarden.Core.Tests.Modules.Games; using GameServiceWarden.API.Module; using Xunit;