Repository navigation
Expand file tree
/
Copy pathasync_cancel_callback.cpp
More file actions
101 lines (86 loc) · 2.48 KB
/
Copy pathasync_cancel_callback.cpp
File metadata and controls
101 lines (86 loc) · 2.48 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
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
namespace
{
//
// Checks that the cancellation callback is invoked.
//
IAsyncAction Action(HANDLE event, bool& canceled)
{
// Put the cancellation token into a lambda just to make
// sure it's possible.
[cancel = co_await get_cancellation_token(), &canceled]
{
cancel.callback([&]
{
REQUIRE(!canceled);
canceled = true;
});
}();
co_await resume_on_signal(event);
co_await std::suspend_never();
REQUIRE(false);
}
IAsyncActionWithProgress<int> ActionWithProgress(HANDLE event, bool& canceled)
{
auto cancel = co_await get_cancellation_token();
cancel.callback([&]
{
REQUIRE(!canceled);
canceled = true;
});
co_await resume_on_signal(event);
co_await std::suspend_never();
REQUIRE(false);
}
IAsyncOperation<int> Operation(HANDLE event, bool& canceled)
{
auto cancel = co_await get_cancellation_token();
cancel.callback([&]
{
REQUIRE(!canceled);
canceled = true;
});
co_await resume_on_signal(event);
co_await std::suspend_never();
REQUIRE(false);
co_return 1;
}
IAsyncOperationWithProgress<int, int> OperationWithProgress(HANDLE event, bool& canceled)
{
auto cancel = co_await get_cancellation_token();
cancel.callback([&]
{
REQUIRE(!canceled);
canceled = true;
});
co_await resume_on_signal(event);
co_await std::suspend_never();
REQUIRE(false);
co_return 1;
}
template <typename F>
void Check(F make)
{
handle event{ CreateEvent(nullptr, true, false, nullptr) };
bool canceled = false;
auto async = make(event.get(), canceled);
async.Cancel();
REQUIRE(canceled);
SetEvent(event.get());
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_cancel_callback", "[.clang-crash]")
#else
TEST_CASE("async_cancel_callback")
#endif
{
Check(Action);
Check(ActionWithProgress);
Check(Operation);
Check(OperationWithProgress);
}