Skip to content

Commit 9d721c8

Browse files
kythantSergio0694
authored andcommitted
Add TestFailingCompletionHandlerCrashesProcess Test
1 parent 5bc0525 commit 9d721c8

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/Tests/TestComponentCSharp/Class.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,14 @@ namespace winrt::TestComponentCSharp::implementation
12271227
winrt::check_hresult(_asyncResult);
12281228
}
12291229

1230+
void Class::SetFailingCompletedHandler(IAsyncAction const& action)
1231+
{
1232+
action.Completed([](IAsyncAction const&, AsyncStatus)
1233+
{
1234+
throw winrt::hresult_error(E_FAIL, L"Intentional failure in completion handler");
1235+
});
1236+
}
1237+
12301238
IAsyncActionWithProgress<int32_t> Class::DoitAsyncWithProgress()
12311239
{
12321240
_asyncResult = E_PENDING;

src/Tests/TestComponentCSharp/Class.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ namespace winrt::TestComponentCSharp::implementation
352352
Windows::Foundation::IAsyncOperation<int32_t> AddAsync(int32_t lhs, int32_t rhs);
353353
Windows::Foundation::IAsyncOperationWithProgress<int32_t, int32_t> AddAsyncWithProgress(int32_t lhs, int32_t rhs);
354354

355+
void SetFailingCompletedHandler(Windows::Foundation::IAsyncAction const& action);
356+
355357
Windows::Foundation::Point PointProperty();
356358
void PointProperty(Windows::Foundation::Point const& value);
357359
Windows::Foundation::Rect RectProperty();

src/Tests/TestComponentCSharp/TestComponentCSharp.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ namespace TestComponentCSharp
440440
Windows.Foundation.IAsyncOperation<Int32> AddAsync(Int32 lhs, Int32 rhs);
441441
Windows.Foundation.IAsyncOperationWithProgress<Int32, Int32> AddAsyncWithProgress(Int32 lhs, Int32 rhs);
442442

443+
void SetFailingCompletedHandler(Windows.Foundation.IAsyncAction action);
444+
443445
// Type mappings
444446
// "Simple" structs (blittable with changes to add properties/functions/constructors/etc.)
445447
Windows.Foundation.Point PointProperty;

src/Tests/UnitTest/TestComponentCSharp_Tests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4853,5 +4853,24 @@ public void TestNontProjectedClassAsBaseClass()
48534853
// Non projected class changes the behavior of the Value property to double it.
48544854
Assert.AreEqual(6, customEquals.Value);
48554855
}
4856+
4857+
[TestMethod]
4858+
public async Task TestFailingCompletionHandlerCrashesProcess()
4859+
{
4860+
// Create an IAsyncAction from a C# task that completes after 2 seconds.
4861+
var asyncAction = AsyncInfo.Run(_ => Task.Delay(TimeSpan.FromSeconds(2)));
4862+
4863+
// Pass it to native code which sets a Completed handler that throws.
4864+
var instance = new Class();
4865+
instance.SetFailingCompletedHandler(asyncAction);
4866+
4867+
// Wait long enough for the async action to complete and the native
4868+
// completion handler to fire. If the throwing handler crashes the
4869+
// process, we will never reach the assertion below.
4870+
await Task.Delay(TimeSpan.FromSeconds(5));
4871+
4872+
// If we get here, the process survived the throwing completion handler.
4873+
Assert.IsTrue(true, "Process survived the throwing native completion handler.");
4874+
}
48564875
}
48574876
}

0 commit comments

Comments
 (0)