Example #1
0
        static async Task Main()
        {
            var wordsToCount     = new[] { "war", "peace" };
            var dataSource       = new FileDataSource("WarAndPeace.txt");
            var wordCountService = new WordCounterService(dataSource);
            var warCount         = 0;
            var peaceCount       = 0;
            var linesProcessed   = 0;

            var progress = new Progress <int>(n => linesProcessed += n);

            await foreach (var update in wordCountService.GetWordCountUpdates(wordsToCount, CancellationToken.None, progress))
            {
                if (update.Word == "war")
                {
                    warCount += update.OccurrencesCount;
                }
                else
                {
                    peaceCount += update.OccurrencesCount;
                }

                Console.SetCursorPosition(0, 0);
                Console.WriteLine($"Lines processed: {linesProcessed} war: {warCount} peace {peaceCount}");
            }
        }
        async Task <List <WordCountUpdate> > GetWordCountUpdate(params string[] wordsToCount)
        {
            var result = new List <WordCountUpdate>();

            await foreach (var update in m_WordCounterService.GetWordCountUpdates(wordsToCount))
            {
                result.Add(update);
            }

            return(result);
        }
        public void GetWordCountUpdates_DataSourceThrowsAndException_RethrowsTheException()
        {
            async IAsyncEnumerable <string> StreamData()
            {
                await Task.FromException(new IOException("hard disk is corrupted"));

                yield break; // otherwise compiler will complain "not all path return a value"
            }

            var dataSource = new Mock <IDataSource>();

            dataSource.Setup(x => x.GetData()).Returns(StreamData);
            var service = new WordCounterService(dataSource.Object);

            var firstUpdate = service.GetWordCountUpdates(new[] { "foo" }).FirstAsync();

            Assert.That(async() => await firstUpdate, Throws.InstanceOf <IOException>());
        }
Example #4
0
        public void GetWordCountUpdates_CanBeCancelledAfterItemInStream()
        {
            var dataSource        = new Mock <IDataSource>();
            var source            = new CancellationTokenSource();
            var cancellationToken = source.Token;

            async IAsyncEnumerable <string> StreamData()
            {
                yield return("foo");

                await Task.CompletedTask;
            }

            dataSource.Setup(x => x.GetData()).Returns(StreamData);
            var service    = new WordCounterService(dataSource.Object);
            var enumerator = service.GetWordCountUpdates(new[] { "foo" }, cancellationToken).GetAsyncEnumerator(cancellationToken);

            source.Cancel();

            Assert.That(async() => await enumerator.MoveNextAsync(), Throws.TypeOf <OperationCanceledException>());
        }