public async void SmokeTest() { var cancellationToken = CancellationToken.None; var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}"); Directory.CreateDirectory(path); var cacheOptions = new HybridCacheOptions(path) { AsyncCacheOptions = new AsyncCacheOptions() { MaxQueuedBytes = 0 } }; var database = new MetaStore.MetaStore(new MetaStoreOptions(path), cacheOptions, null); HybridCache cache = new HybridCache(database, cacheOptions, null); try { await cache.StartAsync(cancellationToken); var key = new byte[] { 0, 1, 2, 3 }; var contentType = "application/octet-stream"; Task <Tuple <string, ArraySegment <byte> > > DataProvider(CancellationToken token) { return(Task.FromResult(new Tuple <string, ArraySegment <byte> >(contentType, new ArraySegment <byte>(new byte[4000])))); } var result = await cache.GetOrCreateBytes(key, DataProvider, cancellationToken, true); Assert.Equal("WriteSucceeded", result.Status); var result2 = await cache.GetOrCreateBytes(key, DataProvider, cancellationToken, true); Assert.Equal("DiskHit", result2.Status); Assert.Equal(contentType, result2.ContentType); Assert.NotNull(result2.Data); await cache.AsyncCache.AwaitEnqueuedTasks(); var result3 = await cache.GetOrCreateBytes(key, DataProvider, cancellationToken, true); Assert.Equal("DiskHit", result3.Status); Assert.Equal(contentType, result3.ContentType); Assert.NotNull(result3.Data); var key2 = new byte[] { 2, 1, 2, 3 }; Task <Tuple <string, ArraySegment <byte> > > DataProvider2(CancellationToken token) { return(Task.FromResult(new Tuple <string, ArraySegment <byte> >(null, new ArraySegment <byte>(new byte[4000])))); } var result4 = await cache.GetOrCreateBytes(key2, DataProvider2, cancellationToken, true); Assert.Equal("WriteSucceeded", result4.Status); var result5 = await cache.GetOrCreateBytes(key2, DataProvider, cancellationToken, true); Assert.Equal("DiskHit", result5.Status); Assert.Null(result5.ContentType); Assert.NotNull(result5.Data); } finally { try { await cache.StopAsync(cancellationToken); } finally { Directory.Delete(path, true); } } }
private static async Task TestRandom(TestParams options, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { throw new OperationCanceledException(cancellationToken); } var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}"); Directory.CreateDirectory(path); Console.WriteLine($"Created cache directory {path}"); try { options.CacheOptions.PhysicalCacheDir = path; options.MetaStoreOptions.DatabaseDir = path; for (var reboot = 0; reboot < options.RebootCount; reboot++) { Console.WriteLine($"------------- Cache Reboot {reboot} ---------------"); var loggerFactory = TestLoggerFactory.Create(); var logger = loggerFactory.CreateLogger <HybridCache>(); ICacheDatabase database = new MetaStore.MetaStore(options.MetaStoreOptions, options.CacheOptions, logger); HybridCache cache = new HybridCache(database, options.CacheOptions, logger); try { Console.Write("Starting cache..."); var swStart = Stopwatch.StartNew(); await cache.StartAsync(cancellationToken); swStart.Stop(); Console.Write($"ready in {swStart.Elapsed}\r\n"); await TestRandomInner(cache, options, loggerFactory, cancellationToken); if (options.DisplayLog) { var logs = loggerFactory.Sink.LogEntries.ToArray(); int firstLogIndex = logs.Length - Math.Min(options.MaxLogEntries, logs.Length); int lastLogIndex = Math.Min(firstLogIndex, options.MaxLogEntries); if (lastLogIndex > 0) { Console.WriteLine($"========== LOG ENTRIES 0..{lastLogIndex} ==============="); } for (var ix = 0; ix < lastLogIndex; ix++) { Console.WriteLine(logs[ix].Message); } Console.WriteLine($"========== LOG ENTRIES {firstLogIndex}..{logs.Length} ==============="); for (var ix = firstLogIndex; ix < logs.Length; ix++) { Console.WriteLine(logs[ix].Message); } Console.WriteLine("============== END LOGS ==============="); } } finally { Console.Write("Stopping cache..."); var swStop = Stopwatch.StartNew(); await cache.StopAsync(cancellationToken); swStop.Stop(); Console.Write($"stopped in {swStop.Elapsed}\r\n"); } } if (options.WaitForKeypress) { Console.WriteLine("Press the any key to continue"); Console.ReadKey(); } } finally { Console.WriteLine("Deleting cache from disk..."); Directory.Delete(path, true); Console.WriteLine("Cache deleted"); } }