Skip to content

Commit

Permalink
[Housekeeping] Fix `The active test run was aborted. Reason: Test hos…
Browse files Browse the repository at this point in the history
…t process crashed` (#2206)
  • Loading branch information
brminnick authored Sep 13, 2024
1 parent 404e3e4 commit dfd8ed8
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 90 deletions.
4 changes: 4 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ jobs:

- task: CmdLine@2
displayName: 'Run CommunityToolkit.Maui.Analyzers.UnitTests'
env:
VSTEST_TESTHOST_SHUTDOWN_TIMEOUT: 1100 # Fixes "The active test run was aborted. Reason: Test host process crashed" https://dev.azure.com/dotnet/CommunityToolkit/_build/results?buildId=109660&view=logs&j=3f96dcf5-6e1e-5485-3200-c557d5216be3&t=12286b69-c788-55db-0a8c-ef899858fbe6&l=76 (source: https://github.com/microsoft/vstest/issues/2952#issuecomment-2234253765): $(sauceUsername)
inputs:
script: 'dotnet test $(PathToCommunityToolkitAnalyzersUnitTestCsproj) -c Release'

Expand Down Expand Up @@ -297,6 +299,8 @@ jobs:

- task: CmdLine@2
displayName: 'Run CommunityToolkit.Maui.UnitTests'
env:
VSTEST_TESTHOST_SHUTDOWN_TIMEOUT: 1100 # Fixes "The active test run was aborted. Reason: Test host process crashed" https://dev.azure.com/dotnet/CommunityToolkit/_build/results?buildId=109660&view=logs&j=3f96dcf5-6e1e-5485-3200-c557d5216be3&t=12286b69-c788-55db-0a8c-ef899858fbe6&l=76 (source: https://github.com/microsoft/vstest/issues/2952#issuecomment-2234253765): $(sauceUsername)
inputs:
script: 'dotnet test $(PathToCommunityToolkitUnitTestCsproj) -c Release --settings ".runsettings" --collect "XPlat code coverage" --logger trx --results-directory $(Agent.TempDirectory)'

Expand Down
11 changes: 6 additions & 5 deletions samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Maui.Sample.Pages;
using System.Collections.ObjectModel;
using CommunityToolkit.Maui.Sample.Pages;
using CommunityToolkit.Maui.Sample.Pages.Alerts;
using CommunityToolkit.Maui.Sample.Pages.Behaviors;
using CommunityToolkit.Maui.Sample.Pages.Converters;
Expand All @@ -24,8 +25,8 @@ namespace CommunityToolkit.Maui.Sample;

public partial class AppShell : Shell
{
static readonly IReadOnlyDictionary<Type, (Type GalleryPageType, Type ContentPageType)> viewModelMappings = new Dictionary<Type, (Type, Type)>(new[]
{
static readonly ReadOnlyDictionary<Type, (Type GalleryPageType, Type ContentPageType)> viewModelMappings = new Dictionary<Type, (Type, Type)>(
[
// Add Alerts View Models
CreateViewModelMapping<SnackbarPage, SnackbarViewModel, AlertsGalleryPage, AlertsGalleryViewModel>(),
CreateViewModelMapping<ToastPage, ToastViewModel, AlertsGalleryPage, AlertsGalleryViewModel>(),
Expand Down Expand Up @@ -137,8 +138,8 @@ public partial class AppShell : Shell
CreateViewModelMapping<StylePopupPage, StylePopupViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),

// Add PlatformSpecific View Models
CreateViewModelMapping<NavigationBarPage, NavigationBarAndroidViewModel, PlatformSpecificGalleryPage, PlatformSpecificGalleryViewModel>(),
});
CreateViewModelMapping<NavigationBarPage, NavigationBarAndroidViewModel, PlatformSpecificGalleryPage, PlatformSpecificGalleryViewModel>()
]).AsReadOnly();

public AppShell()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public StringToListConverterPage(StringToListConverterViewModel stringToListConv
Resources.Add(nameof(StringToListConverter), new StringToListConverter
{
SplitOptions = StringSplitOptions.RemoveEmptyEntries,
Separators = new[] { ",", ".", ";" }
Separators = [",", ".", ";"]
});

InitializeComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public partial class ItemTappedEventArgsConverterViewModel : BaseViewModel
[ObservableProperty]
Person? itemSelected = null;

public IReadOnlyList<Person> Items { get; } = new[]
{
public IReadOnlyList<Person> Items { get; } =
[
new Person(1, "John Doe"),
new Person(2, "Jane Doe"),
new Person(3, "Joe Doe")
};
];

[RelayCommand]
Task ItemTapped(Person? person, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ namespace CommunityToolkit.Maui.Sample.ViewModels.Converters;

public class ListToStringConverterViewModel : BaseViewModel
{
public IReadOnlyList<string> ItemSource { get; } = new[]
{
public IReadOnlyList<string> ItemSource { get; } =
[
"This",
"Is",
"The",
"ListToStringConverter"
};
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public partial class SelectedItemEventArgsConverterViewModel : BaseViewModel
[ObservableProperty]
string labelText = "This label will display the selected item";

public IReadOnlyList<string> StringItemSource { get; } = new[]
{
public IReadOnlyList<string> StringItemSource { get; } =
[
"Item 0",
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
};
"Item 5"
];

[RelayCommand]
void HandleItemSelected(string text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public partial class GravatarImageSourceViewModel : BaseViewModel

public TimeSpan CacheValidityTimespan => TimeSpan.FromDays(CacheValidityInDays);

public IReadOnlyList<DefaultImage> DefaultGravatarItems { get; } = new[]
{
public IReadOnlyList<DefaultImage> DefaultGravatarItems { get; } =
[
DefaultImage.MysteryPerson,
DefaultImage.FileNotFound,
DefaultImage.Identicon,
Expand All @@ -30,5 +30,5 @@ public partial class GravatarImageSourceViewModel : BaseViewModel
DefaultImage.Robohash,
DefaultImage.Wavatar,
DefaultImage.Blank
};
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CommunityToolkit.Maui.Camera.Analyzers;
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(UseCommunityToolkitCameraInitializationAnalyzerCodeFixProvider)), Shared]
public class UseCommunityToolkitCameraInitializationAnalyzerCodeFixProvider : CodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(UseCommunityToolkitCameraInitializationAnalyzer.DiagnosticId);
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = [UseCommunityToolkitCameraInitializationAnalyzer.DiagnosticId];

public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UseCommunityToolkitCameraInitializationAnalyzer : DiagnosticAnalyze

static readonly DiagnosticDescriptor rule = new(DiagnosticId, title, messageFormat, category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: description);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(rule);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [rule];

public override void Initialize(AnalysisContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public static ValueTask<Stream> GetImageStream(IEnumerable<PointF> points,
/// <returns><see cref="ValueTask{Stream}"/> containing the data of the requested image with data that's currently on the <see cref="IDrawingView"/>.</returns>
public ValueTask<Stream> GetImageStream(double imageSizeWidth, double imageSizeHeight, Paint background, CancellationToken token = default)
{
return DrawingViewService.GetImageStream(Points.ToList(), new Size(imageSizeWidth, imageSizeHeight), LineWidth, LineColor, background, token);
return DrawingViewService.GetImageStream([.. Points], new Size(imageSizeWidth, imageSizeHeight), LineWidth, LineColor, background, token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sealed class AndroidMediaPermissions : BasePlatformPermission

static (string androidPermission, bool isRuntime)[] CreatePermissions()
{
List<(string androidPermission, bool isRuntime)> requiredPermissionsList = new();
List<(string androidPermission, bool isRuntime)> requiredPermissionsList = [];

if (OperatingSystem.IsAndroidVersionAtLeast(28))
{
Expand Down
8 changes: 4 additions & 4 deletions src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ protected BaseHandlerTest()

protected IServiceProvider ServiceProvider { get; }

protected static TElementHandler CreateElementHandler<TElementHandler>(IElement view, bool hasMauiContext = true)
protected static TElementHandler CreateElementHandler<TElementHandler>(IElement view, bool doesRequireMauiContext = true)
where TElementHandler : IElementHandler, new()
{
var mockElementHandler = new TElementHandler();
mockElementHandler.SetVirtualView(view);

if (hasMauiContext)
if (doesRequireMauiContext)
{
mockElementHandler.SetMauiContext(Application.Current?.Handler?.MauiContext ?? throw new NullReferenceException());
}

return mockElementHandler;
}

protected static TViewHandler CreateViewHandler<TViewHandler>(IView view, bool hasMauiContext = true)
protected static TViewHandler CreateViewHandler<TViewHandler>(IView view, bool doesRequireMauiContext = true)
where TViewHandler : IViewHandler, new()
{
var mockViewHandler = new TViewHandler();
mockViewHandler.SetVirtualView(view);

if (hasMauiContext)
if (doesRequireMauiContext)
{
mockViewHandler.SetMauiContext(Application.Current?.Handler?.MauiContext ?? throw new NullReferenceException());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public void ValidMaskWithUniqueUnmaskedCharacterTests(string? mask, char unmaske
[Fact]
public void AttachedToInvalidElementTest()
{
IReadOnlyList<VisualElement> invalidVisualElements = new[]
{
IReadOnlyList<VisualElement> invalidVisualElements =
[
new Button(),
new Frame(),
new Label(),
new ProgressBar(),
new VisualElement(),
new View(),
};
new View()
];

foreach (var invalidVisualElement in invalidVisualElements)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void StringToListConverterConverterParameterTest(string? value, object? p
var stringToListConverter = new StringToListConverter
{
Separator = "~",
Separators = new[] { "@", "*" }
Separators = ["@", "*"]
};

var convertFromResult = stringToListConverter.ConvertFrom(value, parameter);
Expand All @@ -37,13 +37,16 @@ public void StringToListConverterConverterParameterTest(string? value, object? p
[Fact]
public void StringToListConverter_EnsureParameterDoesNotOverrideProperty()
{
var converter = new StringToListConverter { Separators = new[] { ",", " " } };
var converter = new StringToListConverter
{
Separators = [",", " "]
};

var parameterResult = converter.ConvertFrom("maui/toolkit tests", new[] { "/", " " });
Assert.Equal(new[] { "maui", "toolkit", "tests" }, parameterResult);
Assert.Equal(["maui", "toolkit", "tests"], parameterResult);

var propertyResult = converter.ConvertFrom("maui,toolkit tests");
Assert.Equal(new[] { "maui", "toolkit", "tests" }, propertyResult);
Assert.Equal(["maui", "toolkit", "tests"], propertyResult);
}

[Fact]
Expand All @@ -55,7 +58,7 @@ public void StringToListConverterSeparatorsTest()
var stringToListConverter = new StringToListConverter
{
Separator = "~",
Separators = new[] { "@", "*" }
Separators = ["@", "*"]
};

var convertFromResult = stringToListConverter.ConvertFrom(valueToConvert);
Expand Down
11 changes: 5 additions & 6 deletions src/CommunityToolkit.Maui.UnitTests/Mocks/MockCameraProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ public class MockCameraProvider : ICameraProvider

public ValueTask RefreshAvailableCameras(CancellationToken token)
{
AvailableCameras = new[]
{
AvailableCameras =
[
new CameraInfo("Test Camera",
Guid.NewGuid().ToString(),
CameraPosition.Front,
false,
1.0f,
5.0f,
new[]
{
[
new Size(1920, 1080)
})
};
])
];

return ValueTask.CompletedTask;
}
Expand Down
45 changes: 20 additions & 25 deletions src/CommunityToolkit.Maui.UnitTests/Mocks/MockDrawingViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
using CommunityToolkit.Maui.Core.Extensions;
using CommunityToolkit.Maui.Core.Handlers;
using CommunityToolkit.Maui.Core.Views;
using CommunityToolkit.Maui.Views;
using Microsoft.Maui.Handlers;

namespace CommunityToolkit.Maui.UnitTests.Mocks;

public class MockDrawingViewHandler : ViewHandler<IDrawingView, object>, IDrawingViewHandler
public sealed class MockDrawingViewHandler(IPropertyMapper mapper) : ViewHandler<IDrawingView, DrawingView>(mapper), IDrawingViewHandler
{
IDrawingLineAdapter adapter = new DrawingLineAdapter();

public static readonly PropertyMapper<IDrawingView, MockDrawingViewHandler> DrawingViewPropertyMapper = new(ViewMapper)
public static readonly PropertyMapper<IDrawingView, MockDrawingViewHandler> DrawingViewPropertyMapper = new()
{
[nameof(IDrawingView.LineWidth)] = MapLineWidth,
[nameof(IDrawingView.LineColor)] = MapLineColor,
Expand All @@ -24,32 +25,36 @@ public MockDrawingViewHandler() : this(DrawingViewPropertyMapper)
{
}

public MockDrawingViewHandler(IPropertyMapper mapper) : base(mapper)
{
public List<MauiDrawingLine> Lines { get; } = [];
public int MapLineWidthCount { get; private set; }
public int MapLineColorCount { get; private set; }
public int MapShouldSmoothPathWhenDrawnCount { get; private set; }
public int MapIsMultiLineModeEnabledCount { get; private set; }
public int MapDrawCount { get; private set; }

/// <inheritdoc />
public void SetDrawingLineAdapter(IDrawingLineAdapter drawingLineAdapter)
{
adapter = drawingLineAdapter;
}

/// <inheritdoc />
protected override void ConnectHandler(object platformView)
protected override DrawingView CreatePlatformView() => new();

/// <inheritdoc />
protected override void ConnectHandler(DrawingView platformView)
{
base.ConnectHandler(platformView);
VirtualView.Lines.CollectionChanged += Lines_CollectionChanged;
}

/// <inheritdoc />
protected override void DisconnectHandler(object platformView)
protected override void DisconnectHandler(DrawingView platformView)
{
base.DisconnectHandler(platformView);
VirtualView.Lines.CollectionChanged -= Lines_CollectionChanged;
}

public int MapLineWidthCount { get; private set; }
public int MapLineColorCount { get; private set; }
public int MapShouldSmoothPathWhenDrawnCount { get; private set; }
public int MapIsMultiLineModeEnabledCount { get; private set; }
public int MapDrawCount { get; private set; }
public List<MauiDrawingLine> Lines { get; } = [];

static void MapLineWidth(MockDrawingViewHandler arg1, IDrawingView arg2)
{
arg1.MapLineWidthCount++;
Expand All @@ -75,11 +80,6 @@ static void MapDrawAction(MockDrawingViewHandler arg1, IDrawingView arg2)
arg1.MapDrawCount++;
}

protected override object CreatePlatformView()
{
return new object();
}

void Lines_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Lines.Clear();
Expand All @@ -101,14 +101,9 @@ void Lines_CollectionChanged(object? sender, System.Collections.Specialized.Noti
VirtualView.OnDrawingLineCompleted(drawingLine);
}
}

public void SetDrawingLineAdapter(IDrawingLineAdapter drawingLineAdapter)
{
adapter = drawingLineAdapter;
}
}

class MockDrawingLineAdapter : IDrawingLineAdapter
sealed class MockDrawingLineAdapter : IDrawingLineAdapter
{
public IDrawingLine ConvertMauiDrawingLine(MauiDrawingLine mauiDrawingLine)
{
Expand All @@ -123,7 +118,7 @@ public IDrawingLine ConvertMauiDrawingLine(MauiDrawingLine mauiDrawingLine)
}
}

class MockDrawingLine : IDrawingLine
sealed class MockDrawingLine : IDrawingLine
{
public int Granularity { get; set; }
public Color LineColor { get; set; } = Colors.Blue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public void GranularityCheckRange(int value, int expectedValue)
drawingLine.Granularity.Should().Be(expectedValue);
}

[Fact(Timeout = (int)TestDuration.Short)]
[Fact(Timeout = (int)TestDuration.Medium)]
public async Task GetImageStream_CancellationTokenExpired()
{
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(1));

// Ensure CancellationToken Expired
await Task.Delay(100, CancellationToken.None);
await Task.Delay(TimeSpan.FromSeconds(1), CancellationToken.None);

await Assert.ThrowsAsync<OperationCanceledException>(async () => await drawingLine.GetImageStream(10, 10, Colors.Blue.AsPaint(), cts.Token));
}
Expand Down
Loading

0 comments on commit dfd8ed8

Please sign in to comment.