コード例 #1
0
        public async Task GetAll_WhenAnEntityOfDifferentTypeAreAdded_ThenAllCanBeRetrieved()
        {
            // arrange
            var type1Entities = new[]
            { new TestCompany {
                  Name = "test1_1", Id = "1"
              }, new TestCompany {
                  Name = "test1_2", Id = "2"
              } };
            var type2Entities = new[]
            { new TestPerson {
                  FirstName = "test2_1", Id = "1"
              }, new TestPerson {
                  FirstName = "test2_2", Id = "2"
              } };

            await _cacheType1.AddOrUpdateAsync(type1Entities.First());

            await _cacheType1.AddOrUpdateAsync(type1Entities.Last());

            await _cacheType2.AddOrUpdateAsync(type2Entities.First());

            await _cacheType2.AddOrUpdateAsync(type2Entities.Last());

            // act
            var retrievedAllType1Entities = (await _cacheType1.GetAllAsync()).ToArray();
            var retrievedAllType2Entities = (await _cacheType2.GetAllAsync()).ToArray();

            // assert
            Assert.That(retrievedAllType1Entities, Has.Length.EqualTo(2));
            Assert.That(retrievedAllType2Entities, Has.Length.EqualTo(2));
            Assert.That(retrievedAllType1Entities, Is.EquivalentTo(type1Entities));
            Assert.That(retrievedAllType2Entities, Is.EquivalentTo(type2Entities));
        }
コード例 #2
0
        public void AccessingLocalRedis100TimesLessThanASecond()
        {
            var c = new RedisStore(ConnectionString);

            var sw = Stopwatch.StartNew();

            for (int i = 0; i < 100; i++)
            {
                var buffer = new byte[_r.Next(4 * 1024, 64 * 1024)];
                _r.NextBytes(buffer);
                var key  = new CacheKey(UrlStem + Guid.NewGuid().ToString(), new string[0]);
                var task = c.AddOrUpdateAsync(key,
                                              new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(buffer)
                });
                task.GetAwaiter().GetResult();
                var r = c.GetValueAsync(key).GetAwaiter().GetResult();
            }

            if (sw.ElapsedMilliseconds > 1000)
            {
                throw new Exception("Took more than 1000 ms");
            }
        }
コード例 #3
0
        public async Task AddOrUpdate_WhenAnEntityIsAdded_ThenItCanBeRetrieved()
        {
            // arrange
            var entity = TestCompany.Apple;

            // act
            await _cacheType1.AddOrUpdateAsync(entity);

            var retrievedEntity = await _cacheType1.GetAsync(entity.Id);

            // assert
            Assert.That(retrievedEntity, Is.EqualTo(entity));
        }
コード例 #4
0
        public async Task FlushAll_WhenStoreGotSomeKeys_ShouldLeaveStoreEmpty()
        {
            // arrange
            await Task.WhenAll(
                _cache1.AddOrUpdateAsync(new TestCompany {
                Id = "a"
            }),
                _cache2.AddOrUpdateAsync(new TestPerson {
                Id = "b"
            })
                );

            // act
            await _databaseManager.FlushAll();

            // assert
            var allkeys = _databaseManager.ScanKeys().ToList();

            Assert.That(allkeys, Is.Empty);
        }
コード例 #5
0
        public void IfNotThrowThenDoesNot()
        {
            var s = new RedisStore("NoneExisting", throwExceptions: false);
            var k = new CacheKey("https://google.com/", new string[0]);
            var r = new HttpResponseMessage(HttpStatusCode.Accepted);

            s.AddOrUpdateAsync(k, r).Wait();
            var r2      = s.GetValueAsync(k).Result;
            var removed = s.TryRemoveAsync(k).Result;

            Assert.Null(r2);
            Assert.False(removed);
        }
コード例 #6
0
        public async Task AddOrUpdate_WhenAnEntityIsAddedWithExpiryOf1Second_ThenItCannotBeRetrieved()
        {
            // arrange
            var entity = TestCompany.Apple;

            // act
            await _cacheType1WithExpiry.AddOrUpdateAsync(entity);

            var retrievedEntityBeforeExpiry = await _cacheType1WithExpiry.GetAsync(entity.Id);

            await Task.Delay(1500);

            var retrievedEntityAfterExpiry = await _cacheType1WithExpiry.GetAsync(entity.Id);

            // assert
            Assert.That(retrievedEntityBeforeExpiry, Is.EqualTo(entity));
            Assert.That(retrievedEntityAfterExpiry, Is.Null);
        }