Ejemplo n.º 1
0
        public void Get_String_ShouldReturnCacheResult()
        {
            _memoryDistributedCache.SetString(Key, JsonConvert.SerializeObject(Value));

            var cached = _distributedCacheProvider.Get <string>(Key);

            cached.Success.ShouldBeTrue();
            cached.Content.ShouldBe(Value);
        }
        public async Task GetAsyncDeserializesBytesIntoUtf8()
        {
            var cache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));

            cache.SetString("test", @"{""Name"":""Geoffrey""}");
            var subject = await cache.GetAsync <Example>("test");

            Assert.IsType <Example>(subject);
            Assert.Equal("Geoffrey", subject.Name);
        }
        public void Remove_FunctionWithoutParameters_ShouldBeRemoved()
        {
            var key = _distributedCacheService.GetKey(() => _testFunctions.FunctionWithoutParameters());

            _memoryDistributedCache.SetString(key, JsonConvert.SerializeObject(Value));
            var beforeRemove = _memoryDistributedCache.GetString(key);

            _distributedCacheService.Remove(() => _testFunctions.FunctionWithoutParameters());
            var afterRemove = _memoryDistributedCache.GetString(key);

            beforeRemove.Should().NotBeNull();
            afterRemove.Should().BeNull();
        }
        public async Task Load_WhenTogglesOrEtagFileDoesNotExists_ReturnsEmptyResult(
            bool toggleCollectionExists,
            bool etagExists,
            [Frozen] DistributedToggleCollectionCacheSettings settings,
            [Frozen] MemoryDistributedCache distributedCache,
            DistributedToggleCollectionCache cache
            )
        {
            var jsonSerializerSettings = new NewtonsoftJsonSerializerSettings();
            var jsonSerializer         = new NewtonsoftJsonSerializer(jsonSerializerSettings);

            settings.EtagKeyName             = "Etag";
            settings.ToggleCollectionKeyName = "Toggles";

            if (toggleCollectionExists)
            {
                var toggleCollection = new ToggleCollection();

                using (var ms = new MemoryStream())
                {
                    jsonSerializer.Serialize(ms, toggleCollection);
                    ms.Seek(0, SeekOrigin.Begin);

                    await distributedCache.SetAsync(settings.ToggleCollectionKeyName, ms.ToArray(), settings.ToggleCollectionEntryOptions, CancellationToken.None).ConfigureAwait(false);
                }
            }

            if (toggleCollectionExists && etagExists)
            {
                var etag = Guid.NewGuid().ToString();
                distributedCache.SetString(settings.EtagKeyName, etag);
            }

            var result = await cache.Load(CancellationToken.None);

            Assert.Equal(string.Empty, result.InitialETag);
            Assert.Null(result.InitialToggleCollection);
        }