Skip to content

Commit

Permalink
Read data: Improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfViola committed Jan 3, 2024
1 parent b672a9d commit 3ec73ee
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 53 deletions.
77 changes: 35 additions & 42 deletions STI Test Report/ReadReportData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
namespace STI_Test_Report
{
public delegate void LogEventHandler(object sender, LogEventArgs e);

public delegate void LogExceptionHandler(object sender, LogExeptionArgs e);
public class LogEventArgs : EventArgs
{
public string Message { get; set; }
}

public class LogExeptionArgs : EventArgs
{
public Exception Exception { get; set; }
}

internal class ReadReportData
{
public event LogEventHandler WriteLog;
private static SemaphoreSlim _semaphore = new SemaphoreSlim(1);
public event LogExceptionHandler WriteException;

#region Sub classes
public class Parameters
Expand Down Expand Up @@ -51,50 +55,39 @@ public string LogHeader()
}
}
#endregion
public async static Task Start(Parameters commandFile, ADORest.RestTestPlan adoRest, TeamProject teamProject, string dbFilePath, LogEventHandler logEventHandler )
{
await _semaphore.WaitAsync();

ReadReportData readData = null;
try
{
readData = new ReadReportData();
readData.WriteLog += logEventHandler;
await readData.Start(commandFile, adoRest, teamProject, dbFilePath );
}
finally
{
readData.WriteLog -= logEventHandler;
_semaphore.Release();
}

}

