Ejemplo n.º 1
0
        /// <summary>
        /// Waits until query is printed on session output and
        /// executes handler. The output including expected query is
        /// passed to handler.
        /// </summary>
        /// <param name="query">expected output</param>
        /// <param name="handler">action to be performed, it accepts session output as ana argument</param>
        /// <exception cref="System.TimeoutException">Thrown when query is not find for given
        /// amount of time</exception>
        public void Expect(string query, ExpectedHandlerWithOutput handler)
        {
            var tokenSource      = new CancellationTokenSource();
            CancellationToken ct = tokenSource.Token;

            _output = "";
            bool expectedQueryFound = false;
            Task task = Task.Factory.StartNew(() =>
            {
                while (!ct.IsCancellationRequested && !expectedQueryFound)
                {
                    _output           += _spawnable.Read();
                    expectedQueryFound = _output.Contains(query);
                }
            }, ct);

            if (task.Wait(_timeout, ct))
            {
                handler(_output);
            }
            else
            {
                tokenSource.Cancel();
                throw new TimeoutException();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Waits until query is printed on session output and
        /// executes handler. The output including expected Regex query is
        /// passed to handler.
        /// </summary>
        /// <param name="query">expected output in RegEx</param>
        /// <param name="handler">action to be performed, it accepts session output as ana argument</param>
        /// <exception cref="System.TimeoutException">Thrown when query is not find for given
        /// amount of time</exception>
        public void ExpectMatch(string query, ExpectedHandlerWithOutput handler)
        {
            var tokenSource      = new CancellationTokenSource();
            CancellationToken ct = tokenSource.Token;

            _output = "";
            bool expectedQueryFound = false;
            Task task = Task.Factory.StartNew(() =>
            {
                while (!ct.IsCancellationRequested && !expectedQueryFound)
                {
                    _output += _spawnable.Read();
                    foreach (string line in _output.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
                    {
                        Match matchfound = Regex.Match(line, query);
                        if (matchfound.Success)
                        {
                            expectedQueryFound = matchfound.Success;
                        }
                    }
                }
            }, ct);

            if (task.Wait(_timeout, ct))
            {
                handler(_output);
            }
            else
            {
                tokenSource.Cancel();
                throw new TimeoutException();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Waits until query is printed on session output and
        /// executes handler. The output including expected query is
        /// passed to handler.
        /// </summary>
        /// <param name="query">expected output</param>
        /// <param name="handler">action to be performed, it accepts session output as ana argument</param>
        /// <exception cref="System.TimeoutException">Thrown when query is not find for given
        /// amount of time</exception>
        private void Expect(IMatcher matcher, ExpectedHandlerWithOutput handler, Int32 timeout = 0)
        {
            var tokenSource = new CancellationTokenSource();

            if (timeout <= 0)
            {
                timeout = _timeout;
            }
            CancellationToken ct = tokenSource.Token;
            //_output_buffer = ""; //lpei: no need to clear buffer, the prevous buffer + new output
            var  read_buffer        = _output_buffer;
            bool expectedQueryFound = false;
            Task task = Task.Factory.StartNew(() =>
            {
                while (!ct.IsCancellationRequested && !expectedQueryFound)
                {
                    read_buffer       += _spawnable.Read();
                    expectedQueryFound = matcher.IsMatch(read_buffer);
                    if (expectedQueryFound)
                    {
                        Logging.WriteLine(String.Format("PreMatched: <#{0}#>", matcher.PreMatchedString));
                        Logging.WriteLine(String.Format("Matched: <#{0}#>", matcher.MatchedString));
                        Logging.WriteLine(String.Format("PostMatched: <#{0}#>", matcher.PostMatchedString));
                        read_buffer = matcher.PreMatchedString + matcher.MatchedString;

                        _output_buffer = matcher.PostMatchedString;
                    }
                }
            }, ct);

            if (task.Wait(timeout, ct))
            {
                if (expectedQueryFound)
                {
                    Logging.WriteLine("Found expected output=PreMatched+Matched!");
                    handler(read_buffer);
                }
                else
                {
                    Logging.WriteLine(String.Format("Not found expected output from: <#{0}#>!", read_buffer));
                    _output_buffer = read_buffer;
                    handler("");
                }
            }
            else
            {
                tokenSource.Cancel();
                throw new TimeoutException();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Waits until query is printed on session output and
        /// executes handler. The output including expected query is
        /// passed to handler.
        /// </summary>
        /// <param name="query">expected output in RegEx</param>
        /// <param name="handler">action to be performed, it accepts session output as ana argument</param>
        /// <exception cref="System.TimeoutException">Thrown when query is not find for given
        /// amount of time</exception>
        public async Task ExpectMatchAsync(string query, ExpectedHandlerWithOutput handler)
        {
            Task timeoutTask = null;

            if (_timeout > 0)
            {
                timeoutTask = Task.Delay(_timeout);
            }
            _output = "";
            bool expectedQueryFound = false;

            while (!expectedQueryFound)
            {
                Task <string> task  = _spawnable.ReadAsync();
                IList <Task>  tasks = new List <Task>();
                tasks.Add(task);
                if (timeoutTask != null)
                {
                    tasks.Add(timeoutTask);
                }
                Task any = await Task.WhenAny(tasks).ConfigureAwait(false);

                if (task == any)
                {
                    _output += await task.ConfigureAwait(false);

                    // expectedQueryFound = _output.Contains(query);
                    foreach (string line in _output.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
                    {
                        Match matchfound = Regex.Match(line, query);
                        if (matchfound.Success)
                        {
                            expectedQueryFound = matchfound.Success;
                        }
                    }
                    if (expectedQueryFound)
                    {
                        handler(_output);
                    }
                }
                else
                {
                    throw new TimeoutException();
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Waits until query is printed on session output and
        /// executes handler. The output including expected query is
        /// passed to handler.
        /// </summary>
        /// <param name="query">expected output</param>
        /// <param name="handler">action to be performed, it accepts session output as ana argument</param>
        /// <exception cref="System.TimeoutException">Thrown when query is not find for given
        /// amount of time</exception>
        public async Task ExpectAsync(string query, ExpectedHandlerWithOutput handler)
        {
            Task timeoutTask = null;

            if (_timeout > 0)
            {
                timeoutTask = Task.Delay(_timeout);
            }
            _output = "";
            bool expectedQueryFound = false;

            while (!expectedQueryFound)
            {
                Task <string> task  = _spawnable.ReadAsync();
                IList <Task>  tasks = new List <Task>();
                tasks.Add(task);
                if (timeoutTask != null)
                {
                    tasks.Add(timeoutTask);
                }
                Task any = await Task.WhenAny(tasks).ConfigureAwait(false);

                if (task == any)
                {
                    _output += await task.ConfigureAwait(false);

                    expectedQueryFound = _output.Contains(query);
                    if (expectedQueryFound)
                    {
                        handler(_output);
                    }
                }
                else
                {
                    throw new TimeoutException();
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Waits until query is printed on session output and
        /// executes handler. The output including expected query is
        /// passed to handler.
        /// </summary>
        /// <param name="query">expected output</param>
        /// <param name="handler">action to be performed, it accepts session output as ana argument</param>
        /// <exception cref="System.TimeoutException">Thrown when query is not find for given
        /// amount of time</exception>
        private async Task ExpectAsync(IMatcher matcher, ExpectedHandlerWithOutput handler)
        {
            Task timeoutTask = null;

            if (_timeout > 0)
            {
                timeoutTask = Task.Delay(_timeout);
            }
            _output_buffer = "";
            bool expectedQueryFound = false;

            while (!expectedQueryFound)
            {
                Task <string> task  = _spawnable.ReadAsync();
                IList <Task>  tasks = new List <Task>();
                tasks.Add(task);
                if (timeoutTask != null)
                {
                    tasks.Add(timeoutTask);
                }
                Task any = await Task.WhenAny(tasks).ConfigureAwait(false);

                if (task == any)
                {
                    _output_buffer += await task.ConfigureAwait(false);

                    expectedQueryFound = matcher.IsMatch(_output_buffer);
                    if (expectedQueryFound)
                    {
                        handler(_output_buffer);
                    }
                }
                else
                {
                    throw new TimeoutException();
                }
            }
        }
Ejemplo n.º 7
0
 public async Task ExpectAsync(Regex regex, ExpectedHandlerWithOutput handler)
 {
     await ExpectAsync(new RegexMatcher(regex), s => handler(s)).ConfigureAwait(false);
 }
Ejemplo n.º 8
0
 public async Task ExpectAsync(string query, ExpectedHandlerWithOutput handler)
 {
     await ExpectAsync(new StringContainsMatcher(query), s => handler(s)).ConfigureAwait(false);
 }
Ejemplo n.º 9
0
 public void Expect(Regex regex, ExpectedHandlerWithOutput handler, Int32 timeout = 0)
 {
     Expect(new RegexMatcher(regex), (s) => handler(s), timeout);
 }
Ejemplo n.º 10
0
 public void Expect(string query, ExpectedHandlerWithOutput handler, Int32 timeout = 0)
 {
     Expect(new StringContainsMatcher(query), (s) => handler(s), timeout);
 }