public void Init()
        {
            _memoryCache = new MemoryCache("sportEventCache");

            _cacheManager      = new CacheManager();
            _dataRouterManager = new TestDataRouterManager(_cacheManager);

            _timer           = new TestTimer(false);
            _sportEventCache = new SportEventCache(_memoryCache,
                                                   _dataRouterManager,
                                                   new SportEventCacheItemFactory(_dataRouterManager, new SemaphorePool(5, ExceptionHandlingStrategy.THROW), TestData.Cultures.First(), new MemoryCache("FixtureTimestampCache")),
                                                   _timer,
                                                   TestData.Cultures3,
                                                   _cacheManager);

            var deserializer  = new Deserializer <tournamentInfoEndpoint>();
            var dataFetcher   = new TestDataFetcher();
            var mapperFactory = new TournamentInfoMapperFactory();

            var tourDataProvider = new DataProvider <tournamentInfoEndpoint, TournamentInfoDTO>(TestData.RestXmlPath + TourInfoXml, dataFetcher, deserializer, mapperFactory);

            _tourApiData = deserializer.Deserialize(dataFetcher.GetData(new Uri(string.Format(TestData.RestXmlPath + TourInfoXml))));
            _tourDtoData = tourDataProvider.GetDataAsync("", "en").Result;
            _tourCiData  = (TournamentInfoCI)_sportEventCache.GetEventCacheItem(URN.Parse("sr:tournament:40"));

            var seasonDataProvider = new DataProvider <tournamentInfoEndpoint, TournamentInfoDTO>(TestData.RestXmlPath + SeasonInfoXml, dataFetcher, deserializer, mapperFactory);

            _seasonApiData = deserializer.Deserialize(dataFetcher.GetData(new Uri(string.Format(TestData.RestXmlPath + SeasonInfoXml))));
            _seasonDtoData = seasonDataProvider.GetDataAsync("", "en").Result;
            _seasonCiData  = (TournamentInfoCI)_sportEventCache.GetEventCacheItem(URN.Parse("sr:season:80242"));
        }
Beispiel #2
0
        public void ConcurrencyFetchTest()
        {
            IEnumerable <SportData> sports   = null;
            SportData        sport           = null;
            SportData        tournamentSport = null;
            TournamentInfoCI tournament      = null;
            IEnumerable <Tuple <URN, URN> > tournamentEvents = null;
            IEnumerable <Tuple <URN, URN> > dateEvents       = null;

            for (var i = 0; i < 5; i++)
            {
                Task.Run(async() =>
                {
                    _sportDataCache.FetchedCultures.Clear();
                    sports          = await _sportDataCache.GetSportsAsync(TestData.Cultures);
                    sport           = await _sportDataCache.GetSportAsync(TestData.SportId, TestData.Cultures);
                    tournamentSport = await _sportDataCache.GetSportForTournamentAsync(URN.Parse("sr:tournament:146"), TestData.Cultures);

                    tournamentEvents = await _sportEventCache.GetEventIdsAsync(TestData.TournamentId, _cultureEn);
                    tournament       = (TournamentInfoCI)_sportEventCache.GetEventCacheItem(TestData.TournamentId);
                    dateEvents       = await _sportEventCache.GetEventIdsAsync(DateTime.Now, _cultureEn);
                }).GetAwaiter().GetResult();

                Assert.IsNotNull(sports, "Retrieved sports cannot be null");
                // ReSharper disable once PossibleMultipleEnumeration
                foreach (var s in sports)
                {
                    BaseSportDataValidation(s);
                }
                Assert.IsNotNull(sport, "sport cannot be a null reference");
                Assert.IsNotNull(tournament, "tournament cannot be a null reference");
                Assert.IsNotNull(tournamentEvents, "tournamentEvents cannot be a null reference");
                Assert.IsNotNull(dateEvents, "dateEvents cannot be a null reference");
                Assert.AreEqual(1, tournamentSport.Categories.Count(), "The number of categories must be 1");
                Assert.AreEqual(1, tournamentSport.Categories.First().Tournaments.Count(), "the number of tournaments must be 1");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets a <see cref="SportData"/> representing the parent sport of the tournament specified by <code>tournamentId</code> in the languages specified by <code>cultures</code>, or a null reference
        /// if the specified sport does not exist, or it(or one of it's children) is not available in one of the requested languages
        /// </summary>
        /// <remarks>
        /// The returned <see cref="SportData"/> represents a sport with flattened hierarchy information - only one category and one tournament are found in the returned instance.
        /// </remarks>
        /// <param name="tournamentId">A <see cref="URN"/> specifying the tournament whose parent sport to get</param>
        /// <param name="cultures">A <see cref="IEnumerable{CultrureInfo}"/> specifying the languages to which the sport must be translated</param>
        /// <param name="fetchTournamentIfMissing">Indicates if the tournament should be fetched if not obtained via all tournaments request</param>
        /// <returns>A <see cref="SportData"/> representing the requested sport translated into requested languages</returns>
        private SportData GetSportForTournamentFromCache(URN tournamentId, IEnumerable <CultureInfo> cultures, bool fetchTournamentIfMissing)
        {
            var cultureList = cultures as IList <CultureInfo> ?? cultures.ToList();

            TournamentInfoCI cachedTournament = null;

            if (!_sportEventCache.CacheHasItem(tournamentId, CacheItemType.Tournament))
            {
                if (fetchTournamentIfMissing)
                {
                    try
                    {
                        cachedTournament = (TournamentInfoCI)_sportEventCache.GetEventCacheItem(tournamentId);
                        var unused = cachedTournament.GetCompetitorsAsync(cultureList).Result;
                    }
                    catch (Exception e)
                    {
                        ExecutionLog.Warn($"Error obtaining data for newly created tournament {tournamentId}.", e);
                        return(null);
                    }
                }
            }
            else
            {
                cachedTournament = (TournamentInfoCI)_sportEventCache.GetEventCacheItem(tournamentId);
                if (fetchTournamentIfMissing)
                {
                    try
                    {
                        var unused = cachedTournament.GetCompetitorsAsync(cultureList).Result;
                    }
                    catch (Exception e)
                    {
                        ExecutionLog.Warn($"Error obtaining data for newly created tournament {tournamentId}.", e);
                    }
                }
            }

            if (!(cachedTournament != null && cachedTournament.HasTranslationsFor(cultureList)))
            {
                return(null);
            }

            CategoryCI cachedCategory;

            if (!(Categories.TryGetValue(cachedTournament.GetCategoryIdAsync().Result, out cachedCategory) && cachedCategory.HasTranslationsFor(cultureList)))
            {
                return(null);
            }

            SportCI cachedSport;

            if (!(Sports.TryGetValue(cachedCategory.SportId, out cachedSport) && cachedSport.HasTranslationsFor(cultureList)))
            {
                return(null);
            }

            var category = new CategoryData(
                cachedCategory.Id,
                cachedCategory.Name.Where(k => cultureList.Contains(k.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                cachedCategory.CountryCode,
                new[] { cachedTournament.Id });

            return(new SportData(
                       cachedSport.Id,
                       cachedSport.Name.Where(kvp => cultureList.Contains(kvp.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
                       new[] { category }));
        }