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);
        }
Пример #3
0
            public async Task StoresArtworkInBlobCache()
            {
                var blobCache    = new InMemoryBlobCache();
                var artworkCache = new ArtworkCache(blobCache);

                var    data = new byte[] { 0, 1 };
                string key  = BlobCacheKeys.GetKeyForArtwork(data);

                await artworkCache.Store(key, data);

                Assert.Equal(data, await blobCache.Get(key));
            }
        public async Task AddsImageDirectlyToCache()
        {
            var singlePixel  = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==");
            var cache        = new InMemoryBlobCache();
            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.SeedImage(new Uri("https://fake/"), singlePixel, DateTimeOffset.MaxValue);

            var retrieved = await cache.Get("https://fake/");

            Assert.That(singlePixel, Is.EqualTo(retrieved));
        }
        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 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/"));
        }
Пример #7
0
        public async Task InitializeAsyncLoadsValuesIntoCache()
        {
            var testCache = new InMemoryBlobCache();
            await testCache.InsertObject("Storage:DummyNumber", 16);
            await testCache.InsertObject("Storage:DummyText", "Random");

            var cache = Substitute.For<IBlobCache>();
            cache.Get(Arg.Any<string>()).Returns(x => testCache.Get(x.Arg<string>()));
            var settings = new DummySettingsStorage("Storage", cache);

            await settings.InitializeAsync();

            int number = settings.DummyNumber;
            string text = settings.DummyText;

            Assert.Equal(16, number);
            Assert.Equal("Random", text);
            cache.ReceivedWithAnyArgs(2).Get(Arg.Any<string>());
        }
        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/"));
        }
Пример #9
0
        public async Task InitializeAsyncLoadsValuesIntoCache()
        {
            var testCache = new InMemoryBlobCache();
            await testCache.InsertObject("Storage:DummyNumber", 16);

            await testCache.InsertObject("Storage:DummyText", "Random");

            var cache = Substitute.For <IBlobCache>();

            cache.Get(Arg.Any <string>()).Returns(x => testCache.Get(x.Arg <string>()));
            var settings = new DummySettingsStorage("Storage", cache);

            await settings.InitializeAsync();

            int    number = settings.DummyNumber;
            string text   = settings.DummyText;

            Assert.Equal(16, number);
            Assert.Equal("Random", text);
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            cache.ReceivedWithAnyArgs(2).Get(Arg.Any <string>());
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }
Пример #10
0
            public async Task StoresArtworkInBlobCache()
            {
                var blobCache = new InMemoryBlobCache();
                var artworkCache = new ArtworkCache(blobCache);

                var data = new byte[] { 0, 1 };
                string key = BlobCacheKeys.GetKeyForArtwork(data);

                await artworkCache.Store(key, data);

                Assert.Equal(data, await blobCache.Get(key));
            }
Пример #11
0
        public async Task AddsImageDirectlyToCache()
        {
            var singlePixel = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==");
            var cache = new InMemoryBlobCache();
            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.SeedImage(new Uri("https://fake/"), singlePixel, DateTimeOffset.MaxValue);

            var retrieved = await cache.Get("https://fake/");
            Assert.Equal(singlePixel, retrieved);
        }
Пример #12
0
        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/"));
        }