| 44 | constexpr std::string_view kDummyName = "unit test"; |
| 45 | |
| 46 | TEST(AsyncTaskScheduler, ShouldScheduleConcurrentTasks) { |
| 47 | // A basic test to make sure we schedule the right number of concurrent tasks |
| 48 | constexpr int kMaxConcurrentTasks = 2; |
| 49 | constexpr int kTotalNumTasks = kMaxConcurrentTasks + 1; |
| 50 | Future<> futures[kTotalNumTasks]; |
| 51 | bool submitted[kTotalNumTasks]; |
| 52 | Future<> finished = AsyncTaskScheduler::Make([&](AsyncTaskScheduler* scheduler) { |
| 53 | std::shared_ptr<ThrottledAsyncTaskScheduler> throttled = |
| 54 | ThrottledAsyncTaskScheduler::Make(scheduler, kMaxConcurrentTasks); |
| 55 | for (int i = 0; i < kTotalNumTasks; i++) { |
| 56 | futures[i] = Future<>::Make(); |
| 57 | submitted[i] = false; |
| 58 | throttled->AddSimpleTask( |
| 59 | [&, i] { |
| 60 | submitted[i] = true; |
| 61 | return futures[i]; |
| 62 | }, |
| 63 | kDummyName); |
| 64 | } |
| 65 | return Status::OK(); |
| 66 | }); |
| 67 | AssertNotFinished(finished); |
| 68 | for (int i = 0; i < kTotalNumTasks; i++) { |
| 69 | if (i < kMaxConcurrentTasks) { |
| 70 | ASSERT_TRUE(submitted[i]); |
| 71 | } else { |
| 72 | ASSERT_FALSE(submitted[i]); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | for (int j = 0; j < kTotalNumTasks; j++) { |
| 77 | futures[j].MarkFinished(); |
| 78 | if (j + kMaxConcurrentTasks < kTotalNumTasks) { |
| 79 | ASSERT_TRUE(submitted[j + kMaxConcurrentTasks]); |
| 80 | } |
| 81 | } |
| 82 | ASSERT_FINISHES_OK(finished); |
| 83 | } |
| 84 | |
| 85 | TEST(AsyncTaskScheduler, CancelWaitsForTasksToFinish) { |
| 86 | StopSource stop_source; |
nothing calls this directly
no test coverage detected