예제 #1
0
        // ReSharper disable once TooManyDeclarations
        public async void CachingBehavior_CallHandlerWithCacheableRequest_ReturnCachedObject()
        {
            // Arrange
            var cacheableRequest = new FooRequestCacheable(TimeSpan.FromMilliseconds(10));
            var cachedObjectTime = DateTime.UtcNow;
            var actual           = new RefTypeObject
            {
                Foo     = 1,
                Bar     = "bar",
                Created = cachedObjectTime
            };
            var sut = new CachingBehavior <FooRequestCacheable, RefTypeObject>(_cache, _loggerFactory);

            // Act

            // call delegate, cache and return result
            await sut.Handle(cacheableRequest, CancellationToken.None,
                             async() => await Task.FromResult(actual));

            // waiting to spend some time
            await Task.Delay(5);

            // return cached result, even-though wrong result expected from delegate
            var expected = await sut.Handle(cacheableRequest, CancellationToken.None,
                                            async() => await Task.FromResult(new RefTypeObject()));

            // Assert
            Assert.Equal(expected.Created, cachedObjectTime);
        }
예제 #2
0
		public Task<IHalHttpClient> CreateClientAsync(HttpMessageHandler httpMessageHandler, CachingBehavior apiRootCachingBehavior = CachingBehavior.Never)
		{
			if (httpMessageHandler == null)
				throw new ArgumentNullException(nameof(httpMessageHandler));

			return CreateHalHttpClientAsync(GetHttpClient(httpMessageHandler), apiRootCachingBehavior);
		}
예제 #3
0
        public async void CachingBehavior_CallHandlerWithNormalRequest_ReturnExpectedObject()
        {
            //Given
            var request = new FooRequest();
            var actual  = new RefTypeObject
            {
                Foo     = 1,
                Bar     = "bar",
                Created = DateTime.UtcNow
            };
            var sut = new CachingBehavior <FooRequest, RefTypeObject>(_cache, _loggerFactory);


            //When
            var expected = await sut.Handle(request, CancellationToken.None,
                                            async() => await Task.FromResult(actual));

            //Then
            Assert.Equal(expected, actual);
        }
예제 #4
0
        public Task <IHalHttpClient> CreateClientAsync(HttpClient httpClient, T context, CachingBehavior apiRootCachingBehavior = CachingBehavior.Never)
        {
            if (httpClient == null)
            {
                throw new ArgumentNullException(nameof(httpClient));
            }

            return(CreateHalHttpClientAsync(httpClient, context, apiRootCachingBehavior));
        }
예제 #5
0
 public Task <IHalHttpClient> CreateClientAsync(T context, CachingBehavior apiRootCachingBehavior = CachingBehavior.Never)
 {
     return(CreateHalHttpClientAsync(GetHttpClient(), context, apiRootCachingBehavior));
 }
예제 #6
0
		private async Task<IHalHttpClient> CreateHalHttpClientAsync(HttpClient httpClient, CachingBehavior apiRootCachingBehavior)
		{
			var wrapped = new HalHttpClient(Parser, httpClient);

			try
			{
				Configure(wrapped.Configuration);

				var decorated = Decorate(wrapped) ?? wrapped;

				switch (apiRootCachingBehavior)
				{
					case CachingBehavior.Never:
						break;
					case CachingBehavior.PerClient:
						var apiRootResource = await GetFreshRootResourceAsync(decorated, wrapped.Configuration).ConfigureAwait(false);
						wrapped.CachedApiRootResource = apiRootResource;
						break;
					case CachingBehavior.Once:
						_cachedApiRootResource = _cachedApiRootResource ?? await GetFreshRootResourceAsync(decorated, wrapped.Configuration).ConfigureAwait(false);
						wrapped.CachedApiRootResource = _cachedApiRootResource;
						break;
					default:
						throw new ArgumentOutOfRangeException(nameof(apiRootCachingBehavior), apiRootCachingBehavior, null);
				}

				return decorated;
			}
			catch (Exception)
			{
				wrapped.Dispose(); // client is unusable ...
				throw;
			}
		}
