コード例 #1
0
        public async Task SetAndGetAsync_SetAndGetObject_SetsObjectAndReturnsExpectedObject()
        {
            // arrange
            var serviceCollection = new ServiceCollection();

            Setup(serviceCollection);
            var logger = new Mock <IAppLogger <AppCache> >();

            serviceCollection.AddTransient(s => logger.Object);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            serviceProvider.TightenBolts();
            var sut     = serviceProvider.GetService <IAppCache>();
            var student = new Fakes.Student {
                FirstName = "first", Id = Guid.NewGuid()
            };

            // act
            await sut.SetAsync("StudentId", student);

            var result = await sut.GetAsync <Fakes.Student>("StudentId");

            // assert
            Assert.NotNull(result);
            Assert.Equal(student.Id, result.Id);
            Assert.Equal(student.FirstName, result.FirstName);
            logger.Verify(l => l.Debug("Setting value in cache... Key: StudentId"));

            // cleanup
            await sut.RemoveAsync("StudentId");
        }
コード例 #2
0
        public async Task SetAndGet_SetWithExpiryIntervalAndWhenRetrievedBeforeAndAfterExpiryInterval_IsSuccessful()
        {
            // arrange
            var serviceCollection = new ServiceCollection();

            Setup(serviceCollection);
            var logger = new Mock <IAppLogger <AppCache> >();

            serviceCollection.AddTransient(s => logger.Object);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            serviceProvider.TightenBolts();
            var sut     = serviceProvider.GetService <IAppCache>();
            var student = new Fakes.Student {
                FirstName = "first", Id = Guid.NewGuid()
            };
            const int expirationInterval = 2000;

            // act
            await sut.SetAsync("Student", student, absoluteExpiration : TimeSpan.FromMilliseconds(expirationInterval));

            var result = await sut.GetAsync <Fakes.Student>("Student");

            // assert
            Assert.NotNull(result);
            Assert.Equal(student.Id, result.Id);
            await Task.Delay(500);

            await sut.GetAsync <Fakes.Student>("Student");

            await Task.Delay(expirationInterval - 200);

            logger.Verify(l => l.Debug("Setting value in cache... Key: Student"));
            var result2 = await sut.GetAsync <Fakes.Student>("Student");

            Assert.Null(result2);

            // cleanup
            await sut.RemoveAsync("Student");
        }