Beispiel #1
0
        public async Task GetDefaultWhenKeyDoesNotExist()
        {
            IDistributedCache cache = new MemoryDistributedCache();

            var foo = await cache.GetAsync <Foo>("foo");

            foo.Should().BeNull();

            var intValue = await cache.GetAsync <int?>("int");

            intValue.Should().BeNull();

            var stringValue = await cache.GetAsync <string>("string");

            stringValue.Should().BeNull();
        }
Beispiel #2
0
        public async Task SetValueToCache()
        {
            IDistributedCache cache = new MemoryDistributedCache();

            await cache.SetAsync("2", 2, new DistributedCacheEntryOptions());

            var value = await cache.GetAsync <int>("2");

            value.Should().Be(2);
        }
        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);
        }
Beispiel #4
0
 public Task <byte[]> GetAsync(string key, CancellationToken token = default)
 {
     if (DisableGet)
     {
         throw new InvalidOperationException();
     }
     if (DelayGetAsync)
     {
         token.WaitHandle.WaitOne(TimeSpan.FromSeconds(10));
         token.ThrowIfCancellationRequested();
     }
     return(_cache.GetAsync(key, token));
 }
Beispiel #5
0
        public async Task SetComplexValueToCache()
        {
            IDistributedCache cache = new MemoryDistributedCache();

            await cache.SetAsync("foo", new Foo()
            {
                Value = 2
            }, new DistributedCacheEntryOptions());

            var value = await cache.GetAsync <Foo>("foo");

            value.Value.Should().Be(2);
        }
        public async Task SetAsyncSerializesObjectAndStoresBytes()
        {
            var cache   = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
            var example = new Example
            {
                Name = "The Name"
            };

            await cache.SetAsync("test", example);

            var bytes = await cache.GetAsync("test");

            var json = Encoding.UTF8.GetString(bytes);

            Assert.Equal(@"{""Name"":""The Name""}", json);
        }
Beispiel #7
0
        public MessageProcessorTests()
        {
            delivererMock = new Mock <IMailDeliverer>();

            delivererMock
            .Setup(d => d.DeliverAsync(It.Is <EmailMessage>(m => m.Subject == DELIVER_FAILURE)))
            .ThrowsAsync(new Exception());

            storageMock = new Mock <IMailStorage>();

            storageMock
            .Setup(s => s.SetProcessedAsync(It.Is <EmailMessage>(m => m.Subject == SET_PROCESSED_FAILURE)))
            .ThrowsAsync(new Exception());

            storageMock
            .Setup(s => s.SetErrorAsync(It.Is <EmailMessage>(m => m.Subject == SET_ERROR_FAILURE), It.IsAny <string>()))
            .ThrowsAsync(new Exception());

            storageMock
            .Setup(s => s.SetSentAsync(It.Is <EmailMessage>(m => m.Subject == SET_SENT_FAILURE)))
            .ThrowsAsync(new Exception());

            cacheMock = new Mock <IDistributedCache>();

            var internalCache = new MemoryDistributedCache(Options.Create <MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions()));

            cacheMock
            .Setup(c => c.GetAsync(It.Is <string>(m => m == CACHE_GET_EXCEPTION.ToString()), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception());

            cacheMock
            .Setup(c => c.GetAsync(It.Is <string>(m => m != CACHE_GET_EXCEPTION.ToString()), It.IsAny <CancellationToken>()))
            .Returns(async(string key, CancellationToken c) => await internalCache.GetAsync(key, c));

            cacheMock
            .Setup(c => c.SetAsync(It.Is <string>(m => m == CACHE_SET_EXCEPTION.ToString()), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>()))
            .ThrowsAsync(new Exception());

            cacheMock
            .Setup(c => c.SetAsync(It.Is <string>(m => m != CACHE_SET_EXCEPTION.ToString()), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>()))
            .Returns(async(string key, byte[] value, DistributedCacheEntryOptions o, CancellationToken c) => await internalCache.SetAsync(key, value, o, c));

            var logger = new NullLogger <MessageProcessor>();

            messageProcessor = new MessageProcessor(delivererMock.Object, storageMock.Object, cacheMock.Object, logger);
        }
        public async Task SetAsyncUsesExpiryPolicy()
        {
            var fixedTime = new DateTimeOffset(2020, 1, 27, 21, 20, 0, TimeSpan.Zero);
            var mockTime  = Substitute.For <ISystemClock>();

            mockTime.UtcNow.Returns(fixedTime);
            var options = new MemoryDistributedCacheOptions
            {
                Clock = mockTime
            };
            var cache             = new MemoryDistributedCache(Options.Create(options));
            var expirationOptions = new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = fixedTime.Subtract(TimeSpan.FromSeconds(1))
            };
            await cache.SetAsync("should_be_gone", new Example { Name = "to expire" }, expirationOptions);

            var missing = await cache.GetAsync <Example>("should_be_gone");

            Assert.Null(missing);
        }
Beispiel #9
0
 public Task <byte[]> GetAsync(string key, CancellationToken token = default(CancellationToken)) => _cache.GetAsync(key);
Beispiel #10
0
 public Task <byte[]> GetAsync(string key) => _cache.GetAsync(key);