/// <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(); } } }
/// <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(); } } }