MCPcopy Create free account
hub / github.com/alibaba/async_simple / TEST_F

Function TEST_F

async_simple/coro/test/SleepTest.cpp:49–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47};
48
49TEST_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

Callers

nothing calls this directly

Calls 6

CurrentExecutorClass · 0.85
sleepFunction · 0.85
syncAwaitFunction · 0.85
cancelSleepFunction · 0.85
viaMethod · 0.80
countMethod · 0.45

Tested by

no test coverage detected