Factored all the code for managing the state around ITestSession
interaction into a single method.
This commit is contained in:
Jared Parsons 2015-08-21 17:00:55 -07:00
Родитель a3cfe6c3c5
Коммит ea51cd39eb
1 изменённых файлов: 44 добавлений и 59 удалений

Просмотреть файл

@ -26,7 +26,6 @@ namespace xunit.runner.wpf.ViewModel
private readonly ObservableCollection<TestCaseViewModel> allTestCases = new ObservableCollection<TestCaseViewModel>(); private readonly ObservableCollection<TestCaseViewModel> allTestCases = new ObservableCollection<TestCaseViewModel>();
private CancellationTokenSource filterCancellationTokenSource = new CancellationTokenSource(); private CancellationTokenSource filterCancellationTokenSource = new CancellationTokenSource();
private List<ITestSession> testSessionList = new List<ITestSession>();
private CancellationTokenSource cancellationTokenSource; private CancellationTokenSource cancellationTokenSource;
private bool isBusy; private bool isBusy;
private SearchQuery searchQuery = new SearchQuery(); private SearchQuery searchQuery = new SearchQuery();
@ -209,9 +208,6 @@ namespace xunit.runner.wpf.ViewModel
private async Task AddAssemblies(IEnumerable<AssemblyAndConfigFile> assemblies) private async Task AddAssemblies(IEnumerable<AssemblyAndConfigFile> assemblies)
{ {
Debug.Assert(this.cancellationTokenSource == null);
Debug.Assert(this.testSessionList.Count == 0);
if (!assemblies.Any()) if (!assemblies.Any())
{ {
return; return;
@ -220,31 +216,24 @@ namespace xunit.runner.wpf.ViewModel
var loadingDialog = new LoadingDialog { Owner = MainWindow.Instance }; var loadingDialog = new LoadingDialog { Owner = MainWindow.Instance };
try try
{ {
this.cancellationTokenSource = new CancellationTokenSource(); await ExecuteTestSessionOperation(() =>
foreach (var assembly in assemblies)
{ {
var assemblyPath = assembly.AssemblyFileName; var testSessionList = new List<ITestSession>();
var session = this.testUtil.Discover(assemblyPath, cancellationTokenSource.Token); foreach (var assembly in assemblies)
this.testSessionList.Add(session); {
var assemblyPath = assembly.AssemblyFileName;
var session = this.testUtil.Discover(assemblyPath, cancellationTokenSource.Token);
session.TestDiscovered += OnTestDiscovered;
session.TestDiscovered += OnTestDiscovered; testSessionList.Add(session);
session.SessionFinished += delegate { OnTestSessionFinished(session); }; Assemblies.Add(new TestAssemblyViewModel(assembly));
}
Assemblies.Add(new TestAssemblyViewModel(assembly)); return testSessionList;
} });
this.IsBusy = true;
await Task.WhenAll(this.testSessionList.Select(x => x.Task));
}
catch (Exception ex)
{
MessageBox.Show(Application.Current.MainWindow, ex.ToString());
this.IsBusy = false;
} }
finally finally
{ {
Debug.Assert(!IsBusy);
loadingDialog.Close(); loadingDialog.Close();
} }
} }
@ -267,15 +256,7 @@ namespace xunit.runner.wpf.ViewModel
private async void OnExecuteWindowLoaded() private async void OnExecuteWindowLoaded()
{ {
try await AddAssemblies(ParseCommandLine(Environment.GetCommandLineArgs().Skip(1)));
{
IsBusy = true;
await AddAssemblies(ParseCommandLine(Environment.GetCommandLineArgs().Skip(1)));
}
finally
{
IsBusy = false;
}
} }
private IEnumerable<AssemblyAndConfigFile> ParseCommandLine(IEnumerable<string> enumerable) private IEnumerable<AssemblyAndConfigFile> ParseCommandLine(IEnumerable<string> enumerable)
@ -303,28 +284,22 @@ namespace xunit.runner.wpf.ViewModel
private bool CanExecuteRun() private bool CanExecuteRun()
=> !IsBusy && TestCases.Any(); => !IsBusy && TestCases.Any();
private void OnExecuteRun() private async void OnExecuteRun()
{ {
try await ExecuteTestSessionOperation(RunTests);
{
TestsCompleted = 0;
TestsPassed = 0;
TestsFailed = 0;
TestsSkipped = 0;
CurrentRunState = TestState.NotRun;
RunTests();
Output = string.Empty;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} }
private void RunTests() private List<ITestSession> RunTests()
{ {
Debug.Assert(this.cancellationTokenSource == null); Debug.Assert(this.isBusy);
Debug.Assert(this.testSessionList.Count == 0); Debug.Assert(this.cancellationTokenSource != null);
TestsCompleted = 0;
TestsPassed = 0;
TestsFailed = 0;
TestsSkipped = 0;
CurrentRunState = TestState.NotRun;
Output = string.Empty;
foreach (var tc in TestCases) foreach (var tc in TestCases)
{ {
@ -334,8 +309,8 @@ namespace xunit.runner.wpf.ViewModel
// TODO: Need a way to filter based on traits // TODO: Need a way to filter based on traits
var runAll = TestCases.Count == this.allTestCases.Count; var runAll = TestCases.Count == this.allTestCases.Count;
var testSessionList = new List<ITestSession>();
this.cancellationTokenSource = new CancellationTokenSource();
foreach (var assemblyPath in TestCases.Select(x => x.AssemblyFileName).Distinct()) foreach (var assemblyPath in TestCases.Select(x => x.AssemblyFileName).Distinct())
{ {
ITestRunSession session; ITestRunSession session;
@ -353,21 +328,31 @@ namespace xunit.runner.wpf.ViewModel
} }
session.TestFinished += OnTestFinished; session.TestFinished += OnTestFinished;
session.SessionFinished += delegate { OnTestSessionFinished(session); }; testSessionList.Add(session);
this.testSessionList.Add(session);
} }
this.IsBusy = true; return testSessionList;
} }
private void OnTestSessionFinished(ITestSession session) private async Task ExecuteTestSessionOperation(Func<List<ITestSession>> operation)
{ {
Debug.Assert(this.testSessionList.Contains(session)); Debug.Assert(!this.IsBusy);
Debug.Assert(this.cancellationTokenSource != null); Debug.Assert(this.cancellationTokenSource == null);
this.testSessionList.Remove(session); try
{
this.IsBusy = true;
this.cancellationTokenSource = new CancellationTokenSource();
if (this.testSessionList.Count == 0) var testSessionList = operation();
await Task.WhenAll(testSessionList.Select(x => x.Task));
}
catch (Exception ex)
{
this.cancellationTokenSource?.Cancel();
MessageBox.Show(Application.Current.MainWindow, ex.ToString());
}
finally
{ {
this.cancellationTokenSource = null; this.cancellationTokenSource = null;
this.IsBusy = false; this.IsBusy = false;