|
9 | 9 | #include <userver/engine/deadline.hpp> |
10 | 10 | #include <userver/engine/future.hpp> |
11 | 11 | #include <userver/engine/single_consumer_event.hpp> |
| 12 | +#include <userver/engine/single_use_event.hpp> |
12 | 13 | #include <userver/engine/sleep.hpp> |
13 | 14 | #include <userver/engine/task/task_with_result.hpp> |
14 | 15 | #include <userver/utest/utest.hpp> |
15 | 16 | #include <userver/utils/async.hpp> |
| 17 | +#include <userver/utils/fast_scope_guard.hpp> |
16 | 18 |
|
17 | 19 | using namespace std::chrono_literals; |
18 | 20 |
|
@@ -435,4 +437,63 @@ UTEST(Cancel, ParentCancelledSample) { |
435 | 437 | EXPECT_FALSE(deadline.IsReached()); |
436 | 438 | } |
437 | 439 |
|
| 440 | +UTEST(Async, CancellationBeforeStartNormal) { |
| 441 | + // The test relies on there only being a single TaskProcessor thread, otherwise there could be a race |
| 442 | + // between `RequestCancel()` and the concurrent execution of the child task. |
| 443 | + ASSERT_EQ(GetThreadCount(), 1); |
| 444 | + |
| 445 | + bool captures_destroyed = false; |
| 446 | + utils::FastScopeGuard guard([&captures_destroyed]() noexcept { |
| 447 | + EXPECT_TRUE(engine::current_task::IsTaskProcessorThread()); |
| 448 | + captures_destroyed = true; |
| 449 | + }); |
| 450 | + |
| 451 | + auto task = engine::AsyncNoSpan([guard = std::move(guard)] { ADD_FAILURE() << "The task body should not start"; }); |
| 452 | + |
| 453 | + EXPECT_FALSE(task.IsFinished()); |
| 454 | + EXPECT_FALSE(captures_destroyed); |
| 455 | + |
| 456 | + // The task has not started by this point. This means that the task is cancelled before starting. |
| 457 | + // This should cause the task body to be skipped, because this is a non-Critical task. |
| 458 | + // See details on engine::TaskBase::State::kCancelled. |
| 459 | + task.RequestCancel(); |
| 460 | + |
| 461 | + task.WaitFor(utest::kMaxTestWaitTime); |
| 462 | + EXPECT_EQ(task.GetState(), engine::TaskBase::State::kCancelled); |
| 463 | + UEXPECT_THROW(task.Get(), engine::TaskCancelledException); |
| 464 | + // The task destructor should run even if the execution of the task body is skipped due to cancellation. |
| 465 | + EXPECT_TRUE(captures_destroyed); |
| 466 | +} |
| 467 | + |
| 468 | +UTEST(Async, CancellationBeforeStartCritical) { |
| 469 | + // The test relies on there only being a single TaskProcessor thread, otherwise there could be a race |
| 470 | + // between `RequestCancel()` and the concurrent execution of the child task. |
| 471 | + ASSERT_EQ(GetThreadCount(), 1); |
| 472 | + |
| 473 | + engine::SingleUseEvent event; |
| 474 | + auto task = engine::CriticalAsyncNoSpan([&event] { |
| 475 | + // The task should start (proven by `return true;` below and `Get() -> true`.) |
| 476 | + |
| 477 | + EXPECT_TRUE(engine::current_task::impl::IsCritical()); |
| 478 | + |
| 479 | + // The task is still cancelled, and any waiting operation will be interrupted. |
| 480 | + EXPECT_TRUE(engine::current_task::IsCancelRequested()); |
| 481 | + EXPECT_TRUE(engine::current_task::ShouldCancel()); |
| 482 | + const auto deadline = engine::Deadline::FromDuration(utest::kMaxTestWaitTime); |
| 483 | + EXPECT_EQ(event.WaitUntil(deadline), engine::FutureStatus::kCancelled); |
| 484 | + |
| 485 | + return true; |
| 486 | + }); |
| 487 | + |
| 488 | + EXPECT_FALSE(task.IsFinished()); |
| 489 | + |
| 490 | + // The task has not started by this point. This means that the task is cancelled before starting. |
| 491 | + // But because the task is started as Critical, the task body should be executed anyway. |
| 492 | + task.RequestCancel(); |
| 493 | + |
| 494 | + task.WaitFor(utest::kMaxTestWaitTime / 2); |
| 495 | + EXPECT_EQ(task.GetState(), engine::TaskBase::State::kCompleted); |
| 496 | + EXPECT_TRUE(task.Get()); |
| 497 | +} |
| 498 | + |
438 | 499 | USERVER_NAMESPACE_END |
0 commit comments