Skip to content

Commit 6fb34e5

Browse files
committed
Add TestFailingCompletionHandlerCrashesProcess Test
1 parent 4c6001a commit 6fb34e5

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
@@ -1213,6 +1213,14 @@ namespace winrt::TestComponentCSharp::implementation
12131213
winrt::check_hresult(_asyncResult);
12141214
}
12151215

1216+
void Class::SetFailingCompletedHandler(IAsyncAction const& action)
1217+
{
1218+
action.Completed([](IAsyncAction const&, AsyncStatus)
1219+
{
1220+
throw winrt::hresult_error(E_FAIL, L"Intentional failure in completion handler");
1221+
});
1222+
}
1223+
12161224
IAsyncActionWithProgress<int32_t> Class::DoitAsyncWithProgress()
12171225
{
12181226
_asyncResult = E_PENDING;

src/Tests/TestComponentCSharp/Class.h

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

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

src/Tests/TestComponentCSharp/TestComponentCSharp.idl

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

432+
void SetFailingCompletedHandler(Windows.Foundation.IAsyncAction action);
433+
432434
// Type mappings
433435
// "Simple" structs (blittable with changes to add properties/functions/constructors/etc.)
434436
Windows.Foundation.Point PointProperty;

src/Tests/UnitTest/TestComponentCSharp_Tests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4092,5 +4092,24 @@ public void TestNontProjectedClassAsBaseClass()
40924092
// Non projected class changes the behavior of the Value property to double it.
40934093
Assert.AreEqual(6, customEquals.Value);
40944094
}
4095+
4096+
[TestMethod]
4097+
public async Task TestFailingCompletionHandlerCrashesProcess()
4098+
{
4099+
// Create an IAsyncAction from a C# task that completes after 2 seconds.
4100+
var asyncAction = AsyncInfo.Run(_ => Task.Delay(TimeSpan.FromSeconds(2)));
4101+
4102+
// Pass it to native code which sets a Completed handler that throws.
4103+
var instance = new Class();
4104+
instance.SetFailingCompletedHandler(asyncAction);
4105+
4106+
// Wait long enough for the async action to complete and the native
4107+
// completion handler to fire. If the throwing handler crashes the
4108+
// process, we will never reach the assertion below.
4109+
await Task.Delay(TimeSpan.FromSeconds(5));
4110+
4111+
// If we get here, the process survived the throwing completion handler.
4112+
Assert.IsTrue(true, "Process survived the throwing native completion handler.");
4113+
}
40954114
}
40964115
}

0 commit comments

Comments
 (0)