public async Task Start(Parameters commandFile, ADORest.RestTestPlan adoRest, TeamProject teamProject, string dbFilePath)
{
if (!File.Exists(dbFilePath) || commandFile.CreateNewFile)
File.Copy(Program.GetTemplateDBFilePath(), dbFilePath, true);
try
{
if (!File.Exists(dbFilePath) || commandFile.CreateNewFile)
File.Copy(Program.GetTemplateDBFilePath(), dbFilePath, true);

var testPlan = await adoRest.GetTestPlan(commandFile.TestPlanId);
var testPlan = await adoRest.GetTestPlan(commandFile.TestPlanId);

using var sqlLiteBL = new SQLiteConnector.BL();
sqlLiteBL.OpenDatabase(dbFilePath);
sqlLiteBL.TestPlanSave(testPlan);
using var sqlLiteBL = new SQLiteConnector.BL();
sqlLiteBL.OpenDatabase(dbFilePath);
sqlLiteBL.TestPlanSave(testPlan);

switch (commandFile.ProcessType)
{
case ReadReportData.Parameters.ProcessTypes.testPoints:
await ReadTestPoints(testPlan, commandFile.TestSuiteIds, adoRest, sqlLiteBL);
break;
case ReadReportData.Parameters.ProcessTypes.testRuns:
await ReadTestRuns(testPlan, adoRest, sqlLiteBL);
break;
case ReadReportData.Parameters.ProcessTypes.testResults:
await ReadTestResults(testPlan, commandFile.BatchSize, commandFile.KeepRecords, commandFile.TestSuiteIds, adoRest, sqlLiteBL);
break;
case ReadReportData.Parameters.ProcessTypes.workItems:
await ReadWorkItems(testPlan, adoRest, sqlLiteBL);
break;
switch (commandFile.ProcessType)
{
case ReadReportData.Parameters.ProcessTypes.testPoints:
await ReadTestPoints(testPlan, commandFile.TestSuiteIds, adoRest, sqlLiteBL);
break;
case ReadReportData.Parameters.ProcessTypes.testRuns:
await ReadTestRuns(testPlan, adoRest, sqlLiteBL);
break;
case ReadReportData.Parameters.ProcessTypes.testResults:
await ReadTestResults(testPlan, commandFile.BatchSize, commandFile.KeepRecords, commandFile.TestSuiteIds, adoRest, sqlLiteBL);
break;
case ReadReportData.Parameters.ProcessTypes.workItems:
await ReadWorkItems(testPlan, adoRest, sqlLiteBL);
break;
}
}
catch (Exception ex)
{
WriteException?.Invoke(this, new LogExeptionArgs() { Exception=ex});
}
}
private async Task ReadTestPoints(SQLite.BusinessObjects.TestPlan testPlan, List<int> testSuiteIds, ADORest.RestTestPlan adoRest, SQLiteConnector.BL sqlLiteBL)
Expand Down Expand Up @@ -193,7 +186,7 @@ private async Task ReadTestResults(SQLite.BusinessObjects.TestPlan testPlan, int
for (int i = 0; i < runIds.Count; i++)
{
if (i == 0 || i == runIds.Count - 1 || (i + 1) % 25 == 0)
WriteLog?.Invoke(this, new LogEventArgs() { Message = $"Test result for run {runIds[i]} ({i + 1}/{runIds.Count})" });
WriteLog?.Invoke(this, new LogEventArgs() { Message = $"Test results for run {runIds[i]} ({i + 1}/{runIds.Count})" });

var testStepResults = await adoRest.GetTestStepResults(runIds[i]);
if (testStepResults != null)
Expand Down
34 changes: 23 additions & 11 deletions STI Test Report/StartForm.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using ADORest.BusinessObjects;
using Newtonsoft.Json;
using PdfSharpCore.Internal;
using SQLite.BusinessObjects;
using System.Windows.Forms;

namespace STI_Test_Report
{
Expand Down Expand Up @@ -133,10 +132,13 @@ private void testPlan_textBox_TextChanged(object sender, EventArgs e)

_active = false;
testPlan_comboBox.DataSource = null;
if (testPlanFilter.Text == "")
this.testPlan_comboBox.DataSource = _testPlans;
else
this.testPlan_comboBox.DataSource = _testPlans.Where(tp => tp.Title.Contains(testPlanFilter.Text, StringComparison.CurrentCultureIgnoreCase) || tp.Id.ToString() == testPlanFilter.Text).ToList();
if (_testPlans != null)
{
if (testPlanFilter.Text == "")
this.testPlan_comboBox.DataSource = _testPlans;
else
this.testPlan_comboBox.DataSource = _testPlans.Where(tp => tp.Title.Contains(testPlanFilter.Text, StringComparison.CurrentCultureIgnoreCase) || tp.Id.ToString() == testPlanFilter.Text).ToList();
}
testPlan_comboBox.DisplayMember = "TitleId";
testPlan_comboBox.ValueMember = "Id";
this.testPlan_comboBox.SelectedIndex = -1;
Expand Down Expand Up @@ -265,7 +267,10 @@ async Task ReadReportData(ReadReportData.Parameters parameters)
{
readData = new ReadReportData();
readData.WriteLog += WriteStatus;
await readData.Start(parameters, _adoRest, teamProject, DBFilePath());
readData.WriteException += WriteException;

await Task.Run(() => readData.Start(parameters, _adoRest, teamProject, DBFilePath()));
//await readData.Start(parameters, _adoRest, teamProject, DBFilePath());
}
catch (Exception ex)
{
Expand All @@ -274,9 +279,11 @@ async Task ReadReportData(ReadReportData.Parameters parameters)
finally
{
readData.WriteLog -= WriteStatus;
readData.WriteException -= WriteException;
_logWriter.WriteLine(new string('-', 80));
_logWriter.Flush(); _logWriter.Dispose(); _logWriter = null;
this.Cursor = Cursors.Default;
this.print_button.Enabled = true;
_semaphore.Release();
}
}
Expand Down Expand Up @@ -415,12 +422,17 @@ static async Task<List<TestPlan>> GetTestPlans(ADORest.RestTestPlan adoRest)
#endregion

#region Logging
private void WriteStatus(string message)

private void WriteStatus(object sender, LogEventArgs e)
{
this.status_toolStripStatusLabel.Text = e.Message;
WriteLog(e.Message);
}
private void WriteException(object sender, LogExeptionArgs e)
{
this.status_toolStripStatusLabel.Text = message;
WriteLog(message);
this.status_toolStripStatusLabel.Text = $"Error: {e.Exception.Message}";
WriteLog(e.Exception);
}
private void WriteStatus(object sender, LogEventArgs e) => WriteStatus(e.Message);

int _logMsgNo = 0;
StreamWriter _logWriter = null;
Expand Down

0 comments on commit 3ec73ee

Please sign in to comment.