Esempio n. 1
0
        public async Task ReadFile_WhenStreamReaderThrows_ShouldReturnEmptyGames()
        {
            var path = "path";

            _mockStreamReader.ReadLineAsync(path)
            .Throws <Exception>();

            var result = await _sut.ReadFile(path);

            result.Count.Should().Be(0);
        }
Esempio n. 2
0
        protected virtual async Task ProcessResponseFromReaderAsync(Action <string> processResponse, IStreamReader reader,
                                                                    CancellationToken cancellationToken)
        {
            // In a loop, reading from a stream. while reading from the stream, process the lines in the stream.
            // Reset the timer after processing each line (reading from the stream).
            // If the read time out occurs, throw exception and exit method.

            while (!cancellationToken.IsCancellationRequested)
            {
                var readTimeoutTask = Task.Delay(_configuration.ReadTimeout);

                var readLineTask = reader.ReadLineAsync();

                var completedTask = await Task.WhenAny(readLineTask, readTimeoutTask);

                if (completedTask == readTimeoutTask)
                {
                    Util.SuppressExceptions(readLineTask); // must do this since we're not going to await the task
                    throw new HttpRequestException(Resources.EventSourceService_Read_Timeout);
                }
                else
                {
                    Util.SuppressExceptions(readTimeoutTask); // this task should never throw an exception, but you never know
                }

                string line = readLineTask.Result;
                if (line == null)
                {
                    // this means the stream is done, i.e. the connection was closed
                    return;
                }
                processResponse(line);
            }
        }
Esempio n. 3
0
        public async Task ProcessesMultiLineResponseCorrectly()
        {
            string responseLn1 = "250-smtp.com at your service, [123.456.789.101]";
            string responseLn2 = "250-SIZE 35882577";
            string responseLn3 = "250 STARTTLS";

            IStreamReader reader = A.Fake <IStreamReader>();

            A.CallTo(() => reader.ReadLineAsync()).ReturnsNextFromSequence(
                Task.FromResult(responseLn1),
                Task.FromResult(responseLn2),
                Task.FromResult(responseLn3));

            SmtpResponse smtpResponse = await _smtpDeserializer.Deserialize(reader);

            Assert.That(smtpResponse.Responses.Count, Is.EqualTo(3));

            Assert.That(smtpResponse.Responses[0].ResponseCode, Is.EqualTo(ResponseCode.Ok));
            Assert.That(smtpResponse.Responses[0].Value, Is.EqualTo("smtp.com at your service, [123.456.789.101]"));

            Assert.That(smtpResponse.Responses[1].ResponseCode, Is.EqualTo(ResponseCode.Ok));
            Assert.That(smtpResponse.Responses[1].Value, Is.EqualTo("SIZE 35882577"));

            Assert.That(smtpResponse.Responses[2].ResponseCode, Is.EqualTo(ResponseCode.Ok));
            Assert.That(smtpResponse.Responses[2].Value, Is.EqualTo("STARTTLS"));
        }
Esempio n. 4
0
        public async Task <List <TennisGame> > ReadFile(string filePath)
        {
            var gamesList = new List <TennisGame>();

            var fileLines = new List <string>();

            try
            {
                fileLines = await _streamReader.ReadLineAsync(filePath);
            }
            catch (Exception)
            {
            }

            foreach (var line in fileLines)
            {
                var tennisGame = new TennisGame(new List <char>());

                foreach (var character in line)
                {
                    tennisGame.Points.Add(character);
                }
                gamesList.Add(tennisGame);
            }

            return(gamesList);
        }
Esempio n. 5
0
        public void InvalidSeparatorThrows()
        {
            string response = "220*response";

            IStreamReader reader = A.Fake <IStreamReader>();

            A.CallTo(() => reader.ReadLineAsync()).Returns(Task.FromResult(response));

            Assert.ThrowsAsync <ArgumentException>(() => _smtpDeserializer.Deserialize(reader));
        }
Esempio n. 6
0
        public async Task ProcessesSingleLineResponseCorrectly()
        {
            string response = "220 2.0.0 Ready to start TLS";

            IStreamReader reader = A.Fake <IStreamReader>();

            A.CallTo(() => reader.ReadLineAsync()).Returns(Task.FromResult(response));

            SmtpResponse smtpResponse = await _smtpDeserializer.Deserialize(reader);

            Assert.That(smtpResponse.Responses.Count, Is.EqualTo(1));
            Assert.That(smtpResponse.Responses.First().ResponseCode, Is.EqualTo(ResponseCode.ServiceReady));
            Assert.That(smtpResponse.Responses.First().Value, Is.EqualTo("2.0.0 Ready to start TLS"));
        }
Esempio n. 7
0
        public async Task <SmtpResponse> Deserialize(IStreamReader reader)
        {
            List <string> values = new List <string>();
            string        response;

            do
            {
                response = await reader.ReadLineAsync();

                if (response != null)
                {
                    values.Add(response);
                }
            } while (response != null && _regex.IsMatch(values.Last()));

            List <Response> responses = values.Select(Parse).ToList();

            return(new SmtpResponse(responses));
        }
Esempio n. 8
0
        protected virtual async Task ProcessResponseFromReaderAsync(Action <string> processResponse, IStreamReader reader,
                                                                    CancellationToken cancellationToken)
        {
            // In a loop, reading from a stream. while reading from the stream, process the lines in the stream.
            // Reset the timer after processing each line (reading from the stream).
            // If the read time out occurs, throw exception and exit method.

            while (!cancellationToken.IsCancellationRequested)
            {
                var readTimeoutTask = Task.Delay(_configuration.ReadTimeout);

                var readLineTask = reader.ReadLineAsync();

                var completedTask = await Task.WhenAny(readLineTask, readTimeoutTask);

                if (completedTask == readTimeoutTask)
                {
                    throw new HttpRequestException(Resources.EventSourceService_Read_Timeout);
                }

                processResponse(readLineTask.Result);
            }
        }