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