| 47 | }; |
| 48 | |
| 49 | TEST_F(SleepTest, testSleep) { |
| 50 | executors::SimpleExecutor e1(5); |
| 51 | |
| 52 | auto sleepTask = [&]() -> Lazy<> { |
| 53 | auto current = co_await CurrentExecutor(); |
| 54 | EXPECT_EQ(&e1, current); |
| 55 | |
| 56 | auto startTime = std::chrono::system_clock::now(); |
| 57 | co_await coro::sleep(1s); |
| 58 | auto endTime = std::chrono::system_clock::now(); |
| 59 | |
| 60 | current = co_await CurrentExecutor(); |
| 61 | EXPECT_EQ(&e1, current); |
| 62 | |
| 63 | auto duration = endTime - startTime; |
| 64 | cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration) |
| 65 | .count() |
| 66 | << endl; |
| 67 | }; |
| 68 | |
| 69 | syncAwait(sleepTask().via(&e1)); |
| 70 | |
| 71 | auto sleepTask2 = [&]() -> Lazy<> { |
| 72 | auto current = co_await CurrentExecutor(); |
| 73 | EXPECT_EQ(&e1, current); |
| 74 | |
| 75 | auto startTime = std::chrono::system_clock::now(); |
| 76 | co_await coro::sleep(900ms); |
| 77 | auto endTime = std::chrono::system_clock::now(); |
| 78 | |
| 79 | current = co_await CurrentExecutor(); |
| 80 | EXPECT_EQ(&e1, current); |
| 81 | |
| 82 | auto duration = endTime - startTime; |
| 83 | cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration) |
| 84 | .count() |
| 85 | << endl; |
| 86 | }; |
| 87 | |
| 88 | syncAwait(sleepTask2().via(&e1)); |
| 89 | |
| 90 | auto sleepTask3 = [&]() -> Lazy<> { |
| 91 | class Executor : public async_simple::Executor { |
| 92 | public: |
| 93 | Executor() = default; |
| 94 | virtual bool schedule(Func) override { return true; } |
| 95 | virtual void schedule(Func func, Duration dur) override { |
| 96 | std::thread([this, func = std::move(func), dur]() { |
| 97 | id = std::this_thread::get_id(); |
| 98 | std::this_thread::sleep_for(dur); |
| 99 | func(); |
| 100 | }).detach(); |
| 101 | } |
| 102 | virtual void schedule(Func func, Duration dur, uint64_t, |
| 103 | Slot*) override { |
| 104 | schedule(std::move(func), dur); |
| 105 | } |
| 106 |
nothing calls this directly
no test coverage detected