-
Notifications
You must be signed in to change notification settings - Fork 0
/
coroutine-test.c++
191 lines (148 loc) · 5.84 KB
/
coroutine-test.c++
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2017 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "./coroutine.h"
#include <kj/compat/http.h>
#include <kj/debug.h>
#include <kj/test.h>
#include <map>
namespace kj {
namespace {
#ifdef KJ_HAVE_COROUTINE
KJ_TEST("Identity coroutine") {
EventLoop loop;
WaitScope waitScope(loop);
auto identity = [](auto&& x) -> kj::Promise<kj::Decay<decltype(x)>> {
co_return kj::fwd<decltype(x)>(x);
};
KJ_EXPECT(identity(123).wait(waitScope) == 123);
KJ_EXPECT(*identity(kj::heap(456)).wait(waitScope) == 456);
{
auto p = identity("we can cancel the coroutine");
}
}
Promise<int> simpleCoroutine(kj::Promise<int> result, kj::Promise<bool> dontThrow = true) {
KJ_ASSERT(co_await dontThrow);
co_return co_await result;
}
KJ_TEST("Simple coroutine test") {
EventLoop loop;
WaitScope waitScope(loop);
KJ_EXPECT(simpleCoroutine(123).wait(waitScope) == 123);
}
KJ_TEST("Exceptions propagate through layered coroutines") {
EventLoop loop;
WaitScope waitScope(loop);
auto throwy = simpleCoroutine(kj::NEVER_DONE, false);
KJ_EXPECT_THROW_RECOVERABLE(FAILED, simpleCoroutine(kj::mv(throwy)).wait(waitScope));
}
KJ_TEST("Coroutines can be canceled while suspended") {
EventLoop loop;
WaitScope waitScope(loop);
size_t count = 0;
struct Counter {
size_t& count;
Counter(size_t& count): count(count) {}
~Counter() { ++count; }
KJ_DISALLOW_COPY(Counter);
};
{
auto neverDone = kj::Promise<int>(kj::NEVER_DONE);
neverDone = neverDone.attach(kj::heap<Counter>(count));
auto promise = simpleCoroutine(kj::mv(neverDone));
}
KJ_EXPECT(count == 1);
}
Promise<void> sendData(Promise<Own<NetworkAddress>> addressPromise) {
auto address = co_await addressPromise;
auto client = co_await address->connect();
co_await client->write("foo", 3);
}
Promise<String> receiveDataChained(Promise<Own<NetworkAddress>> addressPromise) {
auto buffer = heapArray<char>(64);
auto buf = ArrayPtr<char>(buffer);
return addressPromise.then([](auto addr) {
auto listener = addr->listen();
return listener->accept().attach(mv(listener));
}).then([buf](auto stream) mutable {
return stream->read(buf.begin(), 1, buf.size()).attach(mv(stream));
}).then([buf](size_t n) {
KJ_EXPECT(3u == n);
return heapString(buf.begin(), n);
}).attach(mv(buffer));
}
Promise<String> receiveDataCoroutine(Promise<Own<NetworkAddress>> addressPromise) {
auto address = co_await addressPromise;
auto listener = address->listen();
auto server = co_await listener->accept();
char buffer[4];
auto n = co_await server->read(buffer, 3, 4);
KJ_EXPECT(3u == n);
co_return heapString(buffer, n);
}
KJ_TEST("Simple network test with chained promise implementation") {
auto io = setupAsyncIo();
auto& network = io.provider->getNetwork();
constexpr uint port = 5500;
sendData(network.parseAddress("localhost", port)).detach([](Exception&& exception) {
KJ_FAIL_EXPECT(exception);
});
String result = receiveDataChained(network.parseAddress("*", port)).wait(io.waitScope);
KJ_EXPECT("foo" == result);
}
KJ_TEST("Simple network test with coroutine implementation") {
auto io = setupAsyncIo();
auto& network = io.provider->getNetwork();
constexpr uint port = 5500;
sendData(network.parseAddress("localhost", port)).detach([](Exception&& exception) {
KJ_FAIL_EXPECT(exception);
});
String result = receiveDataCoroutine(network.parseAddress("*", port)).wait(io.waitScope);
KJ_EXPECT("foo" == result);
}
Promise<Own<AsyncIoStream>> httpClientConnect(AsyncIoContext& io) {
auto addr = co_await io.provider->getNetwork().parseAddress("capnproto.org", 80);
co_return co_await addr->connect();
}
Promise<void> httpClient(Own<AsyncIoStream> connection) {
// Successfully connected to capnproto.org. Try doing GET /. We expect to get a redirect to
// HTTPS, because what kind of horrible web site would serve in plaintext, really?
HttpHeaderTable table;
auto client = newHttpClient(table, *connection);
HttpHeaders headers(table);
headers.set(HttpHeaderId::HOST, "capnproto.org");
auto response = co_await client->request(HttpMethod::GET, "/", headers).response;
KJ_EXPECT(response.statusCode / 100 == 3);
auto location = KJ_ASSERT_NONNULL(response.headers->get(HttpHeaderId::LOCATION));
KJ_EXPECT(location == "https://capnproto.org/");
auto body = co_await response.body->readAllText();
}
KJ_TEST("HttpClient to capnproto.org with a coroutine") {
auto io = setupAsyncIo();
auto promise = httpClientConnect(io).then([](Own<AsyncIoStream> connection) {
return httpClient(kj::mv(connection));
}, [](Exception&&) {
KJ_LOG(WARNING, "skipping test because couldn't connect to capnproto.org");
});
promise.wait(io.waitScope);
}
#endif // KJ_HAVE_COROUTINE
} // namespace
} // namespace kj