Exemplo n.º 1
0
        public async Task AsyncV3_Log_SingleThread()
        {
            // Arrange
            var logger   = new AsyncLoggerV3(_path);
            var expected = new LogResponse()
            {
                IsSuccess = true
            };

            // Act
            var actual = await logger.LogAsync(string.Format(_logMessage, "[Async3]"));

            // Assert
            Assert.AreEqual(expected.IsSuccess, actual.IsSuccess);
            Assert.IsTrue(File.Exists(_logFile));
        }
Exemplo n.º 2
0
        public async Task AsyncV3_Log_MultiThread()
        {
            // Arrange
            var logger   = new AsyncLoggerV3(_path);
            var expected = new LogResponse()
            {
                IsSuccess = true
            };

            // Act
            var tasks = Enumerable.Range(1, 100)
                        .Select(x => logger.LogAsync(String.Format(_logMessage, $"[Async3 {x}]")));

            var actual = await Task.WhenAll(tasks.ToArray());

            /**** Notice no additional work is needed to schedule/coordinate all threads ****/


            // Assert
            Assert.IsTrue(actual.Length == 100);         // Access to all 100 thread outcomes
            Assert.IsTrue(actual.All(a => a.IsSuccess)); // Checks that every task was successful
            Assert.IsTrue(File.Exists(_logFile));
        }