Exemplo n.º 1
0
        public async Task<Series> RetrieveFeed(string feedAddr)
        {
            string content = string.Empty;
            using (HttpClient client = new HttpClient())
                content = await client.GetStringAsync(feedAddr);

            var feed = XElement.Parse(content);
            var id = feed.Element(ChannelElementName).Element(LinkElementName).Value;
            var title = feed.Element(ChannelElementName).Element(TitleElementName).Value;
            var imagePath = feed.Element(ChannelElementName).Element(ImageXName).Attribute(HrefElementName).Value;
            var imageElement = feed.Element(ChannelElementName).Element(ImageElementName);
            if (imageElement != null)
                imagePath = imageElement.Element(UrlElementName).Value;
            var description = feed.Element(ChannelElementName).Element(DescriptionElementName).Value;
            var rVal = new Series
                {
                    Id = id,
                    Title = title,
                    ImagePath = imagePath,
                    Description = description
                };
            var items = feed.Element(ChannelElementName).Elements(ItemElementName);

            await rVal.SetEpisodes(Task.Run(() => ParseEpisodes(items)));
            return rVal;
        }
        public async Task FastDownloadWorks()
        {
            var downloader = new FeedDownloaderStub();
            var series = new Series();
            await series.SetEpisodes(Task.FromResult(new List<Episode>().AsEnumerable()));
            downloader.SetResult(Task.FromResult(series));
            var underTest = new PodcastCollection(downloader);

            Assert.AreEqual(7, underTest.AllGroups.Count);
        }
        public async Task ExcpetionsCreateEmptySeries()
        {
            var downloader = new FeedDownloaderStub();
            var series = new Series();

            var episodeTCS = new TaskCompletionSource<IEnumerable<Episode>>();
            var downloadTCS = new TaskCompletionSource<Series>();

            var parseTask = series.SetEpisodes(episodeTCS.Task);
            downloader.SetResult(downloadTCS.Task);

            var underTest = new PodcastCollection(downloader);

            episodeTCS.TrySetResult(new List<Episode>().AsEnumerable());
            downloadTCS.TrySetException(new NullReferenceException());
            await parseTask;

            Assert.AreEqual(0, underTest.AllGroups.Count);
        }
        public async Task SlowDownloadWorks()
        {
            var downloader = new FeedDownloaderStub();
            var series = new Series();

            var episodeTCS = new TaskCompletionSource<IEnumerable<Episode>>();
            var downloadTCS = new TaskCompletionSource<Series>();

            var parseTask = series.SetEpisodes(episodeTCS.Task);
            downloader.SetResult(downloadTCS.Task);

            var underTest = new PodcastCollection(downloader);

            episodeTCS.TrySetResult(new List<Episode>().AsEnumerable());
            downloadTCS.TrySetResult(series);
            await parseTask;

            Assert.AreEqual(7, underTest.AllGroups.Count);
        }