Skip to content

Commit

Permalink
fix(samples): Ensure the app can finish even in exceptional cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 24, 2020
1 parent 675dcd4 commit a769295
Showing 1 changed file with 89 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,108 +254,122 @@ internal async Task RecordAllTests(CancellationToken ct, string screenShotPath =

await DumpOutputFolderName(ct, folderName);


await Window.Current.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
async () =>
{
await RecordAllTestsInner(folderName, ct, doneAction);
});
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().Error("RecordAllTests exception", e);
}
}
}

private async Task RecordAllTestsInner(string folderName, CancellationToken ct, Action doneAction = null)
{
try
{
#if XAMARIN
var initialInactiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetInactiveViewReferencesStats();
var initialActiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetReferenceStats();
var initialInactiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetInactiveViewReferencesStats();
var initialActiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetReferenceStats();
#endif
var testQuery = from category in _categories
from sample in category.SamplesContent
where !sample.IgnoreInSnapshotTests
// where sample.ControlName.Equals("GridViewVerticalGrouped")
select new SampleInfo
{
Category = category,
Sample = sample,
};
Debug.Assert(
_firstTargetToRun.IsNullOrEmpty() || testQuery.Any(testInfo => testInfo.Matches(_firstTargetToRun)),
"First target to run must be either a Category or a Sample that is present in the app."
);
Debug.Assert(
_targetsToSkip.Where(target => !target.IsNullOrWhiteSpace()).None(target => target.Equals(_firstTargetToRun, StringComparison.OrdinalIgnoreCase)),
"First test to run cannot be skipped"
);
var tests = testQuery
.SkipWhile(testInfo => _firstTargetToRun.HasValue() && !testInfo.Matches(_firstTargetToRun))
.Where(testInfo => _targetsToSkip.None(testInfo.Matches))
.ToArray();
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Generating tests for {tests.Count()} test in {folderName}");
}
var testQuery = from category in _categories
from sample in category.SamplesContent
where !sample.IgnoreInSnapshotTests
// where sample.ControlName.Equals("GridViewVerticalGrouped")
select new SampleInfo
{
Category = category,
Sample = sample,
};

Debug.Assert(
_firstTargetToRun.IsNullOrEmpty() || testQuery.Any(testInfo => testInfo.Matches(_firstTargetToRun)),
"First target to run must be either a Category or a Sample that is present in the app."
);

Debug.Assert(
_targetsToSkip.Where(target => !target.IsNullOrWhiteSpace()).None(target => target.Equals(_firstTargetToRun, StringComparison.OrdinalIgnoreCase)),
"First test to run cannot be skipped"
);

var tests = testQuery
.SkipWhile(testInfo => _firstTargetToRun.HasValue() && !testInfo.Matches(_firstTargetToRun))
.Where(testInfo => _targetsToSkip.None(testInfo.Matches))
.ToArray();

if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Generating tests for {tests.Count()} test in {folderName}");
}

foreach (var sample in tests)
foreach (var sample in tests)
{
try
{
try
{
#if XAMARIN
var inactiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetInactiveViewReferencesStats();
var activeStats = Uno.UI.DataBinding.BinderReferenceHolder.GetReferenceStats();
var inactiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetInactiveViewReferencesStats();
var activeStats = Uno.UI.DataBinding.BinderReferenceHolder.GetReferenceStats();
#endif

var fileName = $"{sample.Category.Category}-{sample.Sample.ControlName}.png";
var fileName = $"{sample.Category.Category}-{sample.Sample.ControlName}.png";

try
{
LogMemoryStatistics();
try
{
LogMemoryStatistics();

if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Generating {folderName}\\{fileName}");
}
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Generating {folderName}\\{fileName}");
}

await ShowNewSection(ct, Section.SamplesContent);
await ShowNewSection(ct, Section.SamplesContent);

SelectedLibrarySample = sample.Sample;
SelectedLibrarySample = sample.Sample;

var content = await UpdateContent(ct, sample.Sample) as FrameworkElement;
var content = await UpdateContent(ct, sample.Sample) as FrameworkElement;

ContentPhone = content;
ContentPhone = content;

await Task.Delay(500, ct);
await Task.Delay(500, ct);

await GenerateBitmap(ct, folderName, fileName, content);
}
catch (Exception e)
{
this.Log().Error($"Failed to execute test for {fileName}", e);
}
await GenerateBitmap(ct, folderName, fileName, content);
}
catch (Exception e)
{
this.Log().Error($"Failed to execute test for {fileName}", e);
}

#if XAMARIN
Uno.UI.DataBinding.BinderReferenceHolder.LogInactiveViewReferencesStatsDiff(inactiveStats);
Uno.UI.DataBinding.BinderReferenceHolder.LogActiveViewReferencesStatsDiff(activeStats);
Uno.UI.DataBinding.BinderReferenceHolder.LogInactiveViewReferencesStatsDiff(inactiveStats);
Uno.UI.DataBinding.BinderReferenceHolder.LogActiveViewReferencesStatsDiff(activeStats);
#endif
}
catch (Exception e)
}
catch (Exception e)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().Error("Exception", e);
}
this.Log().Error("Exception", e);
}
}
}

ContentPhone = null;
ContentPhone = null;

if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Final binder reference stats");
}
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Final binder reference stats");
}

#if XAMARIN
Uno.UI.DataBinding.BinderReferenceHolder.LogInactiveViewReferencesStatsDiff(initialInactiveStats);
Uno.UI.DataBinding.BinderReferenceHolder.LogActiveViewReferencesStatsDiff(initialActiveStats);
Uno.UI.DataBinding.BinderReferenceHolder.LogInactiveViewReferencesStatsDiff(initialInactiveStats);
Uno.UI.DataBinding.BinderReferenceHolder.LogActiveViewReferencesStatsDiff(initialActiveStats);
#endif
});
}
catch (Exception e)
{
Expand All @@ -366,12 +380,11 @@ from sample in category.SamplesContent
}
finally
{
IsSplitVisible = true;

// Done action is needed as awaiting the task is not enough to deterine the end of this method.
// Done action is needed as awaiting the task is not enough to determine the end of this method.
doneAction?.Invoke();
}

IsSplitVisible = true;
}
}

partial void LogMemoryStatistics();
Expand Down

0 comments on commit a769295

Please sign in to comment.