예제 #7
0
		public Task<IHalHttpClient> CreateClientAsync(HttpClient httpClient, CachingBehavior apiRootCachingBehavior = CachingBehavior.Never)
		{
			return CreateHalHttpClientAsync(httpClient, apiRootCachingBehavior);
		}
예제 #8
0
        public void CachingBehaviorInvokeDelegateNullTest()
        {
            var target = new CachingBehavior(_cachingProvider, _logger);

            target.Invoke(new Mock <IMethodInvocation>().Object, null);
        }
 public Task <IHalHttpClient> CreateClientAsync(HttpClient httpClient, CachingBehavior apiRootCachingBehavior = CachingBehavior.Never)
 {
     return(CreateHalHttpClientAsync(httpClient, apiRootCachingBehavior));
 }
예제 #10
0
 public CachingAttribute(string cacheManagerProviderServiceName, CachingBehavior behavior, params string[] methodNames)
 {
     Behavior = behavior;
     CacheManagerProviderServiceName = cacheManagerProviderServiceName;
     MethodNames = methodNames;
 }
예제 #11
0
 public CachingAttribute(string cacheManagerProviderServiceName, CachingBehavior behavior)
     : this(cacheManagerProviderServiceName, behavior, (string[])null)
 {
 }
예제 #12
0
 public void CachingBehaviorConstructorProfilerNullTest()
 {
     var target = new CachingBehavior(null, _logger);
 }
예제 #13
0
 public void CachingBehaviorConstructorLoggerNullTest()
 {
     var target = new CachingBehavior(_cachingProvider, null);
 }
예제 #14
0
        public void CachingBehaviorConstructorTest()
        {
            var target = new CachingBehavior(_cachingProvider, _logger);

            Assert.IsNotNull(target);
        }
 public Task <IHalHttpClient> CreateClientAsync(CachingBehavior apiRootCachingBehavior)
 {
     return(CreateHalHttpClientAsync(GetHttpClient(), apiRootCachingBehavior));
 }
        public Task <IHalHttpClient> CreateClientAsync(HttpMessageHandler httpMessageHandler, CachingBehavior apiRootCachingBehavior = CachingBehavior.Never)
        {
            if (httpMessageHandler == null)
            {
                throw new ArgumentNullException(nameof(httpMessageHandler));
            }

            return(CreateHalHttpClientAsync(GetHttpClient(httpMessageHandler), apiRootCachingBehavior));
        }
예제 #17
0
		public Task<IHalHttpClient> CreateClientAsync(CachingBehavior apiRootCachingBehavior)
		{
			return CreateHalHttpClientAsync(GetHttpClient(), apiRootCachingBehavior);
		}
        private async Task <IHalHttpClient> CreateHalHttpClientAsync(HttpClient httpClient, CachingBehavior apiRootCachingBehavior)
        {
            var wrapped = new HalHttpClient(HalJsonParser, httpClient);

            try
            {
                Configure(wrapped.Configuration);

                var decorated = Decorate(wrapped) ?? wrapped;

                switch (apiRootCachingBehavior)
                {
                case CachingBehavior.Never:
                    break;

                case CachingBehavior.PerClient:
                    var apiRootResource = await GetFreshRootResourceAsync(decorated, wrapped.Configuration).ConfigureAwait(false);

                    wrapped.CachedApiRootResource = apiRootResource;
                    break;

                case CachingBehavior.Once:
                    _cachedApiRootResource = _cachedApiRootResource ?? await GetFreshRootResourceAsync(decorated, wrapped.Configuration).ConfigureAwait(false);

                    wrapped.CachedApiRootResource = _cachedApiRootResource;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(apiRootCachingBehavior), apiRootCachingBehavior, null);
                }

                return(decorated);
            }
            catch (Exception)
            {
                wrapped.Dispose();                 // client is unusable ...
                throw;
            }
        }
예제 #19
0
        public void CachingBehaviorInvokeInputNullTest()
        {
            var target = new CachingBehavior(_cachingProvider, _logger);

            target.Invoke(null, new GetNextInterceptionBehaviorDelegate(() => null));
        }