Ejemplo n.º 1
0
        public async Task Should_be_valid_when_job_is_requested_to_clear_cache()
        {
            await _cache.SetAsync(CacheKeys.Portfolio, Array.Empty <byte>());

            _updateCacheJob = new UpdateCacheJob(_cache, _mockPortfolio.Object);
            await _updateCacheJob.ClearAsync();

            _cache.Get(CacheKeys.Portfolio).Should().BeNull();
        }
Ejemplo n.º 2
0
        public void GivenCustomerWhenSaveCustomerThenSaveToDatabase()
        {
            // Arrange
            var cacheOption = Options.Create(new MemoryDistributedCacheOptions());
            var cache       = new MemoryDistributedCache(cacheOption);
            var options     = new DbContextOptionsBuilder <CustomerDbContext>()
                              .UseInMemoryDatabase(databaseName: "customerdatabse")
                              .Options;
            var expect = new Customer
            {
                Age        = 35,
                City       = "Chennai",
                Country    = "India",
                Name       = "Mark",
                State      = "Tamil Nadu",
                CustomerId = Guid.NewGuid(),
            };
            var customerDbContext    = new CustomerDbContext(options);
            var customerRedisContext = new CustomerRedisContext(cache, customerDbContext);

            // Act
            var response       = customerRedisContext.SetCustomer(expect);
            var cachedCustomer = cache.Get <Customer>($"{expect.CustomerId}");

            // Assert
            Assert.True(response);
            Assert.Equal(cachedCustomer.Country, expect.Country);
        }
Ejemplo n.º 3
0
 public byte[] Get(string key)
 {
     if (DisableGet)
     {
         throw new InvalidOperationException();
     }
     return(_cache.Get(key));
 }
        public void SetInDistributionCache_IfValidFormFileRequestNotCached()
        {
            // Arrange

            // Prepare the body and headers for the Request and Response:
            const string IDEMPOTENCY_KEY     = "b8fcc234-e1bd-11e9-81b4-2a2ae2dbcce4";
            string       distributedCacheKey = _distributedCacheKeysPrefix + IDEMPOTENCY_KEY;
            string       httpMethod          = "POST";
            FormFile     requestBodyFile     = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
            var          requestHeaders      = new HeaderDictionary();

            requestHeaders.Add(_headerKeyName, IDEMPOTENCY_KEY);

            var controllerExecutionResult = new OkObjectResult(new ResponseModelBasic()
            {
                Id = 1, CreatedOn = new DateTime(2019, 10, 12, 5, 25, 25)
            });
            var responseHeaders = new HeaderDictionary();

            var actionContext          = ArrangeActionContextMock(httpMethod, requestHeaders, requestBodyFile, responseHeaders, controllerExecutionResult);
            var actionExecutingContext = new ActionExecutingContext(
                actionContext,
                new List <IFilterMetadata>(),
                new Dictionary <string, object>(),
                Mock.Of <Controller>()
                );

            var resultExecutedContext = new ResultExecutedContext(
                actionContext,
                new List <IFilterMetadata>(),
                controllerExecutionResult,
                Mock.Of <Controller>());


            var distributedCache           = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
            var idempotencyAttributeFilter = new IdempotencyAttributeFilter(distributedCache, _loggerFactory, true, 1, _headerKeyName, _distributedCacheKeysPrefix);


            // Act Part 1 (check cache):
            idempotencyAttributeFilter.OnActionExecuting(actionExecutingContext);

            // Assert Part 1:
            Assert.Null(actionExecutingContext.Result);

            // Act Part 2:
            idempotencyAttributeFilter.OnResultExecuted(resultExecutedContext);

            // Assert Part 2:
            byte[] cachedData = distributedCache.Get(distributedCacheKey);
            Assert.NotNull(cachedData);
        }
Ejemplo n.º 5
0
        public void ExampleTestMethod()
        {
            var expectedData = new byte[] { 100, 200 };

            var opts = Options.Create <MemoryDistributedCacheOptions>(new MemoryDistributedCacheOptions());
            IDistributedCache cache = new MemoryDistributedCache(opts);

            cache.Set("key1", expectedData);
            var cachedData = cache.Get("key1");

            Assert.AreEqual(expectedData, cachedData);

            //Use the variable cache as an input to any class which expects IDistributedCache
        }