forked from microsoft/cppwinrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.cpp
More file actions
92 lines (78 loc) · 2.09 KB
/
async.cpp
File metadata and controls
92 lines (78 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <Windows.h>
#include "catch.hpp"
import std;
import Windows.Foundation;
using namespace winrt;
using namespace Windows::Foundation;
using namespace std::chrono_literals;
namespace
{
//
// Just some quick tests to make sure that coroutines compile and work with C++20 modules.
// Taken from async_throw in test
//
// TODO: make a new project for this
IAsyncAction Action()
{
co_await 10ms;
throw hresult_invalid_argument(L"Async");
}
IAsyncActionWithProgress<int> ActionWithProgress()
{
co_await 10ms;
throw hresult_invalid_argument(L"Async");
}
IAsyncOperation<int> Operation()
{
co_await 10ms;
throw hresult_invalid_argument(L"Async");
co_return 1;
}
IAsyncOperationWithProgress<int, int> OperationWithProgress()
{
co_await 10ms;
throw hresult_invalid_argument(L"Async");
co_return 1;
}
template <typename F>
void Check(F make)
{
try
{
make().get();
REQUIRE(false);
}
catch (hresult_invalid_argument const& e)
{
REQUIRE(e.message() == L"Async");
}
handle completed{ CreateEvent(nullptr, true, false, nullptr) };
auto async = make();
async.Completed([&](auto&& sender, AsyncStatus status)
{
REQUIRE(async == sender);
REQUIRE(status == AsyncStatus::Error);
SetEvent(completed.get());
});
REQUIRE(WaitForSingleObject(completed.get(), 1000) == WAIT_OBJECT_0);
REQUIRE(async.Status() == AsyncStatus::Error);
hresult_error e(async.ErrorCode(), take_ownership_from_abi);
REQUIRE(e.message() == L"Async");
try
{
async.GetResults();
REQUIRE(false);
}
catch (hresult_invalid_argument const& e)
{
REQUIRE(e.message() == L"Async");
}
}
}
TEST_CASE("async_throw")
{
Check(Action);
Check(ActionWithProgress);
Check(Operation);
Check(OperationWithProgress);
}