| 1 |
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors |
| 2 |
// Licensed under the MIT License: |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to deal |
| 6 |
// in the Software without restriction, including without limitation the rights |
| 7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
// copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
// THE SOFTWARE. |
| 21 |
|
| 22 |
#include "async.h" |
| 23 |
#include "debug.h" |
| 24 |
#include <kj/compat/gtest.h> |
| 25 |
#include "mutex.h" |
| 26 |
#include "thread.h" |
| 27 |
|
| 28 |
#if !KJ_USE_FIBERS && !_WIN32 |
| 29 |
#include <pthread.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#if KJ_USE_FIBERS && __linux__ |
| 33 |
#include <errno.h> |
| 34 |
#include <ucontext.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
namespace kj { |
| 38 |
namespace { |
| 39 |
|
| 40 |
#if !_MSC_VER |
| 41 |
// TODO(msvc): GetFunctorStartAddress is not supported on MSVC currently, so skip the test. |
| 42 |
TEST(Async, GetFunctorStartAddress) { |
| 43 |
EXPECT_TRUE(nullptr != _::GetFunctorStartAddress<>::apply([](){return 0;})); |
| 44 |
} |
| 45 |
#endif |
| 46 |
|
| 47 |
#if KJ_USE_FIBERS |
| 48 |
bool isLibcContextHandlingKnownBroken() { |
| 49 |
// manylinux2014-x86's libc implements getcontext() to fail with ENOSYS. This is flagrantly |
| 50 |
// against spec: getcontext() is not a syscall and is documented as never failing. Our configure |
| 51 |
// script cannot detect this problem because it would require actually executing code to see |
| 52 |
// what happens, which wouldn't work when cross-compiling. It would have been so much better if |
| 53 |
// they had removed the symbol from libc entirely. But as a work-around, we will skip the tests |
| 54 |
// when libc is broken. |
| 55 |
#if __linux__ |
| 56 |
static bool result = ([]() { |
| 57 |
ucontext_t context; |
| 58 |
if (getcontext(&context) < 0 && errno == ENOSYS) { |
| 59 |
KJ_LOG(WARNING, |
| 60 |
"This platform's libc is broken. Its getcontext() errors with ENOSYS. Fibers will not " |
| 61 |
"work, so we'll skip the tests, but libkj was still built with fiber support, which " |
| 62 |
"is broken. Please tell your libc maitnainer to remove the getcontext() function " |
| 63 |
"entirely rather than provide an intentionally-broken version -- that way, the " |
| 64 |
"configure script will detect that it should build libkj without fiber support."); |
| 65 |
return true; |
| 66 |
} else { |
| 67 |
return false; |
| 68 |
} |
| 69 |
})(); |
| 70 |
return result; |
| 71 |
#else |
| 72 |
return false; |
| 73 |
#endif |
| 74 |
} |
| 75 |
#endif |
| 76 |
|
| 77 |
TEST(Async, EvalVoid) { |
| 78 |
EventLoop loop; |
| 79 |
WaitScope waitScope(loop); |
| 80 |
|
| 81 |
bool done = false; |
| 82 |
|
| 83 |
Promise<void> promise = evalLater([&]() { done = true; }); |
| 84 |
EXPECT_FALSE(done); |
| 85 |
promise.wait(waitScope); |
| 86 |
EXPECT_TRUE(done); |
| 87 |
} |
| 88 |
|
| 89 |
TEST(Async, EvalInt) { |
| 90 |
EventLoop loop; |
| 91 |
WaitScope waitScope(loop); |
| 92 |
|
| 93 |
bool done = false; |
| 94 |
|
| 95 |
Promise<int> promise = evalLater([&]() { done = true; return 123; }); |
| 96 |
EXPECT_FALSE(done); |
| 97 |
EXPECT_EQ(123, promise.wait(waitScope)); |
| 98 |
EXPECT_TRUE(done); |
| 99 |
} |
| 100 |
|
| 101 |
TEST(Async, There) { |
| 102 |
EventLoop loop; |
| 103 |
WaitScope waitScope(loop); |
| 104 |
|
| 105 |
Promise<int> a = 123; |
| 106 |
bool done = false; |
| 107 |
|
| 108 |
Promise<int> promise = a.then([&](int ai) { done = true; return ai + 321; }); |
| 109 |
EXPECT_FALSE(done); |
| 110 |
EXPECT_EQ(444, promise.wait(waitScope)); |
| 111 |
EXPECT_TRUE(done); |
| 112 |
} |
| 113 |
|
| 114 |
TEST(Async, ThereVoid) { |
| 115 |
EventLoop loop; |
| 116 |
WaitScope waitScope(loop); |
| 117 |
|
| 118 |
Promise<int> a = 123; |
| 119 |
int value = 0; |
| 120 |
|
| 121 |
Promise<void> promise = a.then([&](int ai) { value = ai; }); |
| 122 |
EXPECT_EQ(0, value); |
| 123 |
promise.wait(waitScope); |
| 124 |
EXPECT_EQ(123, value); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(Async, Exception) { |
| 128 |
EventLoop loop; |
| 129 |
WaitScope waitScope(loop); |
| 130 |
|
| 131 |
Promise<int> promise = evalLater( |
| 132 |
[&]() -> int { KJ_FAIL_ASSERT("foo") { return 123; } }); |
| 133 |
EXPECT_TRUE(kj::runCatchingExceptions([&]() { |
| 134 |
// wait() only returns when compiling with -fno-exceptions. |
| 135 |
EXPECT_EQ(123, promise.wait(waitScope)); |
| 136 |
}) != nullptr); |
| 137 |
} |
| 138 |
|
| 139 |
TEST(Async, HandleException) { |
| 140 |
EventLoop loop; |
| 141 |
WaitScope waitScope(loop); |
| 142 |
|
| 143 |
Promise<int> promise = evalLater( |
| 144 |
[&]() -> int { KJ_FAIL_ASSERT("foo") { return 123; } }); |
| 145 |
int line = __LINE__ - 1; |
| 146 |
|
| 147 |
promise = promise.then( |
| 148 |
[](int i) { return i + 1; }, |
| 149 |
[&](Exception&& e) { EXPECT_EQ(line, e.getLine()); return 345; }); |
| 150 |
|
| 151 |
EXPECT_EQ(345, promise.wait(waitScope)); |
| 152 |
} |
| 153 |
|
| 154 |
TEST(Async, PropagateException) { |
| 155 |
EventLoop loop; |
| 156 |
WaitScope waitScope(loop); |
| 157 |
|
| 158 |
Promise<int> promise = evalLater( |
| 159 |
[&]() -> int { KJ_FAIL_ASSERT("foo") { return 123; } }); |
| 160 |
int line = __LINE__ - 1; |
| 161 |
|
| 162 |
promise = promise.then([](int i) { return i + 1; }); |
| 163 |
|
| 164 |
promise = promise.then( |
| 165 |
[](int i) { return i + 2; }, |
| 166 |
[&](Exception&& e) { EXPECT_EQ(line, e.getLine()); return 345; }); |
| 167 |
|
| 168 |
EXPECT_EQ(345, promise.wait(waitScope)); |
| 169 |
} |
| 170 |
|
| 171 |
TEST(Async, PropagateExceptionTypeChange) { |
| 172 |
EventLoop loop; |
| 173 |
WaitScope waitScope(loop); |
| 174 |
|
| 175 |
Promise<int> promise = evalLater( |
| 176 |
[&]() -> int { KJ_FAIL_ASSERT("foo") { return 123; } }); |
| 177 |
int line = __LINE__ - 1; |
| 178 |
|
| 179 |
Promise<StringPtr> promise2 = promise.then([](int i) -> StringPtr { return "foo"; }); |
| 180 |
|
| 181 |
promise2 = promise2.then( |
| 182 |
[](StringPtr s) -> StringPtr { return "bar"; }, |
| 183 |
[&](Exception&& e) -> StringPtr { EXPECT_EQ(line, e.getLine()); return "baz"; }); |
| 184 |
|
| 185 |
EXPECT_EQ("baz", promise2.wait(waitScope)); |
| 186 |
} |
| 187 |
|
| 188 |
TEST(Async, Then) { |
| 189 |
EventLoop loop; |
| 190 |
WaitScope waitScope(loop); |
| 191 |
|
| 192 |
bool done = false; |
| 193 |
|
| 194 |
Promise<int> promise = Promise<int>(123).then([&](int i) { |
| 195 |
done = true; |
| 196 |
return i + 321; |
| 197 |
}); |
| 198 |
|
| 199 |
EXPECT_FALSE(done); |
| 200 |
|
| 201 |
EXPECT_EQ(444, promise.wait(waitScope)); |
| 202 |
|
| 203 |
EXPECT_TRUE(done); |
| 204 |
} |
| 205 |
|
| 206 |
TEST(Async, Chain) { |
| 207 |
EventLoop loop; |
| 208 |
WaitScope waitScope(loop); |
| 209 |
|
| 210 |
Promise<int> promise = evalLater([&]() -> int { return 123; }); |
| 211 |
Promise<int> promise2 = evalLater([&]() -> int { return 321; }); |
| 212 |
|
| 213 |
auto promise3 = promise.then([&](int i) { |
| 214 |
return promise2.then([i](int j) { |
| 215 |
return i + j; |
| 216 |
}); |
| 217 |
}); |
| 218 |
|
| 219 |
EXPECT_EQ(444, promise3.wait(waitScope)); |
| 220 |
} |
| 221 |
|
| 222 |
TEST(Async, DeepChain) { |
| 223 |
EventLoop loop; |
| 224 |
WaitScope waitScope(loop); |
| 225 |
|
| 226 |
Promise<void> promise = NEVER_DONE; |
| 227 |
|
| 228 |
// Create a ridiculous chain of promises. |
| 229 |
for (uint i = 0; i < 1000; i++) { |
| 230 |
promise = evalLater([promise=kj::mv(promise)]() mutable { |
| 231 |
return kj::mv(promise); |
| 232 |
}); |
| 233 |
} |
| 234 |
|
| 235 |
loop.run(); |
| 236 |
|
| 237 |
auto trace = promise.trace(); |
| 238 |
uint lines = 0; |
| 239 |
for (char c: trace) { |
| 240 |
lines += c == '\n'; |
| 241 |
} |
| 242 |
|
| 243 |
// Chain nodes should have been collapsed such that instead of a chain of 1000 nodes, we have |
| 244 |
// 2-ish nodes. We'll give a little room for implementation freedom. |
| 245 |
EXPECT_LT(lines, 5); |
| 246 |
} |
| 247 |
|
| 248 |
TEST(Async, DeepChain2) { |
| 249 |
EventLoop loop; |
| 250 |
WaitScope waitScope(loop); |
| 251 |
|
| 252 |
Promise<void> promise = nullptr; |
| 253 |
promise = evalLater([&]() { |
| 254 |
auto trace = promise.trace(); |
| 255 |
uint lines = 0; |
| 256 |
for (char c: trace) { |
| 257 |
lines += c == '\n'; |
| 258 |
} |
| 259 |
|
| 260 |
// Chain nodes should have been collapsed such that instead of a chain of 1000 nodes, we have |
| 261 |
// 2-ish nodes. We'll give a little room for implementation freedom. |
| 262 |
EXPECT_LT(lines, 5); |
| 263 |
}); |
| 264 |
|
| 265 |
// Create a ridiculous chain of promises. |
| 266 |
for (uint i = 0; i < 1000; i++) { |
| 267 |
promise = evalLater([promise=kj::mv(promise)]() mutable { |
| 268 |
return kj::mv(promise); |
| 269 |
}); |
| 270 |
} |
| 271 |
|
| 272 |
promise.wait(waitScope); |
| 273 |
} |
| 274 |
|
| 275 |
Promise<void> makeChain(uint i) { |
| 276 |
if (i > 0) { |
| 277 |
return evalLater([i]() -> Promise<void> { |
| 278 |
return makeChain(i - 1); |
| 279 |
}); |
| 280 |
} else { |
| 281 |
return NEVER_DONE; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
TEST(Async, DeepChain3) { |
| 286 |
EventLoop loop; |
| 287 |
WaitScope waitScope(loop); |
| 288 |
|
| 289 |
Promise<void> promise = makeChain(1000); |
| 290 |
|
| 291 |
loop.run(); |
| 292 |
|
| 293 |
auto trace = promise.trace(); |
| 294 |
uint lines = 0; |
| 295 |
for (char c: trace) { |
| 296 |
lines += c == '\n'; |
| 297 |
} |
| 298 |
|
| 299 |
// Chain nodes should have been collapsed such that instead of a chain of 1000 nodes, we have |
| 300 |
// 2-ish nodes. We'll give a little room for implementation freedom. |
| 301 |
EXPECT_LT(lines, 5); |
| 302 |
} |
| 303 |
|
| 304 |
Promise<void> makeChain2(uint i, Promise<void> promise) { |
| 305 |
if (i > 0) { |
| 306 |
return evalLater([i, promise=kj::mv(promise)]() mutable -> Promise<void> { |
| 307 |
return makeChain2(i - 1, kj::mv(promise)); |
| 308 |
}); |
| 309 |
} else { |
| 310 |
return kj::mv(promise); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
TEST(Async, DeepChain4) { |
| 315 |
EventLoop loop; |
| 316 |
WaitScope waitScope(loop); |
| 317 |
|
| 318 |
Promise<void> promise = nullptr; |
| 319 |
promise = evalLater([&]() { |
| 320 |
auto trace = promise.trace(); |
| 321 |
uint lines = 0; |
| 322 |
for (char c: trace) { |
| 323 |
lines += c == '\n'; |
| 324 |
} |
| 325 |
|
| 326 |
// Chain nodes should have been collapsed such that instead of a chain of 1000 nodes, we have |
| 327 |
// 2-ish nodes. We'll give a little room for implementation freedom. |
| 328 |
EXPECT_LT(lines, 5); |
| 329 |
}); |
| 330 |
|
| 331 |
promise = makeChain2(1000, kj::mv(promise)); |
| 332 |
|
| 333 |
promise.wait(waitScope); |
| 334 |
} |
| 335 |
|
| 336 |
TEST(Async, IgnoreResult) { |
| 337 |
EventLoop loop; |
| 338 |
WaitScope waitScope(loop); |
| 339 |
|
| 340 |
bool done = false; |
| 341 |
|
| 342 |
Promise<void> promise = Promise<int>(123).then([&](int i) { |
| 343 |
done = true; |
| 344 |
return i + 321; |
| 345 |
}).ignoreResult(); |
| 346 |
|
| 347 |
EXPECT_FALSE(done); |
| 348 |
|
| 349 |
promise.wait(waitScope); |
| 350 |
|
| 351 |
EXPECT_TRUE(done); |
| 352 |
} |
| 353 |
|
| 354 |
TEST(Async, SeparateFulfiller) { |
| 355 |
EventLoop loop; |
| 356 |
WaitScope waitScope(loop); |
| 357 |
|
| 358 |
auto pair = newPromiseAndFulfiller<int>(); |
| 359 |
|
| 360 |
EXPECT_TRUE(pair.fulfiller->isWaiting()); |
| 361 |
pair.fulfiller->fulfill(123); |
| 362 |
EXPECT_FALSE(pair.fulfiller->isWaiting()); |
| 363 |
|
| 364 |
EXPECT_EQ(123, pair.promise.wait(waitScope)); |
| 365 |
} |
| 366 |
|
| 367 |
TEST(Async, SeparateFulfillerVoid) { |
| 368 |
EventLoop loop; |
| 369 |
WaitScope waitScope(loop); |
| 370 |
|
| 371 |
auto pair = newPromiseAndFulfiller<void>(); |
| 372 |
|
| 373 |
EXPECT_TRUE(pair.fulfiller->isWaiting()); |
| 374 |
pair.fulfiller->fulfill(); |
| 375 |
EXPECT_FALSE(pair.fulfiller->isWaiting()); |
| 376 |
|
| 377 |
pair.promise.wait(waitScope); |
| 378 |
} |
| 379 |
|
| 380 |
TEST(Async, SeparateFulfillerCanceled) { |
| 381 |
auto pair = newPromiseAndFulfiller<void>(); |
| 382 |
|
| 383 |
EXPECT_TRUE(pair.fulfiller->isWaiting()); |
| 384 |
pair.promise = nullptr; |
| 385 |
EXPECT_FALSE(pair.fulfiller->isWaiting()); |
| 386 |
} |
| 387 |
|
| 388 |
TEST(Async, SeparateFulfillerChained) { |
| 389 |
EventLoop loop; |
| 390 |
WaitScope waitScope(loop); |
| 391 |
|
| 392 |
auto pair = newPromiseAndFulfiller<Promise<int>>(); |
| 393 |
auto inner = newPromiseAndFulfiller<int>(); |
| 394 |
|
| 395 |
EXPECT_TRUE(pair.fulfiller->isWaiting()); |
| 396 |
pair.fulfiller->fulfill(kj::mv(inner.promise)); |
| 397 |
EXPECT_FALSE(pair.fulfiller->isWaiting()); |
| 398 |
|
| 399 |
inner.fulfiller->fulfill(123); |
| 400 |
|
| 401 |
EXPECT_EQ(123, pair.promise.wait(waitScope)); |
| 402 |
} |
| 403 |
|
| 404 |
TEST(Async, SeparateFulfillerDiscarded) { |
| 405 |
EventLoop loop; |
| 406 |
WaitScope waitScope(loop); |
| 407 |
|
| 408 |
auto pair = newPromiseAndFulfiller<void>(); |
| 409 |
pair.fulfiller = nullptr; |
| 410 |
|
| 411 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE( |
| 412 |
"PromiseFulfiller was destroyed without fulfilling the promise", |
| 413 |
pair.promise.wait(waitScope)); |
| 414 |
} |
| 415 |
|
| 416 |
#if !KJ_NO_EXCEPTIONS |
| 417 |
TEST(Async, SeparateFulfillerDiscardedDuringUnwind) { |
| 418 |
EventLoop loop; |
| 419 |
WaitScope waitScope(loop); |
| 420 |
|
| 421 |
auto pair = newPromiseAndFulfiller<int>(); |
| 422 |
kj::runCatchingExceptions([&]() { |
| 423 |
auto fulfillerToDrop = kj::mv(pair.fulfiller); |
| 424 |
kj::throwFatalException(KJ_EXCEPTION(FAILED, "test exception")); |
| 425 |
}); |
| 426 |
|
| 427 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE( |
| 428 |
"test exception", pair.promise.wait(waitScope)); |
| 429 |
} |
| 430 |
#endif |
| 431 |
|
| 432 |
TEST(Async, SeparateFulfillerMemoryLeak) { |
| 433 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 434 |
paf.fulfiller->fulfill(); |
| 435 |
} |
| 436 |
|
| 437 |
TEST(Async, Ordering) { |
| 438 |
EventLoop loop; |
| 439 |
WaitScope waitScope(loop); |
| 440 |
|
| 441 |
class ErrorHandlerImpl: public TaskSet::ErrorHandler { |
| 442 |
public: |
| 443 |
void taskFailed(kj::Exception&& exception) override { |
| 444 |
KJ_FAIL_EXPECT(exception); |
| 445 |
} |
| 446 |
}; |
| 447 |
|
| 448 |
int counter = 0; |
| 449 |
ErrorHandlerImpl errorHandler; |
| 450 |
kj::TaskSet tasks(errorHandler); |
| 451 |
|
| 452 |
tasks.add(evalLater([&]() { |
| 453 |
EXPECT_EQ(0, counter++); |
| 454 |
|
| 455 |
{ |
| 456 |
// Use a promise and fulfiller so that we can fulfill the promise after waiting on it in |
| 457 |
// order to induce depth-first scheduling. |
| 458 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 459 |
tasks.add(paf.promise.then([&]() { |
| 460 |
EXPECT_EQ(1, counter++); |
| 461 |
})); |
| 462 |
paf.fulfiller->fulfill(); |
| 463 |
} |
| 464 |
|
| 465 |
// .then() is scheduled breadth-first if the promise has already resolved, but depth-first |
| 466 |
// if the promise resolves later. |
| 467 |
tasks.add(Promise<void>(READY_NOW).then([&]() { |
| 468 |
EXPECT_EQ(4, counter++); |
| 469 |
}).then([&]() { |
| 470 |
EXPECT_EQ(5, counter++); |
| 471 |
tasks.add(kj::evalLast([&]() { |
| 472 |
EXPECT_EQ(7, counter++); |
| 473 |
tasks.add(kj::evalLater([&]() { |
| 474 |
EXPECT_EQ(8, counter++); |
| 475 |
})); |
| 476 |
})); |
| 477 |
})); |
| 478 |
|
| 479 |
{ |
| 480 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 481 |
tasks.add(paf.promise.then([&]() { |
| 482 |
EXPECT_EQ(2, counter++); |
| 483 |
tasks.add(kj::evalLast([&]() { |
| 484 |
EXPECT_EQ(9, counter++); |
| 485 |
tasks.add(kj::evalLater([&]() { |
| 486 |
EXPECT_EQ(10, counter++); |
| 487 |
})); |
| 488 |
})); |
| 489 |
})); |
| 490 |
paf.fulfiller->fulfill(); |
| 491 |
} |
| 492 |
|
| 493 |
// evalLater() is like READY_NOW.then(). |
| 494 |
tasks.add(evalLater([&]() { |
| 495 |
EXPECT_EQ(6, counter++); |
| 496 |
})); |
| 497 |
})); |
| 498 |
|
| 499 |
tasks.add(evalLater([&]() { |
| 500 |
EXPECT_EQ(3, counter++); |
| 501 |
|
| 502 |
// Making this a chain should NOT cause it to preempt the first promise. (This was a problem |
| 503 |
// at one point.) |
| 504 |
return Promise<void>(READY_NOW); |
| 505 |
})); |
| 506 |
|
| 507 |
tasks.onEmpty().wait(waitScope); |
| 508 |
|
| 509 |
EXPECT_EQ(11, counter); |
| 510 |
} |
| 511 |
|
| 512 |
TEST(Async, Fork) { |
| 513 |
EventLoop loop; |
| 514 |
WaitScope waitScope(loop); |
| 515 |
|
| 516 |
Promise<int> promise = evalLater([&]() { return 123; }); |
| 517 |
|
| 518 |
auto fork = promise.fork(); |
| 519 |
|
| 520 |
#if __GNUC__ && !__clang__ && __GNUC__ >= 7 |
| 521 |
// GCC 7 decides the open-brace below is "misleadingly indented" as if it were guarded by the `for` |
| 522 |
// that appears in the implementation of KJ_REQUIRE(). Shut up shut up shut up. |
| 523 |
#pragma GCC diagnostic ignored "-Wmisleading-indentation" |
| 524 |
#endif |
| 525 |
KJ_ASSERT(!fork.hasBranches()); |
| 526 |
{ |
| 527 |
auto cancelBranch = fork.addBranch(); |
| 528 |
KJ_ASSERT(fork.hasBranches()); |
| 529 |
} |
| 530 |
KJ_ASSERT(!fork.hasBranches()); |
| 531 |
|
| 532 |
auto branch1 = fork.addBranch().then([](int i) { |
| 533 |
EXPECT_EQ(123, i); |
| 534 |
return 456; |
| 535 |
}); |
| 536 |
KJ_ASSERT(fork.hasBranches()); |
| 537 |
auto branch2 = fork.addBranch().then([](int i) { |
| 538 |
EXPECT_EQ(123, i); |
| 539 |
return 789; |
| 540 |
}); |
| 541 |
KJ_ASSERT(fork.hasBranches()); |
| 542 |
|
| 543 |
{ |
| 544 |
auto releaseFork = kj::mv(fork); |
| 545 |
} |
| 546 |
|
| 547 |
EXPECT_EQ(456, branch1.wait(waitScope)); |
| 548 |
EXPECT_EQ(789, branch2.wait(waitScope)); |
| 549 |
} |
| 550 |
|
| 551 |
struct RefcountedInt: public Refcounted { |
| 552 |
RefcountedInt(int i): i(i) {} |
| 553 |
int i; |
| 554 |
Own<RefcountedInt> addRef() { return kj::addRef(*this); } |
| 555 |
}; |
| 556 |
|
| 557 |
TEST(Async, ForkRef) { |
| 558 |
EventLoop loop; |
| 559 |
WaitScope waitScope(loop); |
| 560 |
|
| 561 |
Promise<Own<RefcountedInt>> promise = evalLater([&]() { |
| 562 |
return refcounted<RefcountedInt>(123); |
| 563 |
}); |
| 564 |
|
| 565 |
auto fork = promise.fork(); |
| 566 |
|
| 567 |
auto branch1 = fork.addBranch().then([](Own<RefcountedInt>&& i) { |
| 568 |
EXPECT_EQ(123, i->i); |
| 569 |
return 456; |
| 570 |
}); |
| 571 |
auto branch2 = fork.addBranch().then([](Own<RefcountedInt>&& i) { |
| 572 |
EXPECT_EQ(123, i->i); |
| 573 |
return 789; |
| 574 |
}); |
| 575 |
|
| 576 |
{ |
| 577 |
auto releaseFork = kj::mv(fork); |
| 578 |
} |
| 579 |
|
| 580 |
EXPECT_EQ(456, branch1.wait(waitScope)); |
| 581 |
EXPECT_EQ(789, branch2.wait(waitScope)); |
| 582 |
} |
| 583 |
|
| 584 |
TEST(Async, ForkMaybeRef) { |
| 585 |
EventLoop loop; |
| 586 |
WaitScope waitScope(loop); |
| 587 |
|
| 588 |
Promise<Maybe<Own<RefcountedInt>>> promise = evalLater([&]() { |
| 589 |
return Maybe<Own<RefcountedInt>>(refcounted<RefcountedInt>(123)); |
| 590 |
}); |
| 591 |
|
| 592 |
auto fork = promise.fork(); |
| 593 |
|
| 594 |
auto branch1 = fork.addBranch().then([](Maybe<Own<RefcountedInt>>&& i) { |
| 595 |
EXPECT_EQ(123, KJ_REQUIRE_NONNULL(i)->i); |
| 596 |
return 456; |
| 597 |
}); |
| 598 |
auto branch2 = fork.addBranch().then([](Maybe<Own<RefcountedInt>>&& i) { |
| 599 |
EXPECT_EQ(123, KJ_REQUIRE_NONNULL(i)->i); |
| 600 |
return 789; |
| 601 |
}); |
| 602 |
|
| 603 |
{ |
| 604 |
auto releaseFork = kj::mv(fork); |
| 605 |
} |
| 606 |
|
| 607 |
EXPECT_EQ(456, branch1.wait(waitScope)); |
| 608 |
EXPECT_EQ(789, branch2.wait(waitScope)); |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
TEST(Async, Split) { |
| 613 |
EventLoop loop; |
| 614 |
WaitScope waitScope(loop); |
| 615 |
|
| 616 |
Promise<Tuple<int, String, Promise<int>>> promise = evalLater([&]() { |
| 617 |
return kj::tuple(123, str("foo"), Promise<int>(321)); |
| 618 |
}); |
| 619 |
|
| 620 |
Tuple<Promise<int>, Promise<String>, Promise<int>> split = promise.split(); |
| 621 |
|
| 622 |
EXPECT_EQ(123, get<0>(split).wait(waitScope)); |
| 623 |
EXPECT_EQ("foo", get<1>(split).wait(waitScope)); |
| 624 |
EXPECT_EQ(321, get<2>(split).wait(waitScope)); |
| 625 |
} |
| 626 |
|
| 627 |
TEST(Async, ExclusiveJoin) { |
| 628 |
{ |
| 629 |
EventLoop loop; |
| 630 |
WaitScope waitScope(loop); |
| 631 |
|
| 632 |
auto left = evalLater([&]() { return 123; }); |
| 633 |
auto right = newPromiseAndFulfiller<int>(); // never fulfilled |
| 634 |
|
| 635 |
EXPECT_EQ(123, left.exclusiveJoin(kj::mv(right.promise)).wait(waitScope)); |
| 636 |
} |
| 637 |
|
| 638 |
{ |
| 639 |
EventLoop loop; |
| 640 |
WaitScope waitScope(loop); |
| 641 |
|
| 642 |
auto left = newPromiseAndFulfiller<int>(); // never fulfilled |
| 643 |
auto right = evalLater([&]() { return 123; }); |
| 644 |
|
| 645 |
EXPECT_EQ(123, left.promise.exclusiveJoin(kj::mv(right)).wait(waitScope)); |
| 646 |
} |
| 647 |
|
| 648 |
{ |
| 649 |
EventLoop loop; |
| 650 |
WaitScope waitScope(loop); |
| 651 |
|
| 652 |
auto left = evalLater([&]() { return 123; }); |
| 653 |
auto right = evalLater([&]() { return 456; }); |
| 654 |
|
| 655 |
EXPECT_EQ(123, left.exclusiveJoin(kj::mv(right)).wait(waitScope)); |
| 656 |
} |
| 657 |
|
| 658 |
{ |
| 659 |
EventLoop loop; |
| 660 |
WaitScope waitScope(loop); |
| 661 |
|
| 662 |
auto left = evalLater([&]() { return 123; }); |
| 663 |
auto right = evalLater([&]() { return 456; }).eagerlyEvaluate(nullptr); |
| 664 |
|
| 665 |
EXPECT_EQ(456, left.exclusiveJoin(kj::mv(right)).wait(waitScope)); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
TEST(Async, ArrayJoin) { |
| 670 |
for (auto specificJoinPromisesOverload: { |
| 671 |
+[](kj::Array<kj::Promise<int>> promises) { return joinPromises(kj::mv(promises)); }, |
| 672 |
+[](kj::Array<kj::Promise<int>> promises) { return joinPromisesFailFast(kj::mv(promises)); } |
| 673 |
}) { |
| 674 |
EventLoop loop; |
| 675 |
WaitScope waitScope(loop); |
| 676 |
|
| 677 |
auto builder = heapArrayBuilder<Promise<int>>(3); |
| 678 |
builder.add(123); |
| 679 |
builder.add(456); |
| 680 |
builder.add(789); |
| 681 |
|
| 682 |
Promise<Array<int>> promise = specificJoinPromisesOverload(builder.finish()); |
| 683 |
|
| 684 |
auto result = promise.wait(waitScope); |
| 685 |
|
| 686 |
ASSERT_EQ(3u, result.size()); |
| 687 |
EXPECT_EQ(123, result[0]); |
| 688 |
EXPECT_EQ(456, result[1]); |
| 689 |
EXPECT_EQ(789, result[2]); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
TEST(Async, ArrayJoinVoid) { |
| 694 |
for (auto specificJoinPromisesOverload: { |
| 695 |
+[](kj::Array<kj::Promise<void>> promises) { return joinPromises(kj::mv(promises)); }, |
| 696 |
+[](kj::Array<kj::Promise<void>> promises) { return joinPromisesFailFast(kj::mv(promises)); } |
| 697 |
}) { |
| 698 |
EventLoop loop; |
| 699 |
WaitScope waitScope(loop); |
| 700 |
|
| 701 |
auto builder = heapArrayBuilder<Promise<void>>(3); |
| 702 |
builder.add(READY_NOW); |
| 703 |
builder.add(READY_NOW); |
| 704 |
builder.add(READY_NOW); |
| 705 |
|
| 706 |
Promise<void> promise = specificJoinPromisesOverload(builder.finish()); |
| 707 |
|
| 708 |
promise.wait(waitScope); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
struct Pafs { |
| 713 |
kj::Array<Promise<void>> promises; |
| 714 |
kj::Array<Own<PromiseFulfiller<void>>> fulfillers; |
| 715 |
}; |
| 716 |
|
| 717 |
Pafs makeCompletionCountingPafs(uint count, uint& tasksCompleted) { |
| 718 |
auto promisesBuilder = heapArrayBuilder<Promise<void>>(count); |
| 719 |
auto fulfillersBuilder = heapArrayBuilder<Own<PromiseFulfiller<void>>>(count); |
| 720 |
|
| 721 |
for (auto KJ_UNUSED value: zeroTo(count)) { |
| 722 |
auto paf = newPromiseAndFulfiller<void>(); |
| 723 |
promisesBuilder.add(paf.promise.then([&tasksCompleted]() { |
| 724 |
++tasksCompleted; |
| 725 |
})); |
| 726 |
fulfillersBuilder.add(kj::mv(paf.fulfiller)); |
| 727 |
} |
| 728 |
|
| 729 |
return { promisesBuilder.finish(), fulfillersBuilder.finish() }; |
| 730 |
} |
| 731 |
|
| 732 |
TEST(Async, ArrayJoinException) { |
| 733 |
EventLoop loop; |
| 734 |
WaitScope waitScope(loop); |
| 735 |
|
| 736 |
uint tasksCompleted = 0; |
| 737 |
auto pafs = makeCompletionCountingPafs(5, tasksCompleted); |
| 738 |
auto& fulfillers = pafs.fulfillers; |
| 739 |
Promise<void> promise = joinPromises(kj::mv(pafs.promises)); |
| 740 |
|
| 741 |
{ |
| 742 |
uint i = 0; |
| 743 |
KJ_EXPECT(tasksCompleted == 0); |
| 744 |
|
| 745 |
// Joined tasks are not completed early. |
| 746 |
fulfillers[i++]->fulfill(); |
| 747 |
KJ_EXPECT(!promise.poll(waitScope)); |
| 748 |
KJ_EXPECT(tasksCompleted == 0); |
| 749 |
|
| 750 |
fulfillers[i++]->fulfill(); |
| 751 |
KJ_EXPECT(!promise.poll(waitScope)); |
| 752 |
KJ_EXPECT(tasksCompleted == 0); |
| 753 |
|
| 754 |
// Rejected tasks do not fail-fast. |
| 755 |
fulfillers[i++]->reject(KJ_EXCEPTION(FAILED, "Test exception")); |
| 756 |
KJ_EXPECT(!promise.poll(waitScope)); |
| 757 |
KJ_EXPECT(tasksCompleted == 0); |
| 758 |
|
| 759 |
fulfillers[i++]->fulfill(); |
| 760 |
KJ_EXPECT(!promise.poll(waitScope)); |
| 761 |
KJ_EXPECT(tasksCompleted == 0); |
| 762 |
|
| 763 |
// The final fulfillment makes the promise ready. |
| 764 |
fulfillers[i++]->fulfill(); |
| 765 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("Test exception", promise.wait(waitScope)); |
| 766 |
KJ_EXPECT(tasksCompleted == 4); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
TEST(Async, ArrayJoinFailFastException) { |
| 771 |
EventLoop loop; |
| 772 |
WaitScope waitScope(loop); |
| 773 |
|
| 774 |
uint tasksCompleted = 0; |
| 775 |
auto pafs = makeCompletionCountingPafs(5, tasksCompleted); |
| 776 |
auto& fulfillers = pafs.fulfillers; |
| 777 |
Promise<void> promise = joinPromisesFailFast(kj::mv(pafs.promises)); |
| 778 |
|
| 779 |
{ |
| 780 |
uint i = 0; |
| 781 |
KJ_EXPECT(tasksCompleted == 0); |
| 782 |
|
| 783 |
// Joined tasks are completed eagerly, not waiting until the join node is awaited. |
| 784 |
fulfillers[i++]->fulfill(); |
| 785 |
KJ_EXPECT(!promise.poll(waitScope)); |
| 786 |
KJ_EXPECT(tasksCompleted == i); |
| 787 |
|
| 788 |
fulfillers[i++]->fulfill(); |
| 789 |
KJ_EXPECT(!promise.poll(waitScope)); |
| 790 |
KJ_EXPECT(tasksCompleted == i); |
| 791 |
|
| 792 |
fulfillers[i++]->reject(KJ_EXCEPTION(FAILED, "Test exception")); |
| 793 |
|
| 794 |
// The first rejection makes the promise ready. |
| 795 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("Test exception", promise.wait(waitScope)); |
| 796 |
KJ_EXPECT(tasksCompleted == i - 1); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
TEST(Async, Canceler) { |
| 801 |
EventLoop loop; |
| 802 |
WaitScope waitScope(loop); |
| 803 |
Canceler canceler; |
| 804 |
|
| 805 |
auto never = canceler.wrap(kj::Promise<void>(kj::NEVER_DONE)); |
| 806 |
auto now = canceler.wrap(kj::Promise<void>(kj::READY_NOW)); |
| 807 |
auto neverI = canceler.wrap(kj::Promise<void>(kj::NEVER_DONE).then([]() { return 123u; })); |
| 808 |
auto nowI = canceler.wrap(kj::Promise<uint>(123u)); |
| 809 |
|
| 810 |
KJ_EXPECT(!never.poll(waitScope)); |
| 811 |
KJ_EXPECT(now.poll(waitScope)); |
| 812 |
KJ_EXPECT(!neverI.poll(waitScope)); |
| 813 |
KJ_EXPECT(nowI.poll(waitScope)); |
| 814 |
|
| 815 |
canceler.cancel("foobar"); |
| 816 |
|
| 817 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("foobar", never.wait(waitScope)); |
| 818 |
now.wait(waitScope); |
| 819 |
KJ_EXPECT_THROW_MESSAGE("foobar", neverI.wait(waitScope)); |
| 820 |
KJ_EXPECT(nowI.wait(waitScope) == 123u); |
| 821 |
} |
| 822 |
|
| 823 |
TEST(Async, CancelerDoubleWrap) { |
| 824 |
EventLoop loop; |
| 825 |
WaitScope waitScope(loop); |
| 826 |
|
| 827 |
// This used to crash. |
| 828 |
Canceler canceler; |
| 829 |
auto promise = canceler.wrap(canceler.wrap(kj::Promise<void>(kj::NEVER_DONE))); |
| 830 |
canceler.cancel("whoops"); |
| 831 |
} |
| 832 |
|
| 833 |
class ErrorHandlerImpl: public TaskSet::ErrorHandler { |
| 834 |
public: |
| 835 |
uint exceptionCount = 0; |
| 836 |
void taskFailed(kj::Exception&& exception) override { |
| 837 |
EXPECT_TRUE(exception.getDescription().endsWith("example TaskSet failure")); |
| 838 |
++exceptionCount; |
| 839 |
} |
| 840 |
}; |
| 841 |
|
| 842 |
TEST(Async, TaskSet) { |
| 843 |
EventLoop loop; |
| 844 |
WaitScope waitScope(loop); |
| 845 |
ErrorHandlerImpl errorHandler; |
| 846 |
TaskSet tasks(errorHandler); |
| 847 |
|
| 848 |
int counter = 0; |
| 849 |
|
| 850 |
tasks.add(evalLater([&]() { |
| 851 |
EXPECT_EQ(0, counter++); |
| 852 |
})); |
| 853 |
tasks.add(evalLater([&]() { |
| 854 |
EXPECT_EQ(1, counter++); |
| 855 |
KJ_FAIL_ASSERT("example TaskSet failure") { break; } |
| 856 |
})); |
| 857 |
tasks.add(evalLater([&]() { |
| 858 |
EXPECT_EQ(2, counter++); |
| 859 |
})); |
| 860 |
|
| 861 |
auto ignore KJ_UNUSED = evalLater([&]() { |
| 862 |
KJ_FAIL_EXPECT("Promise without waiter shouldn't execute."); |
| 863 |
}); |
| 864 |
|
| 865 |
evalLater([&]() { |
| 866 |
EXPECT_EQ(3, counter++); |
| 867 |
}).wait(waitScope); |
| 868 |
|
| 869 |
EXPECT_EQ(4, counter); |
| 870 |
EXPECT_EQ(1u, errorHandler.exceptionCount); |
| 871 |
} |
| 872 |
|
| 873 |
#if KJ_USE_FIBERS || !_WIN32 |
| 874 |
// This test requires either fibers or pthreads in order to limit the stack size. Currently we |
| 875 |
// don't have a version that works on Windows without fibers, so skip the test there. |
| 876 |
|
| 877 |
TEST(Async, LargeTaskSetDestruction) { |
| 878 |
static constexpr size_t stackSize = 200 * 1024; |
| 879 |
|
| 880 |
static auto testBody = [] { |
| 881 |
|
| 882 |
ErrorHandlerImpl errorHandler; |
| 883 |
TaskSet tasks(errorHandler); |
| 884 |
|
| 885 |
for (int i = 0; i < stackSize / sizeof(void*); i++) { |
| 886 |
tasks.add(kj::NEVER_DONE); |
| 887 |
} |
| 888 |
}; |
| 889 |
|
| 890 |
#if KJ_USE_FIBERS |
| 891 |
if (isLibcContextHandlingKnownBroken()) return; |
| 892 |
|
| 893 |
EventLoop loop; |
| 894 |
WaitScope waitScope(loop); |
| 895 |
|
| 896 |
startFiber(stackSize, |
| 897 |
[](WaitScope&) mutable { |
| 898 |
testBody(); |
| 899 |
}).wait(waitScope); |
| 900 |
|
| 901 |
#else |
| 902 |
pthread_attr_t attr; |
| 903 |
KJ_REQUIRE(0 == pthread_attr_init(&attr)); |
| 904 |
KJ_DEFER(KJ_REQUIRE(0 == pthread_attr_destroy(&attr))); |
| 905 |
|
| 906 |
KJ_REQUIRE(0 == pthread_attr_setstacksize(&attr, stackSize)); |
| 907 |
pthread_t thread; |
| 908 |
KJ_REQUIRE(0 == pthread_create(&thread, &attr, [](void*) -> void* { |
| 909 |
EventLoop loop; |
| 910 |
WaitScope waitScope(loop); |
| 911 |
testBody(); |
| 912 |
return nullptr; |
| 913 |
}, nullptr)); |
| 914 |
KJ_REQUIRE(0 == pthread_join(thread, nullptr)); |
| 915 |
#endif |
| 916 |
} |
| 917 |
|
| 918 |
#endif // KJ_USE_FIBERS || !_WIN32 |
| 919 |
|
| 920 |
TEST(Async, TaskSet) { |
| 921 |
EventLoop loop; |
| 922 |
WaitScope waitScope(loop); |
| 923 |
|
| 924 |
bool destroyed = false; |
| 925 |
|
| 926 |
{ |
| 927 |
ErrorHandlerImpl errorHandler; |
| 928 |
TaskSet tasks(errorHandler); |
| 929 |
|
| 930 |
tasks.add(kj::Promise<void>(kj::NEVER_DONE) |
| 931 |
.attach(kj::defer([&]() { |
| 932 |
// During cancellation, append another task! |
| 933 |
// It had better be canceled too! |
| 934 |
tasks.add(kj::Promise<void>(kj::READY_NOW) |
| 935 |
.then([]() { KJ_FAIL_EXPECT("shouldn't get here"); }, |
| 936 |
[](auto) { KJ_FAIL_EXPECT("shouldn't get here"); }) |
| 937 |
.attach(kj::defer([&]() { |
| 938 |
destroyed = true; |
| 939 |
}))); |
| 940 |
}))); |
| 941 |
} |
| 942 |
|
| 943 |
KJ_EXPECT(destroyed); |
| 944 |
|
| 945 |
// Give a chance for the "shouldn't get here" asserts to execute, if the event is still running, |
| 946 |
// which it shouldn't be. |
| 947 |
waitScope.poll(); |
| 948 |
} |
| 949 |
|
| 950 |
TEST(Async, TaskSetOnEmpty) { |
| 951 |
EventLoop loop; |
| 952 |
WaitScope waitScope(loop); |
| 953 |
ErrorHandlerImpl errorHandler; |
| 954 |
TaskSet tasks(errorHandler); |
| 955 |
|
| 956 |
KJ_EXPECT(tasks.isEmpty()); |
| 957 |
|
| 958 |
auto paf = newPromiseAndFulfiller<void>(); |
| 959 |
tasks.add(kj::mv(paf.promise)); |
| 960 |
tasks.add(evalLater([]() {})); |
| 961 |
|
| 962 |
KJ_EXPECT(!tasks.isEmpty()); |
| 963 |
|
| 964 |
auto promise = tasks.onEmpty(); |
| 965 |
KJ_EXPECT(!promise.poll(waitScope)); |
| 966 |
KJ_EXPECT(!tasks.isEmpty()); |
| 967 |
|
| 968 |
paf.fulfiller->fulfill(); |
| 969 |
KJ_ASSERT(promise.poll(waitScope)); |
| 970 |
KJ_EXPECT(tasks.isEmpty()); |
| 971 |
promise.wait(waitScope); |
| 972 |
} |
| 973 |
|
| 974 |
KJ_TEST("TaskSet::clear()") { |
| 975 |
EventLoop loop; |
| 976 |
WaitScope waitScope(loop); |
| 977 |
|
| 978 |
class ClearOnError: public TaskSet::ErrorHandler { |
| 979 |
public: |
| 980 |
TaskSet* tasks; |
| 981 |
void taskFailed(kj::Exception&& exception) override { |
| 982 |
KJ_EXPECT(exception.getDescription().endsWith("example TaskSet failure")); |
| 983 |
tasks->clear(); |
| 984 |
} |
| 985 |
}; |
| 986 |
|
| 987 |
ClearOnError errorHandler; |
| 988 |
TaskSet tasks(errorHandler); |
| 989 |
errorHandler.tasks = &tasks; |
| 990 |
|
| 991 |
auto doTest = [&](auto&& causeClear) { |
| 992 |
KJ_EXPECT(tasks.isEmpty()); |
| 993 |
|
| 994 |
uint count = 0; |
| 995 |
tasks.add(kj::Promise<void>(kj::READY_NOW).attach(kj::defer([&]() { ++count; }))); |
| 996 |
tasks.add(kj::Promise<void>(kj::NEVER_DONE).attach(kj::defer([&]() { ++count; }))); |
| 997 |
tasks.add(kj::Promise<void>(kj::NEVER_DONE).attach(kj::defer([&]() { ++count; }))); |
| 998 |
|
| 999 |
auto onEmpty = tasks.onEmpty(); |
| 1000 |
KJ_EXPECT(!onEmpty.poll(waitScope)); |
| 1001 |
KJ_EXPECT(count == 1); |
| 1002 |
KJ_EXPECT(!tasks.isEmpty()); |
| 1003 |
|
| 1004 |
causeClear(); |
| 1005 |
KJ_EXPECT(tasks.isEmpty()); |
| 1006 |
onEmpty.wait(waitScope); |
| 1007 |
KJ_EXPECT(count == 3); |
| 1008 |
}; |
| 1009 |
|
| 1010 |
// Try it where we just call clear() directly. |
| 1011 |
doTest([&]() { tasks.clear(); }); |
| 1012 |
|
| 1013 |
// Try causing clear() inside taskFailed(), ensuring that this is permitted. |
| 1014 |
doTest([&]() { |
| 1015 |
tasks.add(KJ_EXCEPTION(FAILED, "example TaskSet failure")); |
| 1016 |
waitScope.poll(); |
| 1017 |
}); |
| 1018 |
} |
| 1019 |
|
| 1020 |
class DestructorDetector { |
| 1021 |
public: |
| 1022 |
DestructorDetector(bool& setTrue): setTrue(setTrue) {} |
| 1023 |
~DestructorDetector() { setTrue = true; } |
| 1024 |
|
| 1025 |
private: |
| 1026 |
bool& setTrue; |
| 1027 |
}; |
| 1028 |
|
| 1029 |
TEST(Async, Attach) { |
| 1030 |
bool destroyed = false; |
| 1031 |
|
| 1032 |
EventLoop loop; |
| 1033 |
WaitScope waitScope(loop); |
| 1034 |
|
| 1035 |
Promise<int> promise = evalLater([&]() { |
| 1036 |
EXPECT_FALSE(destroyed); |
| 1037 |
return 123; |
| 1038 |
}).attach(kj::heap<DestructorDetector>(destroyed)); |
| 1039 |
|
| 1040 |
promise = promise.then([&](int i) { |
| 1041 |
EXPECT_TRUE(destroyed); |
| 1042 |
return i + 321; |
| 1043 |
}); |
| 1044 |
|
| 1045 |
EXPECT_FALSE(destroyed); |
| 1046 |
EXPECT_EQ(444, promise.wait(waitScope)); |
| 1047 |
EXPECT_TRUE(destroyed); |
| 1048 |
} |
| 1049 |
|
| 1050 |
TEST(Async, EagerlyEvaluate) { |
| 1051 |
bool called = false; |
| 1052 |
|
| 1053 |
EventLoop loop; |
| 1054 |
WaitScope waitScope(loop); |
| 1055 |
|
| 1056 |
Promise<void> promise = Promise<void>(READY_NOW).then([&]() { |
| 1057 |
called = true; |
| 1058 |
}); |
| 1059 |
evalLater([]() {}).wait(waitScope); |
| 1060 |
|
| 1061 |
EXPECT_FALSE(called); |
| 1062 |
|
| 1063 |
promise = promise.eagerlyEvaluate(nullptr); |
| 1064 |
|
| 1065 |
evalLater([]() {}).wait(waitScope); |
| 1066 |
|
| 1067 |
EXPECT_TRUE(called); |
| 1068 |
} |
| 1069 |
|
| 1070 |
TEST(Async, Detach) { |
| 1071 |
EventLoop loop; |
| 1072 |
WaitScope waitScope(loop); |
| 1073 |
|
| 1074 |
bool ran1 = false; |
| 1075 |
bool ran2 = false; |
| 1076 |
bool ran3 = false; |
| 1077 |
|
| 1078 |
{ |
| 1079 |
// let returned promise be destroyed (canceled) |
| 1080 |
auto ignore KJ_UNUSED = evalLater([&]() { ran1 = true; }); |
| 1081 |
} |
| 1082 |
evalLater([&]() { ran2 = true; }).detach([](kj::Exception&&) { ADD_FAILURE(); }); |
| 1083 |
evalLater([]() { KJ_FAIL_ASSERT("foo"){break;} }).detach([&](kj::Exception&& e) { ran3 = true; }); |
| 1084 |
|
| 1085 |
EXPECT_FALSE(ran1); |
| 1086 |
EXPECT_FALSE(ran2); |
| 1087 |
EXPECT_FALSE(ran3); |
| 1088 |
|
| 1089 |
evalLater([]() {}).wait(waitScope); |
| 1090 |
|
| 1091 |
EXPECT_FALSE(ran1); |
| 1092 |
EXPECT_TRUE(ran2); |
| 1093 |
EXPECT_TRUE(ran3); |
| 1094 |
} |
| 1095 |
|
| 1096 |
class DummyEventPort: public EventPort { |
| 1097 |
public: |
| 1098 |
bool runnable = false; |
| 1099 |
int callCount = 0; |
| 1100 |
|
| 1101 |
bool wait() override { KJ_FAIL_ASSERT("Nothing to wait for."); } |
| 1102 |
bool poll() override { return false; } |
| 1103 |
void setRunnable(bool runnable) override { |
| 1104 |
this->runnable = runnable; |
| 1105 |
++callCount; |
| 1106 |
} |
| 1107 |
}; |
| 1108 |
|
| 1109 |
TEST(Async, SetRunnable) { |
| 1110 |
DummyEventPort port; |
| 1111 |
EventLoop loop(port); |
| 1112 |
WaitScope waitScope(loop); |
| 1113 |
|
| 1114 |
EXPECT_FALSE(port.runnable); |
| 1115 |
EXPECT_EQ(0, port.callCount); |
| 1116 |
|
| 1117 |
{ |
| 1118 |
auto promise = evalLater([]() {}).eagerlyEvaluate(nullptr); |
| 1119 |
|
| 1120 |
EXPECT_TRUE(port.runnable); |
| 1121 |
loop.run(1); |
| 1122 |
EXPECT_FALSE(port.runnable); |
| 1123 |
EXPECT_EQ(2, port.callCount); |
| 1124 |
|
| 1125 |
promise.wait(waitScope); |
| 1126 |
EXPECT_FALSE(port.runnable); |
| 1127 |
EXPECT_EQ(4, port.callCount); |
| 1128 |
} |
| 1129 |
|
| 1130 |
{ |
| 1131 |
auto paf = newPromiseAndFulfiller<void>(); |
| 1132 |
auto promise = paf.promise.then([]() {}).eagerlyEvaluate(nullptr); |
| 1133 |
EXPECT_FALSE(port.runnable); |
| 1134 |
|
| 1135 |
auto promise2 = evalLater([]() {}).eagerlyEvaluate(nullptr); |
| 1136 |
paf.fulfiller->fulfill(); |
| 1137 |
|
| 1138 |
EXPECT_TRUE(port.runnable); |
| 1139 |
loop.run(1); |
| 1140 |
EXPECT_TRUE(port.runnable); |
| 1141 |
loop.run(10); |
| 1142 |
EXPECT_FALSE(port.runnable); |
| 1143 |
|
| 1144 |
promise.wait(waitScope); |
| 1145 |
EXPECT_FALSE(port.runnable); |
| 1146 |
|
| 1147 |
EXPECT_EQ(8, port.callCount); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
TEST(Async, Poll) { |
| 1152 |
EventLoop loop; |
| 1153 |
WaitScope waitScope(loop); |
| 1154 |
|
| 1155 |
auto paf = newPromiseAndFulfiller<void>(); |
| 1156 |
KJ_ASSERT(!paf.promise.poll(waitScope)); |
| 1157 |
paf.fulfiller->fulfill(); |
| 1158 |
KJ_ASSERT(paf.promise.poll(waitScope)); |
| 1159 |
paf.promise.wait(waitScope); |
| 1160 |
} |
| 1161 |
|
| 1162 |
KJ_TEST("Maximum turn count during wait scope poll is enforced") { |
| 1163 |
EventLoop loop; |
| 1164 |
WaitScope waitScope(loop); |
| 1165 |
ErrorHandlerImpl errorHandler; |
| 1166 |
TaskSet tasks(errorHandler); |
| 1167 |
|
| 1168 |
auto evaluated1 = false; |
| 1169 |
tasks.add(evalLater([&]() { |
| 1170 |
evaluated1 = true; |
| 1171 |
})); |
| 1172 |
|
| 1173 |
auto evaluated2 = false; |
| 1174 |
tasks.add(evalLater([&]() { |
| 1175 |
evaluated2 = true; |
| 1176 |
})); |
| 1177 |
|
| 1178 |
auto evaluated3 = false; |
| 1179 |
tasks.add(evalLater([&]() { |
| 1180 |
evaluated3 = true; |
| 1181 |
})); |
| 1182 |
|
| 1183 |
uint count; |
| 1184 |
|
| 1185 |
// Check that only events up to a maximum are resolved: |
| 1186 |
count = waitScope.poll(2); |
| 1187 |
KJ_ASSERT(count == 2); |
| 1188 |
KJ_EXPECT(evaluated1); |
| 1189 |
KJ_EXPECT(evaluated2); |
| 1190 |
KJ_EXPECT(!evaluated3); |
| 1191 |
|
| 1192 |
// Get the last remaining event in the queue: |
| 1193 |
count = waitScope.poll(1); |
| 1194 |
KJ_ASSERT(count == 1); |
| 1195 |
KJ_EXPECT(evaluated3); |
| 1196 |
|
| 1197 |
// No more events: |
| 1198 |
count = waitScope.poll(1); |
| 1199 |
KJ_ASSERT(count == 0); |
| 1200 |
} |
| 1201 |
|
| 1202 |
KJ_TEST("exclusiveJoin both events complete simultaneously") { |
| 1203 |
// Previously, if both branches of an exclusiveJoin() completed simultaneously, then the parent |
| 1204 |
// event could be armed twice. This is an error, but the exact results of this error depend on |
| 1205 |
// the parent PromiseNode type. One case where it matters is ArrayJoinPromiseNode, which counts |
| 1206 |
// events and decides it is done when it has received exactly the number of events expected. |
| 1207 |
|
| 1208 |
EventLoop loop; |
| 1209 |
WaitScope waitScope(loop); |
| 1210 |
|
| 1211 |
auto builder = kj::heapArrayBuilder<kj::Promise<uint>>(2); |
| 1212 |
builder.add(kj::Promise<uint>(123).exclusiveJoin(kj::Promise<uint>(456))); |
| 1213 |
builder.add(kj::NEVER_DONE); |
| 1214 |
auto joined = kj::joinPromises(builder.finish()); |
| 1215 |
|
| 1216 |
KJ_EXPECT(!joined.poll(waitScope)); |
| 1217 |
} |
| 1218 |
|
| 1219 |
#if KJ_USE_FIBERS |
| 1220 |
KJ_TEST("start a fiber") { |
| 1221 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1222 |
|
| 1223 |
EventLoop loop; |
| 1224 |
WaitScope waitScope(loop); |
| 1225 |
|
| 1226 |
auto paf = newPromiseAndFulfiller<int>(); |
| 1227 |
|
| 1228 |
Promise<StringPtr> fiber = startFiber(65536, |
| 1229 |
[promise = kj::mv(paf.promise)](WaitScope& fiberScope) mutable { |
| 1230 |
int i = promise.wait(fiberScope); |
| 1231 |
KJ_EXPECT(i == 123); |
| 1232 |
return "foo"_kj; |
| 1233 |
}); |
| 1234 |
|
| 1235 |
KJ_EXPECT(!fiber.poll(waitScope)); |
| 1236 |
|
| 1237 |
paf.fulfiller->fulfill(123); |
| 1238 |
|
| 1239 |
KJ_ASSERT(fiber.poll(waitScope)); |
| 1240 |
KJ_EXPECT(fiber.wait(waitScope) == "foo"); |
| 1241 |
} |
| 1242 |
|
| 1243 |
KJ_TEST("fiber promise chaining") { |
| 1244 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1245 |
|
| 1246 |
EventLoop loop; |
| 1247 |
WaitScope waitScope(loop); |
| 1248 |
|
| 1249 |
auto paf = newPromiseAndFulfiller<int>(); |
| 1250 |
bool ran = false; |
| 1251 |
|
| 1252 |
Promise<int> fiber = startFiber(65536, |
| 1253 |
[promise = kj::mv(paf.promise), &ran](WaitScope& fiberScope) mutable { |
| 1254 |
ran = true; |
| 1255 |
return kj::mv(promise); |
| 1256 |
}); |
| 1257 |
|
| 1258 |
KJ_EXPECT(!ran); |
| 1259 |
KJ_EXPECT(!fiber.poll(waitScope)); |
| 1260 |
KJ_EXPECT(ran); |
| 1261 |
|
| 1262 |
paf.fulfiller->fulfill(123); |
| 1263 |
|
| 1264 |
KJ_ASSERT(fiber.poll(waitScope)); |
| 1265 |
KJ_EXPECT(fiber.wait(waitScope) == 123); |
| 1266 |
} |
| 1267 |
|
| 1268 |
KJ_TEST("throw from a fiber") { |
| 1269 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1270 |
|
| 1271 |
EventLoop loop; |
| 1272 |
WaitScope waitScope(loop); |
| 1273 |
|
| 1274 |
auto paf = newPromiseAndFulfiller<void>(); |
| 1275 |
|
| 1276 |
Promise<void> fiber = startFiber(65536, |
| 1277 |
[promise = kj::mv(paf.promise)](WaitScope& fiberScope) mutable { |
| 1278 |
promise.wait(fiberScope); |
| 1279 |
KJ_FAIL_EXPECT("wait() should have thrown"); |
| 1280 |
}); |
| 1281 |
|
| 1282 |
KJ_EXPECT(!fiber.poll(waitScope)); |
| 1283 |
|
| 1284 |
paf.fulfiller->reject(KJ_EXCEPTION(FAILED, "test exception")); |
| 1285 |
|
| 1286 |
KJ_ASSERT(fiber.poll(waitScope)); |
| 1287 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("test exception", fiber.wait(waitScope)); |
| 1288 |
} |
| 1289 |
|
| 1290 |
#if !__MINGW32__ || __MINGW64__ |
| 1291 |
// This test fails on MinGW 32-bit builds due to a compiler bug with exceptions + fibers: |
| 1292 |
// https://sourceforge.net/p/mingw-w64/bugs/835/ |
| 1293 |
KJ_TEST("cancel a fiber") { |
| 1294 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1295 |
|
| 1296 |
EventLoop loop; |
| 1297 |
WaitScope waitScope(loop); |
| 1298 |
|
| 1299 |
// When exceptions are disabled we can't wait() on a non-void promise that throws. |
| 1300 |
auto paf = newPromiseAndFulfiller<void>(); |
| 1301 |
|
| 1302 |
bool exited = false; |
| 1303 |
bool canceled = false; |
| 1304 |
|
| 1305 |
{ |
| 1306 |
Promise<StringPtr> fiber = startFiber(65536, |
| 1307 |
[promise = kj::mv(paf.promise), &exited, &canceled](WaitScope& fiberScope) mutable { |
| 1308 |
KJ_DEFER(exited = true); |
| 1309 |
try { |
| 1310 |
promise.wait(fiberScope); |
| 1311 |
} catch (kj::CanceledException) { |
| 1312 |
canceled = true; |
| 1313 |
throw; |
| 1314 |
} |
| 1315 |
return "foo"_kj; |
| 1316 |
}); |
| 1317 |
|
| 1318 |
KJ_EXPECT(!fiber.poll(waitScope)); |
| 1319 |
KJ_EXPECT(!exited); |
| 1320 |
KJ_EXPECT(!canceled); |
| 1321 |
} |
| 1322 |
|
| 1323 |
KJ_EXPECT(exited); |
| 1324 |
KJ_EXPECT(canceled); |
| 1325 |
} |
| 1326 |
#endif |
| 1327 |
|
| 1328 |
KJ_TEST("fiber pool") { |
| 1329 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1330 |
|
| 1331 |
EventLoop loop; |
| 1332 |
WaitScope waitScope(loop); |
| 1333 |
FiberPool pool(65536); |
| 1334 |
|
| 1335 |
int* i1_local = nullptr; |
| 1336 |
int* i2_local = nullptr; |
| 1337 |
|
| 1338 |
auto run = [&]() mutable { |
| 1339 |
auto paf1 = newPromiseAndFulfiller<int>(); |
| 1340 |
auto paf2 = newPromiseAndFulfiller<int>(); |
| 1341 |
|
| 1342 |
{ |
| 1343 |
Promise<int> fiber1 = pool.startFiber([&, promise = kj::mv(paf1.promise)](WaitScope& scope) mutable { |
| 1344 |
int i = promise.wait(scope); |
| 1345 |
KJ_EXPECT(i == 123); |
| 1346 |
if (i1_local == nullptr) { |
| 1347 |
i1_local = &i; |
| 1348 |
} else { |
| 1349 |
#if !KJ_HAS_COMPILER_FEATURE(address_sanitizer) |
| 1350 |
// Verify that the stack variable is in the exact same spot as before. |
| 1351 |
// May not work under ASAN as the instrumentation to detect stack-use-after-return can |
| 1352 |
// change the address. |
| 1353 |
KJ_ASSERT(i1_local == &i); |
| 1354 |
#endif |
| 1355 |
} |
| 1356 |
return i; |
| 1357 |
}); |
| 1358 |
{ |
| 1359 |
Promise<int> fiber2 = pool.startFiber([&, promise = kj::mv(paf2.promise)](WaitScope& scope) mutable { |
| 1360 |
int i = promise.wait(scope); |
| 1361 |
KJ_EXPECT(i == 456); |
| 1362 |
if (i2_local == nullptr) { |
| 1363 |
i2_local = &i; |
| 1364 |
} else { |
| 1365 |
#if !KJ_HAS_COMPILER_FEATURE(address_sanitizer) |
| 1366 |
KJ_ASSERT(i2_local == &i); |
| 1367 |
#endif |
| 1368 |
} |
| 1369 |
return i; |
| 1370 |
}); |
| 1371 |
|
| 1372 |
KJ_EXPECT(!fiber1.poll(waitScope)); |
| 1373 |
KJ_EXPECT(!fiber2.poll(waitScope)); |
| 1374 |
|
| 1375 |
KJ_EXPECT(pool.getFreelistSize() == 0); |
| 1376 |
|
| 1377 |
paf2.fulfiller->fulfill(456); |
| 1378 |
|
| 1379 |
KJ_EXPECT(!fiber1.poll(waitScope)); |
| 1380 |
KJ_ASSERT(fiber2.poll(waitScope)); |
| 1381 |
KJ_EXPECT(fiber2.wait(waitScope) == 456); |
| 1382 |
|
| 1383 |
KJ_EXPECT(pool.getFreelistSize() == 1); |
| 1384 |
} |
| 1385 |
|
| 1386 |
paf1.fulfiller->fulfill(123); |
| 1387 |
|
| 1388 |
KJ_ASSERT(fiber1.poll(waitScope)); |
| 1389 |
KJ_EXPECT(fiber1.wait(waitScope) == 123); |
| 1390 |
|
| 1391 |
KJ_EXPECT(pool.getFreelistSize() == 2); |
| 1392 |
} |
| 1393 |
}; |
| 1394 |
run(); |
| 1395 |
KJ_ASSERT(i1_local != nullptr); |
| 1396 |
KJ_ASSERT(i2_local != nullptr); |
| 1397 |
// run the same thing and reuse the fibers |
| 1398 |
run(); |
| 1399 |
} |
| 1400 |
|
| 1401 |
bool onOurStack(char* p) { |
| 1402 |
// If p points less than 64k away from a random stack variable, then it must be on the same |
| 1403 |
// stack, since we never allocate stacks smaller than 64k. |
| 1404 |
#if KJ_HAS_COMPILER_FEATURE(address_sanitizer) |
| 1405 |
// The stack-use-after-return detection mechanism breaks our ability to check this, so don't. |
| 1406 |
return true; |
| 1407 |
#else |
| 1408 |
char c; |
| 1409 |
ptrdiff_t diff = p - &c; |
| 1410 |
return diff < 65536 && diff > -65536; |
| 1411 |
#endif |
| 1412 |
} |
| 1413 |
|
| 1414 |
bool notOnOurStack(char* p) { |
| 1415 |
// Opposite of onOurStack(), except returns true if the check can't be performed. |
| 1416 |
#if KJ_HAS_COMPILER_FEATURE(address_sanitizer) |
| 1417 |
// The stack-use-after-return detection mechanism breaks our ability to check this, so don't. |
| 1418 |
return true; |
| 1419 |
#else |
| 1420 |
return !onOurStack(p); |
| 1421 |
#endif |
| 1422 |
} |
| 1423 |
|
| 1424 |
KJ_TEST("fiber pool runSynchronously()") { |
| 1425 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1426 |
|
| 1427 |
FiberPool pool(65536); |
| 1428 |
|
| 1429 |
{ |
| 1430 |
char c; |
| 1431 |
KJ_EXPECT(onOurStack(&c)); // sanity check... |
| 1432 |
} |
| 1433 |
|
| 1434 |
char* ptr1 = nullptr; |
| 1435 |
char* ptr2 = nullptr; |
| 1436 |
|
| 1437 |
pool.runSynchronously([&]() { |
| 1438 |
char c; |
| 1439 |
ptr1 = &c; |
| 1440 |
}); |
| 1441 |
KJ_ASSERT(ptr1 != nullptr); |
| 1442 |
|
| 1443 |
pool.runSynchronously([&]() { |
| 1444 |
char c; |
| 1445 |
ptr2 = &c; |
| 1446 |
}); |
| 1447 |
KJ_ASSERT(ptr2 != nullptr); |
| 1448 |
|
| 1449 |
#if !KJ_HAS_COMPILER_FEATURE(address_sanitizer) |
| 1450 |
// Should have used the same stack both times, so local var would be in the same place. |
| 1451 |
// Under ASAN, the stack-use-after-return detection correctly fires on this, so we skip the check. |
| 1452 |
KJ_EXPECT(ptr1 == ptr2); |
| 1453 |
#endif |
| 1454 |
|
| 1455 |
// Should have been on a different stack from the main stack. |
| 1456 |
KJ_EXPECT(notOnOurStack(ptr1)); |
| 1457 |
|
| 1458 |
KJ_EXPECT_THROW_MESSAGE("test exception", |
| 1459 |
pool.runSynchronously([&]() { KJ_FAIL_ASSERT("test exception"); })); |
| 1460 |
} |
| 1461 |
|
| 1462 |
KJ_TEST("fiber pool limit") { |
| 1463 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1464 |
|
| 1465 |
FiberPool pool(65536); |
| 1466 |
|
| 1467 |
pool.setMaxFreelist(1); |
| 1468 |
|
| 1469 |
kj::MutexGuarded<uint> state; |
| 1470 |
|
| 1471 |
char* ptr1; |
| 1472 |
char* ptr2; |
| 1473 |
|
| 1474 |
// Run some code that uses two stacks in separate threads at the same time. |
| 1475 |
{ |
| 1476 |
kj::Thread thread([&]() noexcept { |
| 1477 |
auto lock = state.lockExclusive(); |
| 1478 |
lock.wait([](uint val) { return val == 1; }); |
| 1479 |
|
| 1480 |
pool.runSynchronously([&]() { |
| 1481 |
char c; |
| 1482 |
ptr2 = &c; |
| 1483 |
|
| 1484 |
*lock = 2; |
| 1485 |
lock.wait([](uint val) { return val == 3; }); |
| 1486 |
}); |
| 1487 |
}); |
| 1488 |
|
| 1489 |
([&]() noexcept { |
| 1490 |
auto lock = state.lockExclusive(); |
| 1491 |
|
| 1492 |
pool.runSynchronously([&]() { |
| 1493 |
char c; |
| 1494 |
ptr1 = &c; |
| 1495 |
|
| 1496 |
*lock = 1; |
| 1497 |
lock.wait([](uint val) { return val == 2; }); |
| 1498 |
}); |
| 1499 |
|
| 1500 |
*lock = 3; |
| 1501 |
})(); |
| 1502 |
} |
| 1503 |
|
| 1504 |
KJ_EXPECT(pool.getFreelistSize() == 1); |
| 1505 |
|
| 1506 |
// We expect that if we reuse a stack from the pool, it will be the last one that exited, which |
| 1507 |
// is the one from the thread. |
| 1508 |
pool.runSynchronously([&]() { |
| 1509 |
KJ_EXPECT(onOurStack(ptr2)); |
| 1510 |
KJ_EXPECT(notOnOurStack(ptr1)); |
| 1511 |
|
| 1512 |
KJ_EXPECT(pool.getFreelistSize() == 0); |
| 1513 |
}); |
| 1514 |
|
| 1515 |
KJ_EXPECT(pool.getFreelistSize() == 1); |
| 1516 |
|
| 1517 |
// Note that it would NOT work to try to allocate two stacks at the same time again and verify |
| 1518 |
// that the second stack doesn't match the previously-deleted stack, because there's a high |
| 1519 |
// likelihood that the new stack would be allocated in the same location. |
| 1520 |
} |
| 1521 |
|
| 1522 |
#if __GNUC__ >= 12 && !__clang__ |
| 1523 |
// The test below intentionally takes a pointer to a stack variable and stores it past the end |
| 1524 |
// of the function. This seems to trigger a warning in newer GCCs. |
| 1525 |
#pragma GCC diagnostic ignored "-Wdangling-pointer" |
| 1526 |
#endif |
| 1527 |
|
| 1528 |
KJ_TEST("run event loop on freelisted stacks") { |
| 1529 |
if (isLibcContextHandlingKnownBroken()) return; |
| 1530 |
|
| 1531 |
FiberPool pool(65536); |
| 1532 |
|
| 1533 |
class MockEventPort: public EventPort { |
| 1534 |
public: |
| 1535 |
bool wait() override { |
| 1536 |
char c; |
| 1537 |
waitStack = &c; |
| 1538 |
KJ_IF_MAYBE(f, fulfiller) { |
| 1539 |
f->get()->fulfill(); |
| 1540 |
fulfiller = nullptr; |
| 1541 |
} |
| 1542 |
return false; |
| 1543 |
} |
| 1544 |
bool poll() override { |
| 1545 |
char c; |
| 1546 |
pollStack = &c; |
| 1547 |
KJ_IF_MAYBE(f, fulfiller) { |
| 1548 |
f->get()->fulfill(); |
| 1549 |
fulfiller = nullptr; |
| 1550 |
} |
| 1551 |
return false; |
| 1552 |
} |
| 1553 |
|
| 1554 |
char* waitStack = nullptr; |
| 1555 |
char* pollStack = nullptr; |
| 1556 |
|
| 1557 |
kj::Maybe<kj::Own<PromiseFulfiller<void>>> fulfiller; |
| 1558 |
}; |
| 1559 |
|
| 1560 |
MockEventPort port; |
| 1561 |
EventLoop loop(port); |
| 1562 |
WaitScope waitScope(loop); |
| 1563 |
waitScope.runEventCallbacksOnStackPool(pool); |
| 1564 |
|
| 1565 |
{ |
| 1566 |
auto paf = newPromiseAndFulfiller<void>(); |
| 1567 |
port.fulfiller = kj::mv(paf.fulfiller); |
| 1568 |
|
| 1569 |
char* ptr1 = nullptr; |
| 1570 |
char* ptr2 = nullptr; |
| 1571 |
kj::evalLater([&]() { |
| 1572 |
char c; |
| 1573 |
ptr1 = &c; |
| 1574 |
return kj::mv(paf.promise); |
| 1575 |
}).then([&]() { |
| 1576 |
char c; |
| 1577 |
ptr2 = &c; |
| 1578 |
}).wait(waitScope); |
| 1579 |
|
| 1580 |
KJ_EXPECT(ptr1 != nullptr); |
| 1581 |
KJ_EXPECT(ptr2 != nullptr); |
| 1582 |
KJ_EXPECT(port.waitStack != nullptr); |
| 1583 |
KJ_EXPECT(port.pollStack == nullptr); |
| 1584 |
|
| 1585 |
// The event callbacks should have run on a different stack, but the wait should have been on |
| 1586 |
// the main stack. |
| 1587 |
KJ_EXPECT(notOnOurStack(ptr1)); |
| 1588 |
KJ_EXPECT(notOnOurStack(ptr2)); |
| 1589 |
KJ_EXPECT(onOurStack(port.waitStack)); |
| 1590 |
|
| 1591 |
pool.runSynchronously([&]() { |
| 1592 |
// This should run on the same stack where the event callbacks ran. |
| 1593 |
KJ_EXPECT(onOurStack(ptr1)); |
| 1594 |
KJ_EXPECT(onOurStack(ptr2)); |
| 1595 |
KJ_EXPECT(notOnOurStack(port.waitStack)); |
| 1596 |
}); |
| 1597 |
} |
| 1598 |
|
| 1599 |
port.waitStack = nullptr; |
| 1600 |
port.pollStack = nullptr; |
| 1601 |
|
| 1602 |
// Now try poll() instead of wait(). Note that since poll() doesn't block, we let it run on the |
| 1603 |
// event stack. |
| 1604 |
{ |
| 1605 |
auto paf = newPromiseAndFulfiller<void>(); |
| 1606 |
port.fulfiller = kj::mv(paf.fulfiller); |
| 1607 |
|
| 1608 |
char* ptr1 = nullptr; |
| 1609 |
char* ptr2 = nullptr; |
| 1610 |
auto promise = kj::evalLater([&]() { |
| 1611 |
char c; |
| 1612 |
ptr1 = &c; |
| 1613 |
return kj::mv(paf.promise); |
| 1614 |
}).then([&]() { |
| 1615 |
char c; |
| 1616 |
ptr2 = &c; |
| 1617 |
}); |
| 1618 |
|
| 1619 |
KJ_EXPECT(promise.poll(waitScope)); |
| 1620 |
|
| 1621 |
KJ_EXPECT(ptr1 != nullptr); |
| 1622 |
KJ_EXPECT(ptr2 == nullptr); // didn't run because of lazy continuation evaluation |
| 1623 |
KJ_EXPECT(port.waitStack == nullptr); |
| 1624 |
KJ_EXPECT(port.pollStack != nullptr); |
| 1625 |
|
| 1626 |
// The event callback should have run on a different stack, and poll() should have run on |
| 1627 |
// a separate stack too. |
| 1628 |
KJ_EXPECT(notOnOurStack(ptr1)); |
| 1629 |
KJ_EXPECT(notOnOurStack(port.pollStack)); |
| 1630 |
|
| 1631 |
pool.runSynchronously([&]() { |
| 1632 |
// This should run on the same stack where the event callbacks ran. |
| 1633 |
KJ_EXPECT(onOurStack(ptr1)); |
| 1634 |
KJ_EXPECT(onOurStack(port.pollStack)); |
| 1635 |
}); |
| 1636 |
} |
| 1637 |
} |
| 1638 |
#endif |
| 1639 |
|
| 1640 |
KJ_TEST("retryOnDisconnect") { |
| 1641 |
EventLoop loop; |
| 1642 |
WaitScope waitScope(loop); |
| 1643 |
|
| 1644 |
{ |
| 1645 |
uint i = 0; |
| 1646 |
auto promise = retryOnDisconnect([&]() -> Promise<int> { |
| 1647 |
i++; |
| 1648 |
return 123; |
| 1649 |
}); |
| 1650 |
KJ_EXPECT(i == 0); |
| 1651 |
KJ_EXPECT(promise.wait(waitScope) == 123); |
| 1652 |
KJ_EXPECT(i == 1); |
| 1653 |
} |
| 1654 |
|
| 1655 |
{ |
| 1656 |
uint i = 0; |
| 1657 |
auto promise = retryOnDisconnect([&]() -> Promise<int> { |
| 1658 |
if (i++ == 0) { |
| 1659 |
return KJ_EXCEPTION(DISCONNECTED, "test disconnect"); |
| 1660 |
} else { |
| 1661 |
return 123; |
| 1662 |
} |
| 1663 |
}); |
| 1664 |
KJ_EXPECT(i == 0); |
| 1665 |
KJ_EXPECT(promise.wait(waitScope) == 123); |
| 1666 |
KJ_EXPECT(i == 2); |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
{ |
| 1671 |
uint i = 0; |
| 1672 |
auto promise = retryOnDisconnect([&]() -> Promise<int> { |
| 1673 |
if (i++ <= 1) { |
| 1674 |
return KJ_EXCEPTION(DISCONNECTED, "test disconnect", i); |
| 1675 |
} else { |
| 1676 |
return 123; |
| 1677 |
} |
| 1678 |
}); |
| 1679 |
KJ_EXPECT(i == 0); |
| 1680 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("test disconnect; i = 2", |
| 1681 |
promise.ignoreResult().wait(waitScope)); |
| 1682 |
KJ_EXPECT(i == 2); |
| 1683 |
} |
| 1684 |
|
| 1685 |
{ |
| 1686 |
// Test passing a reference to a function. |
| 1687 |
struct Func { |
| 1688 |
uint i = 0; |
| 1689 |
Promise<int> operator()() { |
| 1690 |
if (i++ == 0) { |
| 1691 |
return KJ_EXCEPTION(DISCONNECTED, "test disconnect"); |
| 1692 |
} else { |
| 1693 |
return 123; |
| 1694 |
} |
| 1695 |
} |
| 1696 |
}; |
| 1697 |
Func func; |
| 1698 |
|
| 1699 |
auto promise = retryOnDisconnect(func); |
| 1700 |
KJ_EXPECT(func.i == 0); |
| 1701 |
KJ_EXPECT(promise.wait(waitScope) == 123); |
| 1702 |
KJ_EXPECT(func.i == 2); |
| 1703 |
} |
| 1704 |
} |
| 1705 |
|
| 1706 |
#if (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 17) || (__MINGW32__ && !__MINGW64__) |
| 1707 |
// manylinux2014-x86 doesn't seem to respect `alignas(16)`. I am guessing this is a glibc issue |
| 1708 |
// but I don't really know. It uses glibc 2.17, so testing for that and skipping the test makes |
| 1709 |
// CI work. |
| 1710 |
// |
| 1711 |
// MinGW 32-bit also mysteriously fails this test but I am not going to spend time figuring out |
| 1712 |
// why. |
| 1713 |
#else |
| 1714 |
KJ_TEST("capture weird alignment in continuation") { |
| 1715 |
struct alignas(16) WeirdAlign { |
| 1716 |
~WeirdAlign() { |
| 1717 |
KJ_EXPECT(reinterpret_cast<uintptr_t>(this) % 16 == 0); |
| 1718 |
} |
| 1719 |
int i; |
| 1720 |
}; |
| 1721 |
|
| 1722 |
EventLoop loop; |
| 1723 |
WaitScope waitScope(loop); |
| 1724 |
|
| 1725 |
kj::Promise<void> p = kj::READY_NOW; |
| 1726 |
|
| 1727 |
WeirdAlign value = { 123 }; |
| 1728 |
WeirdAlign value2 = { 456 }; |
| 1729 |
auto p2 = p.then([value, value2]() -> WeirdAlign { |
| 1730 |
return { value.i + value2.i }; |
| 1731 |
}); |
| 1732 |
|
| 1733 |
KJ_EXPECT(p2.wait(waitScope).i == 579); |
| 1734 |
} |
| 1735 |
#endif |
| 1736 |
|
| 1737 |
KJ_TEST("constPromise") { |
| 1738 |
EventLoop loop; |
| 1739 |
WaitScope waitScope(loop); |
| 1740 |
|
| 1741 |
Promise<int> p = constPromise<int, 123>(); |
| 1742 |
int i = p.wait(waitScope); |
| 1743 |
KJ_EXPECT(i == 123); |
| 1744 |
} |
| 1745 |
|
| 1746 |
} // namespace |
| 1747 |
} // namespace kj |