| 121 | } |
| 122 | |
| 123 | Awaitable<void> func(brpc::Channel& channel, int* out) { |
| 124 | Trace t("func"); |
| 125 | test::EchoService_Stub stub(&channel); |
| 126 | test::EchoRequest request; |
| 127 | request.set_message("hello world"); |
| 128 | test::EchoResponse response; |
| 129 | brpc::Controller cntl; |
| 130 | |
| 131 | LOG(INFO) << "before start coroutine"; |
| 132 | Coroutine coro(sleep_func()); |
| 133 | usleep(delay_us); |
| 134 | LOG(INFO) << "before wait coroutine"; |
| 135 | int ret = co_await coro.awaitable<int>(); |
| 136 | EXPECT_EQ(ret, 123); |
| 137 | LOG(INFO) << "after wait coroutine, ret:" << ret; |
| 138 | |
| 139 | auto str = co_await inplace_func("hello"); |
| 140 | EXPECT_EQ("hello", str); |
| 141 | |
| 142 | float num = 0.0; |
| 143 | try { |
| 144 | num = co_await exception_func(); |
| 145 | } catch(std::string str) { |
| 146 | EXPECT_EQ("error", str); |
| 147 | num = 1.0; |
| 148 | } |
| 149 | EXPECT_EQ(1.0, num); |
| 150 | |
| 151 | AwaitableDone done; |
| 152 | LOG(INFO) << "start echo"; |
| 153 | stub.Echo(&cntl, &request, &response, &done); |
| 154 | LOG(INFO) << "after echo"; |
| 155 | usleep(delay_us); |
| 156 | co_await done.awaitable(); |
| 157 | LOG(INFO) << "after wait"; |
| 158 | EXPECT_FALSE(cntl.Failed()) << cntl.ErrorText(); |
| 159 | EXPECT_EQ("hello world", response.message()); |
| 160 | |
| 161 | cntl.Reset(); |
| 162 | request.set_sleep_us(2000); |
| 163 | AwaitableDone done2; |
| 164 | LOG(INFO) << "start echo2"; |
| 165 | int64_t s = butil::monotonic_time_us(); |
| 166 | stub.Echo(&cntl, &request, &response, &done2); |
| 167 | LOG(INFO) << "after echo2"; |
| 168 | co_await done2.awaitable(); |
| 169 | int cost = butil::monotonic_time_us() - s; |
| 170 | LOG(INFO) << "after wait2"; |
| 171 | EXPECT_GE(cost, 2000); |
| 172 | EXPECT_FALSE(cntl.Failed()) << cntl.ErrorText(); |
| 173 | EXPECT_EQ("hello world", response.message()); |
| 174 | |
| 175 | *out = 456; |
| 176 | } |
| 177 | |
| 178 | TEST_F(CoroutineTest, coroutine) { |
| 179 | butil::EndPoint ep; |
no test coverage detected