Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Semaphore for AttachAndRun #7269

Merged
merged 1 commit into from May 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/TestUtils/src/DeviceTests/AssertionExtensions.Android.cs
Expand Up @@ -95,6 +95,10 @@ public static AColor ColorAtPoint(this Bitmap bitmap, int x, int y, bool include
return true;
});

// Android doesn't handle adding and removing views in parallel very well
// If a view is removed while a different test triggers a layout then you hit
// a NRE exception
static SemaphoreSlim _attachAndRunSemaphore = new SemaphoreSlim(1);
public static async Task<T> AttachAndRun<T>(this AView view, Func<Task<T>> action)
{
if (view.Parent is WrapperView wrapper)
Expand All @@ -115,17 +119,21 @@ public static async Task<T> AttachAndRun<T>(this AView view, Func<Task<T>> actio
var act = context.GetActivity()!;
var rootView = act.FindViewById<FrameLayout>(Android.Resource.Id.Content)!;

layout.AddView(view);
rootView.AddView(layout);
view.Id = AView.GenerateViewId();
layout.Id = AView.GenerateViewId();

try
{
await _attachAndRunSemaphore.WaitAsync();
layout.AddView(view);
rootView.AddView(layout);
return await Run(view, action);
}
finally
{
rootView.RemoveView(layout);
layout.RemoveView(view);
_attachAndRunSemaphore.Release();
}
}
else
Expand Down