forked from microsoft/cppwinrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_check_cancel.cpp
More file actions
118 lines (98 loc) · 2.9 KB
/
async_check_cancel.cpp
File metadata and controls
118 lines (98 loc) · 2.9 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
namespace
{
#ifdef __cpp_lib_coroutine
using std::suspend_never;
#else
using std::experimental::suspend_never;
#endif
//
// Checks that manual cancellation checks work.
//
IAsyncAction Action(HANDLE event, bool& canceled)
{
co_await resume_on_signal(event);
auto cancel = co_await get_cancellation_token();
if (cancel())
{
REQUIRE(!canceled);
canceled = true;
}
co_await suspend_never();
REQUIRE(false);
}
IAsyncActionWithProgress<int> ActionWithProgress(HANDLE event, bool& canceled)
{
co_await resume_on_signal(event);
auto cancel = co_await get_cancellation_token();
if (cancel())
{
REQUIRE(!canceled);
canceled = true;
}
co_await suspend_never();
REQUIRE(false);
}
IAsyncOperation<int> Operation(HANDLE event, bool& canceled)
{
co_await resume_on_signal(event);
auto cancel = co_await get_cancellation_token();
if (cancel())
{
REQUIRE(!canceled);
canceled = true;
}
co_await suspend_never();
REQUIRE(false);
co_return 1;
}
IAsyncOperationWithProgress<int, int> OperationWithProgress(HANDLE event, bool& canceled)
{
co_await resume_on_signal(event);
auto cancel = co_await get_cancellation_token();
if (cancel())
{
REQUIRE(!canceled);
canceled = true;
}
co_await suspend_never();
REQUIRE(false);
co_return 1;
}
template <typename F>
void Check(F make)
{
handle start{ CreateEvent(nullptr, true, false, nullptr) };
handle completed{ CreateEvent(nullptr, true, false, nullptr) };
bool canceled = false;
auto async = make(start.get(), canceled);
REQUIRE(async.Status() == AsyncStatus::Started);
async.Completed([&](auto&& sender, AsyncStatus status)
{
REQUIRE(async == sender);
REQUIRE(status == AsyncStatus::Canceled);
REQUIRE(canceled);
SetEvent(completed.get());
});
async.Cancel();
SetEvent(start.get());
REQUIRE(WaitForSingleObject(completed.get(), 1000) == WAIT_OBJECT_0);
REQUIRE(async.Status() == AsyncStatus::Canceled);
REQUIRE(async.ErrorCode() == HRESULT_FROM_WIN32(ERROR_CANCELLED));
REQUIRE_THROWS_AS(async.GetResults(), hresult_canceled);
}
}
#if defined(__clang__) && defined(_MSC_VER)
// FIXME: Test is known to segfault when built with Clang.
TEST_CASE("async_check_cancel", "[.clang-crash]")
#else
TEST_CASE("async_check_cancel")
#endif
{
Check(Action);
Check(ActionWithProgress);
Check(Operation);
Check(OperationWithProgress);
}