vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <forward_list> |
| 35 | #include <functional> |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 36 | #include <mutex> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 37 | #include <sys/time.h> |
| 38 | #include <sys/resource.h> |
| 39 | #include <sys/signal.h> |
| 40 | #include <thread> |
| 41 | |
| 42 | #include <gflags/gflags.h> |
| 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/host_port.h> |
| 45 | #include <grpc++/async_unary_call.h> |
| 46 | #include <grpc++/config.h> |
| 47 | #include <grpc++/server.h> |
| 48 | #include <grpc++/server_builder.h> |
| 49 | #include <grpc++/server_context.h> |
Craig Tiller | 61691f9 | 2015-03-02 08:51:52 -0800 | [diff] [blame] | 50 | #include <grpc++/server_credentials.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 51 | #include <grpc++/status.h> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 52 | #include <grpc++/stream.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 53 | #include <gtest/gtest.h> |
| 54 | #include "src/cpp/server/thread_pool.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 55 | #include "test/cpp/qps/qpstest.grpc.pb.h" |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 56 | #include "test/cpp/qps/server.h" |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 57 | |
| 58 | #include <grpc/grpc.h> |
| 59 | #include <grpc/support/log.h> |
| 60 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 61 | namespace grpc { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 62 | namespace testing { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 63 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 64 | class AsyncQpsServerTest : public Server { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 65 | public: |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 66 | AsyncQpsServerTest(const ServerConfig &config, int port) : shutdown_(false) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 67 | char *server_address = NULL; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 68 | gpr_join_host_port(&server_address, "::", port); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 69 | |
| 70 | ServerBuilder builder; |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 71 | builder.AddListeningPort(server_address, InsecureServerCredentials()); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 72 | gpr_free(server_address); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 73 | |
| 74 | builder.RegisterAsyncService(&async_service_); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 75 | srv_cq_ = builder.AddCompletionQueue(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 76 | |
| 77 | server_ = builder.BuildAndStart(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 78 | |
| 79 | using namespace std::placeholders; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 80 | request_unary_ = |
| 81 | std::bind(&TestService::AsyncService::RequestUnaryCall, &async_service_, |
| 82 | _1, _2, _3, srv_cq_.get(), srv_cq_.get(), _4); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 83 | request_streaming_ = |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 84 | std::bind(&TestService::AsyncService::RequestStreamingCall, |
| 85 | &async_service_, _1, _2, srv_cq_.get(), srv_cq_.get(), _3); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 86 | for (int i = 0; i < 100; i++) { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 87 | contexts_.push_front( |
| 88 | new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>( |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 89 | request_unary_, ProcessRPC)); |
| 90 | contexts_.push_front( |
| 91 | new ServerRpcContextStreamingImpl<SimpleRequest, SimpleResponse>( |
| 92 | request_streaming_, ProcessRPC)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 93 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 94 | for (int i = 0; i < config.threads(); i++) { |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 95 | threads_.push_back(std::thread([=]() { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 96 | // Wait until work is available or we are shutting down |
| 97 | bool ok; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 98 | void *got_tag; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 99 | while (srv_cq_->Next(&got_tag, &ok)) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 100 | ServerRpcContext *ctx = detag(got_tag); |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 101 | // The tag is a pointer to an RPC context to invoke |
Craig Tiller | aeea2f2 | 2015-05-21 13:54:42 -0700 | [diff] [blame^] | 102 | bool still_going = ctx->RunNextState(ok); |
| 103 | std::lock_guard<std::mutex> g(shutdown_mutex_); |
| 104 | if (!shutdown_) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 105 | // this RPC context is done, so refresh it |
Craig Tiller | aeea2f2 | 2015-05-21 13:54:42 -0700 | [diff] [blame^] | 106 | if (!still_going) { |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 107 | ctx->Reset(); |
| 108 | } |
Craig Tiller | aeea2f2 | 2015-05-21 13:54:42 -0700 | [diff] [blame^] | 109 | } else { |
| 110 | return; |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 111 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 112 | } |
| 113 | return; |
| 114 | })); |
| 115 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 116 | } |
| 117 | ~AsyncQpsServerTest() { |
| 118 | server_->Shutdown(); |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 119 | { |
| 120 | std::lock_guard<std::mutex> g(shutdown_mutex_); |
| 121 | shutdown_ = true; |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 122 | } |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 123 | for (auto thr = threads_.begin(); thr != threads_.end(); thr++) { |
| 124 | thr->join(); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 125 | } |
Craig Tiller | aeea2f2 | 2015-05-21 13:54:42 -0700 | [diff] [blame^] | 126 | srv_cq_->Shutdown(); |
| 127 | bool ok; |
| 128 | void *got_tag; |
| 129 | while (srv_cq_->Next(&got_tag, &ok)); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 130 | while (!contexts_.empty()) { |
| 131 | delete contexts_.front(); |
| 132 | contexts_.pop_front(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 135 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 136 | private: |
| 137 | class ServerRpcContext { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 138 | public: |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 139 | ServerRpcContext() {} |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 140 | virtual ~ServerRpcContext(){}; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 141 | virtual bool RunNextState(bool) = 0; // next state, return false if done |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 142 | virtual void Reset() = 0; // start this back at a clean state |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 143 | }; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 144 | static void *tag(ServerRpcContext *func) { |
| 145 | return reinterpret_cast<void *>(func); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 146 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 147 | static ServerRpcContext *detag(void *tag) { |
| 148 | return reinterpret_cast<ServerRpcContext *>(tag); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | template <class RequestType, class ResponseType> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 152 | class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 153 | public: |
| 154 | ServerRpcContextUnaryImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 155 | std::function<void(ServerContext *, RequestType *, |
| 156 | grpc::ServerAsyncResponseWriter<ResponseType> *, |
| 157 | void *)> request_method, |
| 158 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 159 | invoke_method) |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 160 | : next_state_(&ServerRpcContextUnaryImpl::invoker), |
| 161 | request_method_(request_method), |
| 162 | invoke_method_(invoke_method), |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 163 | response_writer_(&srv_ctx_) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 164 | request_method_(&srv_ctx_, &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 165 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 166 | } |
vjpai | 3c11066 | 2015-02-27 07:17:16 -0800 | [diff] [blame] | 167 | ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {} |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 168 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 169 | return (this->*next_state_)(ok); |
| 170 | } |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 171 | void Reset() GRPC_OVERRIDE { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 172 | srv_ctx_ = ServerContext(); |
| 173 | req_ = RequestType(); |
| 174 | response_writer_ = |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 175 | grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 176 | |
| 177 | // Then request the method |
| 178 | next_state_ = &ServerRpcContextUnaryImpl::invoker; |
| 179 | request_method_(&srv_ctx_, &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 180 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 181 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 182 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 183 | private: |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 184 | bool finisher(bool) { return false; } |
| 185 | bool invoker(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 186 | if (!ok) { |
| 187 | return false; |
| 188 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 189 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 190 | ResponseType response; |
| 191 | |
| 192 | // Call the RPC processing function |
| 193 | grpc::Status status = invoke_method_(&req_, &response); |
| 194 | |
| 195 | // Have the response writer work and invoke on_finish when done |
| 196 | next_state_ = &ServerRpcContextUnaryImpl::finisher; |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 197 | response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | ServerContext srv_ctx_; |
| 201 | RequestType req_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 202 | bool (ServerRpcContextUnaryImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 203 | std::function<void(ServerContext *, RequestType *, |
| 204 | grpc::ServerAsyncResponseWriter<ResponseType> *, void *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 205 | request_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 206 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 207 | invoke_method_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 208 | grpc::ServerAsyncResponseWriter<ResponseType> response_writer_; |
| 209 | }; |
| 210 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 211 | template <class RequestType, class ResponseType> |
| 212 | class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext { |
| 213 | public: |
| 214 | ServerRpcContextStreamingImpl( |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 215 | std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter< |
| 216 | ResponseType, RequestType> *, |
| 217 | void *)> request_method, |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 218 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 219 | invoke_method) |
| 220 | : next_state_(&ServerRpcContextStreamingImpl::request_done), |
| 221 | request_method_(request_method), |
| 222 | invoke_method_(invoke_method), |
| 223 | stream_(&srv_ctx_) { |
| 224 | request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this)); |
| 225 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 226 | ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {} |
| 227 | bool RunNextState(bool ok) GRPC_OVERRIDE { |
| 228 | return (this->*next_state_)(ok); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 229 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 230 | void Reset() GRPC_OVERRIDE { |
| 231 | srv_ctx_ = ServerContext(); |
| 232 | req_ = RequestType(); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 233 | stream_ = |
| 234 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType>(&srv_ctx_); |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 235 | |
| 236 | // Then request the method |
| 237 | next_state_ = &ServerRpcContextStreamingImpl::request_done; |
| 238 | request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this)); |
| 239 | } |
| 240 | |
| 241 | private: |
| 242 | bool request_done(bool ok) { |
Craig Tiller | 8221e40 | 2015-04-09 12:23:21 -0700 | [diff] [blame] | 243 | if (!ok) { |
| 244 | return false; |
| 245 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 246 | stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 247 | next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 248 | return true; |
| 249 | } |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 250 | |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 251 | bool read_done(bool ok) { |
| 252 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 253 | // invoke the method |
| 254 | ResponseType response; |
| 255 | // Call the RPC processing function |
| 256 | grpc::Status status = invoke_method_(&req_, &response); |
| 257 | // initiate the write |
| 258 | stream_.Write(response, AsyncQpsServerTest::tag(this)); |
| 259 | next_state_ = &ServerRpcContextStreamingImpl::write_done; |
| 260 | } else { // client has sent writes done |
| 261 | // finish the stream |
| 262 | stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this)); |
| 263 | next_state_ = &ServerRpcContextStreamingImpl::finish_done; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | bool write_done(bool ok) { |
| 268 | // now go back and get another streaming read! |
| 269 | if (ok) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 270 | stream_.Read(&req_, AsyncQpsServerTest::tag(this)); |
| 271 | next_state_ = &ServerRpcContextStreamingImpl::read_done; |
| 272 | } else { |
| 273 | stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this)); |
| 274 | next_state_ = &ServerRpcContextStreamingImpl::finish_done; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 275 | } |
| 276 | return true; |
| 277 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 278 | bool finish_done(bool ok) { return false; /* reset the context */ } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 279 | |
| 280 | ServerContext srv_ctx_; |
| 281 | RequestType req_; |
| 282 | bool (ServerRpcContextStreamingImpl::*next_state_)(bool); |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 283 | std::function<void( |
| 284 | ServerContext *, |
| 285 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> *, void *)> |
| 286 | request_method_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 287 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 288 | invoke_method_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 289 | grpc::ServerAsyncReaderWriter<ResponseType, RequestType> stream_; |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 290 | }; |
| 291 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 292 | static Status ProcessRPC(const SimpleRequest *request, |
| 293 | SimpleResponse *response) { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 294 | if (request->response_size() > 0) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 295 | if (!SetPayload(request->response_type(), request->response_size(), |
| 296 | response->mutable_payload())) { |
| 297 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 298 | } |
| 299 | } |
| 300 | return Status::OK; |
| 301 | } |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 302 | std::vector<std::thread> threads_; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 303 | std::unique_ptr<grpc::Server> server_; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 304 | std::unique_ptr<grpc::ServerCompletionQueue> srv_cq_; |
| 305 | TestService::AsyncService async_service_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 306 | std::function<void(ServerContext *, SimpleRequest *, |
| 307 | grpc::ServerAsyncResponseWriter<SimpleResponse> *, void *)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 308 | request_unary_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 309 | std::function<void( |
| 310 | ServerContext *, |
| 311 | grpc::ServerAsyncReaderWriter<SimpleResponse, SimpleRequest> *, void *)> |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 312 | request_streaming_; |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 313 | std::forward_list<ServerRpcContext *> contexts_; |
Vijay Pai | 8ad3209 | 2015-03-23 12:44:28 -0700 | [diff] [blame] | 314 | |
| 315 | std::mutex shutdown_mutex_; |
| 316 | bool shutdown_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 317 | }; |
| 318 | |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 319 | std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config, |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 320 | int port) { |
| 321 | return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 324 | } // namespace testing |
| 325 | } // namespace grpc |