コード例 #1
0
        public async Task returns_empty_when_no_currencies_are_found()
        {
            _messageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.NotFound
            });

            var emptyRates = await _retriever.GetAll();

            Assert.Empty(emptyRates);
        }
コード例 #2
0
        private async Task RefreshCache(Currency currencyToRefresh)
        {
            if (_cache.Count == 0)
            {
                var newExchangeRates = await _exchangeRatesRetriever.GetAll();

                foreach (var(currency, rate) in newExchangeRates)
                {
                    _cache.Add(currency, rate);
                }

                return;
            }

            // this can happen when the above method does not load the currency because it was not found
            var hasCurrency = _cache.TryGetValue(currencyToRefresh, out var exchangeRate);

            if (!hasCurrency)
            {
                return;
            }

            var cacheIsFreshFrom = DateTimeOffset.UtcNow.AddDays(-_configuration.CacheRefreshRate).ToUnixTimeSeconds();
            var cacheHasExpired  = exchangeRate.UpdatedAt < cacheIsFreshFrom;

            if (cacheHasExpired)
            {
                var newExchangeRate = await _exchangeRatesRetriever.Get(currencyToRefresh);

                if (newExchangeRate is EmptyExchangeRate)
                {
                    // could not retrieve fresher info
                    // so its best to remove it and signal this as an issue
                    // please review the comment in https://github.com/gacastro/rates-r-us#future-improvements
                    _cache.Remove(currencyToRefresh);
                    return;
                }

                _cache[currencyToRefresh] = newExchangeRate;
            }
        }