public async Task RoundTripIntegrationTest() { var cache = new InMemoryBlobCache(); var cachingHandler = new RateLimitedHttpMessageHandler(new HttpClientHandler(), Priority.UserInitiated, cacheResultFunc: async (rq, resp, key, ct) => { var data = await resp.Content.ReadAsByteArrayAsync(); await cache.Insert(key, data); }); var client = new HttpClient(cachingHandler); var origData = await client.GetStringAsync("http://httpbin.org/get"); Assert.True(origData.Contains("origin")); Assert.Equal(1, (await cache.GetAllKeys()).Count()); var offlineHandler = new OfflineHttpMessageHandler(async (rq, key, ct) => { return await cache.Get(key); }); client = new HttpClient(offlineHandler); var newData = await client.GetStringAsync("http://httpbin.org/get"); Assert.Equal(origData, newData); bool shouldDie = true; try { await client.GetStringAsync("http://httpbin.org/gzip"); } catch (Exception ex) { shouldDie = false; Console.WriteLine(ex); } Assert.False(shouldDie); }
public async Task RoundTripIntegrationTest() { var cache = new InMemoryBlobCache(); var cachingHandler = new RateLimitedHttpMessageHandler(new HttpClientHandler(), Priority.UserInitiated, cacheResultFunc: async(rq, resp, key, ct) => { var data = await resp.Content.ReadAsByteArrayAsync(); await cache.Insert(key, data); }); var client = new HttpClient(cachingHandler); var origData = await client.GetStringAsync("http://httpbin.org/get"); Assert.True(origData.Contains("origin")); Assert.Equal(1, (await cache.GetAllKeys()).Count()); var offlineHandler = new OfflineHttpMessageHandler(async(rq, key, ct) => { return(await cache.Get(key)); }); client = new HttpClient(offlineHandler); var newData = await client.GetStringAsync("http://httpbin.org/get"); Assert.Equal(origData, newData); bool shouldDie = true; try { await client.GetStringAsync("http://httpbin.org/gzip"); } catch (Exception ex) { shouldDie = false; Console.WriteLine(ex); } Assert.False(shouldDie); }
public async Task RemovesImageFromCache() { var singlePixel = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=="); var cache = new InMemoryBlobCache(); await cache.Insert("https://fake/", singlePixel); var cacheFactory = Substitute.For <IBlobCacheFactory>(); cacheFactory.CreateBlobCache(Args.String).Returns(cache); var imageCache = new ImageCache(cacheFactory, Substitute.For <IEnvironment>(), new Lazy <IImageDownloader>(() => Substitute.For <IImageDownloader>())); await imageCache.Invalidate(new Uri("https://fake/")); Assert.ThrowsAsync <KeyNotFoundException>(async() => await cache.Get("https://fake/")); }
public void CanStoreMulipleArtworksIfArtworksExistsInCache() { var blobCache = new InMemoryBlobCache(); var data = new byte[] { 0, 1 }; string key = BlobCacheKeys.GetKeyForArtwork(data); blobCache.Insert(key, data); var fixture = new ArtworkCache(blobCache); Task firstTask = fixture.Store(key, data); Task secondTask = fixture.Store(key, data); Task thrirdTask = fixture.Store(key, data); Assert.True(firstTask.IsCompleted); Assert.True(secondTask.IsCompleted); Assert.True(thrirdTask.IsCompleted); }
public async Task RetrievesImageFromCacheAndDoesNotFetchIt() { var singlePixel = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=="); var cache = new InMemoryBlobCache(); await cache.Insert("https://fake/", singlePixel); var cacheFactory = Substitute.For<IBlobCacheFactory>(); cacheFactory.CreateBlobCache(Args.String).Returns(cache); var imageDownloader = Substitute.For<IImageDownloader>(); imageDownloader.DownloadImageBytes(Args.Uri).Returns(_ => { throw new InvalidOperationException(); }); var imageCache = new ImageCache(cacheFactory, Substitute.For<IEnvironment>(), new Lazy<IImageDownloader>(() => imageDownloader)); var retrieved = await imageCache.GetImage(new Uri("https://fake/")).FirstAsync(); Assert.NotNull(retrieved); Assert.Equal(32, retrieved.PixelWidth); Assert.Equal(32, retrieved.PixelHeight); }
public async Task WhenLoadingFromCacheFailsInvalidatesCacheEntry() { var cache = new InMemoryBlobCache(); await cache.Insert("https://fake/", new byte[] { 0, 0, 0 }); var cacheFactory = Substitute.For<IBlobCacheFactory>(); cacheFactory.CreateBlobCache(Args.String).Returns(cache); var imageDownloader = Substitute.For<IImageDownloader>(); imageDownloader.DownloadImageBytes(Args.Uri).Returns(_ => { throw new InvalidOperationException(); }); var imageCache = new ImageCache(cacheFactory, Substitute.For<IEnvironment>(), new Lazy<IImageDownloader>(() => imageDownloader)); var retrieved = await imageCache .GetImage(new Uri("https://fake/")) .Catch(Observable.Return<BitmapSource>(null)) .FirstAsync(); Assert.Null(retrieved); await Assert.ThrowsAsync<KeyNotFoundException>(async () => await cache.Get("https://fake/")); }
public async Task RetrievesImageFromCacheAndDoesNotFetchIt() { var singlePixel = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=="); var cache = new InMemoryBlobCache(); await cache.Insert("https://fake/", singlePixel); var cacheFactory = Substitute.For <IBlobCacheFactory>(); cacheFactory.CreateBlobCache(Args.String).Returns(cache); var imageDownloader = Substitute.For <IImageDownloader>(); imageDownloader.DownloadImageBytes(Args.Uri).Returns(_ => { throw new InvalidOperationException(); }); var imageCache = new ImageCache(cacheFactory, Substitute.For <IEnvironment>(), new Lazy <IImageDownloader>(() => imageDownloader)); var retrieved = await imageCache.GetImage(new Uri("https://fake/")).FirstAsync(); Assert.NotNull(retrieved); Assert.That(32, Is.EqualTo(retrieved.PixelWidth)); Assert.That(32, Is.EqualTo(retrieved.PixelHeight)); }
public async Task WhenLoadingFromCacheFailsInvalidatesCacheEntry() { var cache = new InMemoryBlobCache(); await cache.Insert("https://fake/", new byte[] { 0, 0, 0 }); var cacheFactory = Substitute.For <IBlobCacheFactory>(); cacheFactory.CreateBlobCache(Args.String).Returns(cache); var imageDownloader = Substitute.For <IImageDownloader>(); imageDownloader.DownloadImageBytes(Args.Uri).Returns(_ => { throw new InvalidOperationException(); }); var imageCache = new ImageCache(cacheFactory, Substitute.For <IEnvironment>(), new Lazy <IImageDownloader>(() => imageDownloader)); var retrieved = await imageCache .GetImage(new Uri("https://fake/")) .Catch(Observable.Return <BitmapSource>(null)) .FirstAsync(); Assert.That(retrieved, Is.Null); Assert.ThrowsAsync <KeyNotFoundException>(async() => await cache.Get("https://fake/")); }
public async Task RemovesImageFromCache() { var singlePixel = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=="); var cache = new InMemoryBlobCache(); await cache.Insert("https://fake/", singlePixel); var cacheFactory = Substitute.For<IBlobCacheFactory>(); cacheFactory.CreateBlobCache(Args.String).Returns(cache); var imageCache = new ImageCache(cacheFactory, Substitute.For<IEnvironment>(), new Lazy<IImageDownloader>(() => Substitute.For<IImageDownloader>())); await imageCache.Invalidate(new Uri("https://fake/")); await Assert.ThrowsAsync<KeyNotFoundException>(async () => await cache.Get("https://fake/")); }