public static async Task GetSetValueAsync_WhenInvokedTogetherUpdatingValue_HasEqualInputAndOutput() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "getsetvalueasync_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); var input = new byte[] { 1, 2, 3, 4 }; var updatedInput = new byte[] { 3, 4, 5, 6 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { await cache.SetValueAsync("asd", new MemoryStream(input)).ConfigureAwait(false); await cache.SetValueAsync("asd", new MemoryStream(updatedInput)).ConfigureAwait(false); var result = await cache.GetValueAsync("asd").ConfigureAwait(false); using (var reader = new BinaryReader(result)) { var resultBytes = reader.ReadBytes(4); var seqEqual = updatedInput.SequenceEqual(resultBytes); Assert.IsTrue(seqEqual); } } testDir.Delete(true); }
public static async Task ClearAsync_WhenValuePresent_RemovesAnyPresentValues() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "clearasync_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = new FifoCachePolicy <string>(); var input = new byte[] { 1, 2, 3, 4 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { await cache.SetValueAsync("asd", new MemoryStream(input)).ConfigureAwait(false); await cache.ClearAsync().ConfigureAwait(false); var result = await cache.ContainsKeyAsync("asd").ConfigureAwait(false); Assert.IsFalse(result); } testDir.Delete(true); }
public static async Task TryGetValueAsync_WhenValueExpired_ReturnsFalseAndNullStream() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "trygetvalueexpired_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = new FixedTimespanCachePolicy <string>(TimeSpan.FromMilliseconds(1)); var input = new byte[] { 1, 2, 3, 4 }; const ulong size = 20; using (var cache = new DiskCache <string>(testDir, cachePolicy, size, TimeSpan.FromMilliseconds(5))) { await cache.SetValueAsync("asd", new MemoryStream(input)).ConfigureAwait(false); await Task.Delay(100).ConfigureAwait(false); var(hasValue, stream) = await cache.TryGetValueAsync("asd").ConfigureAwait(false); Assert.IsFalse(hasValue); Assert.IsNull(stream); } testDir.Delete(true); }
public static void SetValueAsync_WhenGivenValueTooLarge_ThrowsArgException() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "setvalueasync_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); const ulong size = 2; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { Assert.ThrowsAsync <ArgumentException>(async() => await cache.SetValueAsync("asd", new MemoryStream(new byte[4])).ConfigureAwait(false)); } testDir.Delete(true); }
public static void SetValueAsync_WhenGivenUnreadableStream_ThrowsArgException() { var testDirPath = Path.Combine(Environment.CurrentDirectory, "setvalueasync_test"); var testDir = new DirectoryInfo(testDirPath); if (!testDir.Exists) { testDir.Create(); } var cachePolicy = Mock.Of <ICachePolicy <string> >(); var stream = new Mock <Stream>(); stream.Setup(s => s.CanRead).Returns(false); const ulong size = 123; using (var cache = new DiskCache <string>(testDir, cachePolicy, size)) { Assert.ThrowsAsync <ArgumentException>(async() => await cache.SetValueAsync("asd", stream.Object).ConfigureAwait(false)); } testDir.Delete(true); }