mirror of https://github.com/grpc/grpc.git
commit
8dbe2cb071
253 changed files with 6543 additions and 2602 deletions
@ -0,0 +1,153 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <iostream> |
||||||
|
#include <memory> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include <grpc++/grpc++.h> |
||||||
|
#include <thread> |
||||||
|
|
||||||
|
#include "helloworld.grpc.pb.h" |
||||||
|
|
||||||
|
using grpc::Channel; |
||||||
|
using grpc::ClientAsyncResponseReader; |
||||||
|
using grpc::ClientContext; |
||||||
|
using grpc::CompletionQueue; |
||||||
|
using grpc::Status; |
||||||
|
using helloworld::HelloRequest; |
||||||
|
using helloworld::HelloReply; |
||||||
|
using helloworld::Greeter; |
||||||
|
|
||||||
|
class GreeterClient { |
||||||
|
public: |
||||||
|
explicit GreeterClient(std::shared_ptr<Channel> channel) |
||||||
|
: stub_(Greeter::NewStub(channel)) {} |
||||||
|
|
||||||
|
// Assembles the client's payload and sends it to the server.
|
||||||
|
void SayHello(const std::string& user) { |
||||||
|
// Data we are sending to the server.
|
||||||
|
HelloRequest request; |
||||||
|
request.set_name(user); |
||||||
|
|
||||||
|
// Call object to store rpc data
|
||||||
|
AsyncClientCall* call = new AsyncClientCall; |
||||||
|
|
||||||
|
// stub_->AsyncSayHello() performs the RPC call, returning an instance to
|
||||||
|
// store in "call". Because we are using the asynchronous API, we need to
|
||||||
|
// hold on to the "call" instance in order to get updates on the ongoing RPC.
|
||||||
|
call->response_reader = stub_->AsyncSayHello(&call->context, request, &cq_); |
||||||
|
|
||||||
|
|
||||||
|
// Request that, upon completion of the RPC, "reply" be updated with the
|
||||||
|
// server's response; "status" with the indication of whether the operation
|
||||||
|
// was successful. Tag the request with the memory address of the call object.
|
||||||
|
call->response_reader->Finish(&call->reply, &call->status, (void*)call); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
// Loop while listening for completed responses.
|
||||||
|
// Prints out the response from the server.
|
||||||
|
void AsyncCompleteRpc() { |
||||||
|
void* got_tag; |
||||||
|
bool ok = false; |
||||||
|
|
||||||
|
// Block until the next result is available in the completion queue "cq".
|
||||||
|
while (cq_.Next(&got_tag, &ok)) { |
||||||
|
// The tag in this example is the memory location of the call object
|
||||||
|
AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag); |
||||||
|
|
||||||
|
// Verify that the request was completed successfully. Note that "ok"
|
||||||
|
// corresponds solely to the request for updates introduced by Finish().
|
||||||
|
GPR_ASSERT(ok); |
||||||
|
|
||||||
|
if (call->status.ok()) |
||||||
|
std::cout << "Greeter received: " << call->reply.message() << std::endl; |
||||||
|
else |
||||||
|
std::cout << "RPC failed" << std::endl; |
||||||
|
|
||||||
|
// Once we're complete, deallocate the call object.
|
||||||
|
delete call; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
|
||||||
|
// struct for keeping state and data information
|
||||||
|
struct AsyncClientCall { |
||||||
|
// Container for the data we expect from the server.
|
||||||
|
HelloReply reply; |
||||||
|
|
||||||
|
// Context for the client. It could be used to convey extra information to
|
||||||
|
// the server and/or tweak certain RPC behaviors.
|
||||||
|
ClientContext context; |
||||||
|
|
||||||
|
// Storage for the status of the RPC upon completion.
|
||||||
|
Status status; |
||||||
|
|
||||||
|
|
||||||
|
std::unique_ptr<ClientAsyncResponseReader<HelloReply>> response_reader; |
||||||
|
}; |
||||||
|
|
||||||
|
// Out of the passed in Channel comes the stub, stored here, our view of the
|
||||||
|
// server's exposed services.
|
||||||
|
std::unique_ptr<Greeter::Stub> stub_; |
||||||
|
|
||||||
|
// The producer-consumer queue we use to communicate asynchronously with the
|
||||||
|
// gRPC runtime.
|
||||||
|
CompletionQueue cq_; |
||||||
|
}; |
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
|
||||||
|
|
||||||
|
// Instantiate the client. It requires a channel, out of which the actual RPCs
|
||||||
|
// are created. This channel models a connection to an endpoint (in this case,
|
||||||
|
// localhost at port 50051). We indicate that the channel isn't authenticated
|
||||||
|
// (use of InsecureChannelCredentials()).
|
||||||
|
GreeterClient greeter(grpc::CreateChannel( |
||||||
|
"localhost:50051", grpc::InsecureChannelCredentials())); |
||||||
|
|
||||||
|
// Spawn reader thread that loops indefinitely
|
||||||
|
std::thread thread_ = std::thread(&GreeterClient::AsyncCompleteRpc, &greeter); |
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) { |
||||||
|
std::string user("world " + std::to_string(i)); |
||||||
|
greeter.SayHello(user); // The actual RPC call!
|
||||||
|
} |
||||||
|
|
||||||
|
std::cout << "Press control-c to quit" << std::endl << std::endl; |
||||||
|
thread_.join(); //blocks forever
|
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,57 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef GRPC_IMPL_CODEGEN_BYTE_BUFFER_READER_H |
||||||
|
#define GRPC_IMPL_CODEGEN_BYTE_BUFFER_READER_H |
||||||
|
|
||||||
|
#include <grpc/impl/codegen/byte_buffer.h> |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
extern "C" { |
||||||
|
#endif |
||||||
|
|
||||||
|
struct grpc_byte_buffer_reader { |
||||||
|
grpc_byte_buffer *buffer_in; |
||||||
|
grpc_byte_buffer *buffer_out; |
||||||
|
/* Different current objects correspond to different types of byte buffers */ |
||||||
|
union { |
||||||
|
/* Index into a slice buffer's array of slices */ |
||||||
|
unsigned index; |
||||||
|
} current; |
||||||
|
}; |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
#endif /* GRPC_IMPL_CODEGEN_BYTE_BUFFER_READER_H */ |
@ -1,182 +0,0 @@ |
|||||||
/*
|
|
||||||
* |
|
||||||
* Copyright 2016, Google Inc. |
|
||||||
* All rights reserved. |
|
||||||
* |
|
||||||
* Redistribution and use in source and binary forms, with or without |
|
||||||
* modification, are permitted provided that the following conditions are |
|
||||||
* met: |
|
||||||
* |
|
||||||
* * Redistributions of source code must retain the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer. |
|
||||||
* * Redistributions in binary form must reproduce the above |
|
||||||
* copyright notice, this list of conditions and the following disclaimer |
|
||||||
* in the documentation and/or other materials provided with the |
|
||||||
* distribution. |
|
||||||
* * Neither the name of Google Inc. nor the names of its |
|
||||||
* contributors may be used to endorse or promote products derived from |
|
||||||
* this software without specific prior written permission. |
|
||||||
* |
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
||||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
||||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
||||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
||||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
||||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
||||||
* |
|
||||||
*/ |
|
||||||
/* Automatically generated nanopb header */ |
|
||||||
/* Generated by nanopb-0.3.5-dev */ |
|
||||||
|
|
||||||
#ifndef PB_LOAD_BALANCER_PB_H_INCLUDED |
|
||||||
#define PB_LOAD_BALANCER_PB_H_INCLUDED |
|
||||||
#include "third_party/nanopb/pb.h" |
|
||||||
#if PB_PROTO_HEADER_VERSION != 30 |
|
||||||
#error Regenerate this file with the current version of nanopb generator. |
|
||||||
#endif |
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
extern "C" { |
|
||||||
#endif |
|
||||||
|
|
||||||
/* Struct definitions */ |
|
||||||
typedef struct _grpc_lb_v0_ClientStats { |
|
||||||
bool has_total_requests; |
|
||||||
int64_t total_requests; |
|
||||||
bool has_client_rpc_errors; |
|
||||||
int64_t client_rpc_errors; |
|
||||||
bool has_dropped_requests; |
|
||||||
int64_t dropped_requests; |
|
||||||
} grpc_lb_v0_ClientStats; |
|
||||||
|
|
||||||
typedef struct _grpc_lb_v0_Duration { |
|
||||||
bool has_seconds; |
|
||||||
int64_t seconds; |
|
||||||
bool has_nanos; |
|
||||||
int32_t nanos; |
|
||||||
} grpc_lb_v0_Duration; |
|
||||||
|
|
||||||
typedef struct _grpc_lb_v0_InitialLoadBalanceRequest { |
|
||||||
bool has_name; |
|
||||||
char name[128]; |
|
||||||
} grpc_lb_v0_InitialLoadBalanceRequest; |
|
||||||
|
|
||||||
typedef PB_BYTES_ARRAY_T(64) grpc_lb_v0_Server_load_balance_token_t; |
|
||||||
typedef struct _grpc_lb_v0_Server { |
|
||||||
bool has_ip_address; |
|
||||||
char ip_address[46]; |
|
||||||
bool has_port; |
|
||||||
int32_t port; |
|
||||||
bool has_load_balance_token; |
|
||||||
grpc_lb_v0_Server_load_balance_token_t load_balance_token; |
|
||||||
bool has_drop_request; |
|
||||||
bool drop_request; |
|
||||||
} grpc_lb_v0_Server; |
|
||||||
|
|
||||||
typedef struct _grpc_lb_v0_InitialLoadBalanceResponse { |
|
||||||
bool has_client_config; |
|
||||||
char client_config[64]; |
|
||||||
bool has_load_balancer_delegate; |
|
||||||
char load_balancer_delegate[64]; |
|
||||||
bool has_client_stats_report_interval; |
|
||||||
grpc_lb_v0_Duration client_stats_report_interval; |
|
||||||
} grpc_lb_v0_InitialLoadBalanceResponse; |
|
||||||
|
|
||||||
typedef struct _grpc_lb_v0_LoadBalanceRequest { |
|
||||||
bool has_initial_request; |
|
||||||
grpc_lb_v0_InitialLoadBalanceRequest initial_request; |
|
||||||
bool has_client_stats; |
|
||||||
grpc_lb_v0_ClientStats client_stats; |
|
||||||
} grpc_lb_v0_LoadBalanceRequest; |
|
||||||
|
|
||||||
typedef struct _grpc_lb_v0_ServerList { |
|
||||||
pb_callback_t servers; |
|
||||||
bool has_expiration_interval; |
|
||||||
grpc_lb_v0_Duration expiration_interval; |
|
||||||
} grpc_lb_v0_ServerList; |
|
||||||
|
|
||||||
typedef struct _grpc_lb_v0_LoadBalanceResponse { |
|
||||||
bool has_initial_response; |
|
||||||
grpc_lb_v0_InitialLoadBalanceResponse initial_response; |
|
||||||
bool has_server_list; |
|
||||||
grpc_lb_v0_ServerList server_list; |
|
||||||
} grpc_lb_v0_LoadBalanceResponse; |
|
||||||
|
|
||||||
/* Default values for struct fields */ |
|
||||||
|
|
||||||
/* Initializer values for message structs */ |
|
||||||
#define grpc_lb_v0_Duration_init_default {false, 0, false, 0} |
|
||||||
#define grpc_lb_v0_LoadBalanceRequest_init_default {false, grpc_lb_v0_InitialLoadBalanceRequest_init_default, false, grpc_lb_v0_ClientStats_init_default} |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceRequest_init_default {false, ""} |
|
||||||
#define grpc_lb_v0_ClientStats_init_default {false, 0, false, 0, false, 0} |
|
||||||
#define grpc_lb_v0_LoadBalanceResponse_init_default {false, grpc_lb_v0_InitialLoadBalanceResponse_init_default, false, grpc_lb_v0_ServerList_init_default} |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceResponse_init_default {false, "", false, "", false, grpc_lb_v0_Duration_init_default} |
|
||||||
#define grpc_lb_v0_ServerList_init_default {{{NULL}, NULL}, false, grpc_lb_v0_Duration_init_default} |
|
||||||
#define grpc_lb_v0_Server_init_default {false, "", false, 0, false, {0, {0}}, false, 0} |
|
||||||
#define grpc_lb_v0_Duration_init_zero {false, 0, false, 0} |
|
||||||
#define grpc_lb_v0_LoadBalanceRequest_init_zero {false, grpc_lb_v0_InitialLoadBalanceRequest_init_zero, false, grpc_lb_v0_ClientStats_init_zero} |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceRequest_init_zero {false, ""} |
|
||||||
#define grpc_lb_v0_ClientStats_init_zero {false, 0, false, 0, false, 0} |
|
||||||
#define grpc_lb_v0_LoadBalanceResponse_init_zero {false, grpc_lb_v0_InitialLoadBalanceResponse_init_zero, false, grpc_lb_v0_ServerList_init_zero} |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceResponse_init_zero {false, "", false, "", false, grpc_lb_v0_Duration_init_zero} |
|
||||||
#define grpc_lb_v0_ServerList_init_zero {{{NULL}, NULL}, false, grpc_lb_v0_Duration_init_zero} |
|
||||||
#define grpc_lb_v0_Server_init_zero {false, "", false, 0, false, {0, {0}}, false, 0} |
|
||||||
|
|
||||||
/* Field tags (for use in manual encoding/decoding) */ |
|
||||||
#define grpc_lb_v0_ClientStats_total_requests_tag 1 |
|
||||||
#define grpc_lb_v0_ClientStats_client_rpc_errors_tag 2 |
|
||||||
#define grpc_lb_v0_ClientStats_dropped_requests_tag 3 |
|
||||||
#define grpc_lb_v0_Duration_seconds_tag 1 |
|
||||||
#define grpc_lb_v0_Duration_nanos_tag 2 |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceRequest_name_tag 1 |
|
||||||
#define grpc_lb_v0_Server_ip_address_tag 1 |
|
||||||
#define grpc_lb_v0_Server_port_tag 2 |
|
||||||
#define grpc_lb_v0_Server_load_balance_token_tag 3 |
|
||||||
#define grpc_lb_v0_Server_drop_request_tag 4 |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceResponse_client_config_tag 1 |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceResponse_load_balancer_delegate_tag 2 |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceResponse_client_stats_report_interval_tag 3 |
|
||||||
#define grpc_lb_v0_LoadBalanceRequest_initial_request_tag 1 |
|
||||||
#define grpc_lb_v0_LoadBalanceRequest_client_stats_tag 2 |
|
||||||
#define grpc_lb_v0_ServerList_servers_tag 1 |
|
||||||
#define grpc_lb_v0_ServerList_expiration_interval_tag 3 |
|
||||||
#define grpc_lb_v0_LoadBalanceResponse_initial_response_tag 1 |
|
||||||
#define grpc_lb_v0_LoadBalanceResponse_server_list_tag 2 |
|
||||||
|
|
||||||
/* Struct field encoding specification for nanopb */ |
|
||||||
extern const pb_field_t grpc_lb_v0_Duration_fields[3]; |
|
||||||
extern const pb_field_t grpc_lb_v0_LoadBalanceRequest_fields[3]; |
|
||||||
extern const pb_field_t grpc_lb_v0_InitialLoadBalanceRequest_fields[2]; |
|
||||||
extern const pb_field_t grpc_lb_v0_ClientStats_fields[4]; |
|
||||||
extern const pb_field_t grpc_lb_v0_LoadBalanceResponse_fields[3]; |
|
||||||
extern const pb_field_t grpc_lb_v0_InitialLoadBalanceResponse_fields[4]; |
|
||||||
extern const pb_field_t grpc_lb_v0_ServerList_fields[3]; |
|
||||||
extern const pb_field_t grpc_lb_v0_Server_fields[5]; |
|
||||||
|
|
||||||
/* Maximum encoded size of messages (where known) */ |
|
||||||
#define grpc_lb_v0_Duration_size 22 |
|
||||||
#define grpc_lb_v0_LoadBalanceRequest_size 169 |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceRequest_size 131 |
|
||||||
#define grpc_lb_v0_ClientStats_size 33 |
|
||||||
#define grpc_lb_v0_LoadBalanceResponse_size (165 + grpc_lb_v0_ServerList_size) |
|
||||||
#define grpc_lb_v0_InitialLoadBalanceResponse_size 156 |
|
||||||
#define grpc_lb_v0_Server_size 127 |
|
||||||
|
|
||||||
/* Message IDs (where set with "msgid" option) */ |
|
||||||
#ifdef PB_MSGID |
|
||||||
|
|
||||||
#define LOAD_BALANCER_MESSAGES \ |
|
||||||
|
|
||||||
|
|
||||||
#endif |
|
||||||
|
|
||||||
#ifdef __cplusplus |
|
||||||
} /* extern "C" */ |
|
||||||
#endif |
|
||||||
|
|
||||||
#endif |
|
@ -0,0 +1,178 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2016, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
/* Automatically generated nanopb header */ |
||||||
|
/* Generated by nanopb-0.3.5-dev */ |
||||||
|
|
||||||
|
#ifndef PB_LOAD_BALANCER_PB_H_INCLUDED |
||||||
|
#define PB_LOAD_BALANCER_PB_H_INCLUDED |
||||||
|
#include "third_party/nanopb/pb.h" |
||||||
|
#if PB_PROTO_HEADER_VERSION != 30 |
||||||
|
#error Regenerate this file with the current version of nanopb generator. |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
extern "C" { |
||||||
|
#endif |
||||||
|
|
||||||
|
/* Struct definitions */ |
||||||
|
typedef struct _grpc_lb_v1_ClientStats { |
||||||
|
bool has_total_requests; |
||||||
|
int64_t total_requests; |
||||||
|
bool has_client_rpc_errors; |
||||||
|
int64_t client_rpc_errors; |
||||||
|
bool has_dropped_requests; |
||||||
|
int64_t dropped_requests; |
||||||
|
} grpc_lb_v1_ClientStats; |
||||||
|
|
||||||
|
typedef struct _grpc_lb_v1_Duration { |
||||||
|
bool has_seconds; |
||||||
|
int64_t seconds; |
||||||
|
bool has_nanos; |
||||||
|
int32_t nanos; |
||||||
|
} grpc_lb_v1_Duration; |
||||||
|
|
||||||
|
typedef struct _grpc_lb_v1_InitialLoadBalanceRequest { |
||||||
|
bool has_name; |
||||||
|
char name[128]; |
||||||
|
} grpc_lb_v1_InitialLoadBalanceRequest; |
||||||
|
|
||||||
|
typedef struct _grpc_lb_v1_Server { |
||||||
|
bool has_ip_address; |
||||||
|
char ip_address[46]; |
||||||
|
bool has_port; |
||||||
|
int32_t port; |
||||||
|
bool has_load_balance_token; |
||||||
|
char load_balance_token[64]; |
||||||
|
bool has_drop_request; |
||||||
|
bool drop_request; |
||||||
|
} grpc_lb_v1_Server; |
||||||
|
|
||||||
|
typedef struct _grpc_lb_v1_InitialLoadBalanceResponse { |
||||||
|
bool has_load_balancer_delegate; |
||||||
|
char load_balancer_delegate[64]; |
||||||
|
bool has_client_stats_report_interval; |
||||||
|
grpc_lb_v1_Duration client_stats_report_interval; |
||||||
|
} grpc_lb_v1_InitialLoadBalanceResponse; |
||||||
|
|
||||||
|
typedef struct _grpc_lb_v1_LoadBalanceRequest { |
||||||
|
bool has_initial_request; |
||||||
|
grpc_lb_v1_InitialLoadBalanceRequest initial_request; |
||||||
|
bool has_client_stats; |
||||||
|
grpc_lb_v1_ClientStats client_stats; |
||||||
|
} grpc_lb_v1_LoadBalanceRequest; |
||||||
|
|
||||||
|
typedef struct _grpc_lb_v1_ServerList { |
||||||
|
pb_callback_t servers; |
||||||
|
bool has_expiration_interval; |
||||||
|
grpc_lb_v1_Duration expiration_interval; |
||||||
|
} grpc_lb_v1_ServerList; |
||||||
|
|
||||||
|
typedef struct _grpc_lb_v1_LoadBalanceResponse { |
||||||
|
bool has_initial_response; |
||||||
|
grpc_lb_v1_InitialLoadBalanceResponse initial_response; |
||||||
|
bool has_server_list; |
||||||
|
grpc_lb_v1_ServerList server_list; |
||||||
|
} grpc_lb_v1_LoadBalanceResponse; |
||||||
|
|
||||||
|
/* Default values for struct fields */ |
||||||
|
|
||||||
|
/* Initializer values for message structs */ |
||||||
|
#define grpc_lb_v1_Duration_init_default {false, 0, false, 0} |
||||||
|
#define grpc_lb_v1_LoadBalanceRequest_init_default {false, grpc_lb_v1_InitialLoadBalanceRequest_init_default, false, grpc_lb_v1_ClientStats_init_default} |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceRequest_init_default {false, ""} |
||||||
|
#define grpc_lb_v1_ClientStats_init_default {false, 0, false, 0, false, 0} |
||||||
|
#define grpc_lb_v1_LoadBalanceResponse_init_default {false, grpc_lb_v1_InitialLoadBalanceResponse_init_default, false, grpc_lb_v1_ServerList_init_default} |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceResponse_init_default {false, "", false, grpc_lb_v1_Duration_init_default} |
||||||
|
#define grpc_lb_v1_ServerList_init_default {{{NULL}, NULL}, false, grpc_lb_v1_Duration_init_default} |
||||||
|
#define grpc_lb_v1_Server_init_default {false, "", false, 0, false, "", false, 0} |
||||||
|
#define grpc_lb_v1_Duration_init_zero {false, 0, false, 0} |
||||||
|
#define grpc_lb_v1_LoadBalanceRequest_init_zero {false, grpc_lb_v1_InitialLoadBalanceRequest_init_zero, false, grpc_lb_v1_ClientStats_init_zero} |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceRequest_init_zero {false, ""} |
||||||
|
#define grpc_lb_v1_ClientStats_init_zero {false, 0, false, 0, false, 0} |
||||||
|
#define grpc_lb_v1_LoadBalanceResponse_init_zero {false, grpc_lb_v1_InitialLoadBalanceResponse_init_zero, false, grpc_lb_v1_ServerList_init_zero} |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceResponse_init_zero {false, "", false, grpc_lb_v1_Duration_init_zero} |
||||||
|
#define grpc_lb_v1_ServerList_init_zero {{{NULL}, NULL}, false, grpc_lb_v1_Duration_init_zero} |
||||||
|
#define grpc_lb_v1_Server_init_zero {false, "", false, 0, false, "", false, 0} |
||||||
|
|
||||||
|
/* Field tags (for use in manual encoding/decoding) */ |
||||||
|
#define grpc_lb_v1_ClientStats_total_requests_tag 1 |
||||||
|
#define grpc_lb_v1_ClientStats_client_rpc_errors_tag 2 |
||||||
|
#define grpc_lb_v1_ClientStats_dropped_requests_tag 3 |
||||||
|
#define grpc_lb_v1_Duration_seconds_tag 1 |
||||||
|
#define grpc_lb_v1_Duration_nanos_tag 2 |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceRequest_name_tag 1 |
||||||
|
#define grpc_lb_v1_Server_ip_address_tag 1 |
||||||
|
#define grpc_lb_v1_Server_port_tag 2 |
||||||
|
#define grpc_lb_v1_Server_load_balance_token_tag 3 |
||||||
|
#define grpc_lb_v1_Server_drop_request_tag 4 |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceResponse_load_balancer_delegate_tag 2 |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceResponse_client_stats_report_interval_tag 3 |
||||||
|
#define grpc_lb_v1_LoadBalanceRequest_initial_request_tag 1 |
||||||
|
#define grpc_lb_v1_LoadBalanceRequest_client_stats_tag 2 |
||||||
|
#define grpc_lb_v1_ServerList_servers_tag 1 |
||||||
|
#define grpc_lb_v1_ServerList_expiration_interval_tag 3 |
||||||
|
#define grpc_lb_v1_LoadBalanceResponse_initial_response_tag 1 |
||||||
|
#define grpc_lb_v1_LoadBalanceResponse_server_list_tag 2 |
||||||
|
|
||||||
|
/* Struct field encoding specification for nanopb */ |
||||||
|
extern const pb_field_t grpc_lb_v1_Duration_fields[3]; |
||||||
|
extern const pb_field_t grpc_lb_v1_LoadBalanceRequest_fields[3]; |
||||||
|
extern const pb_field_t grpc_lb_v1_InitialLoadBalanceRequest_fields[2]; |
||||||
|
extern const pb_field_t grpc_lb_v1_ClientStats_fields[4]; |
||||||
|
extern const pb_field_t grpc_lb_v1_LoadBalanceResponse_fields[3]; |
||||||
|
extern const pb_field_t grpc_lb_v1_InitialLoadBalanceResponse_fields[3]; |
||||||
|
extern const pb_field_t grpc_lb_v1_ServerList_fields[3]; |
||||||
|
extern const pb_field_t grpc_lb_v1_Server_fields[5]; |
||||||
|
|
||||||
|
/* Maximum encoded size of messages (where known) */ |
||||||
|
#define grpc_lb_v1_Duration_size 22 |
||||||
|
#define grpc_lb_v1_LoadBalanceRequest_size 169 |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceRequest_size 131 |
||||||
|
#define grpc_lb_v1_ClientStats_size 33 |
||||||
|
#define grpc_lb_v1_LoadBalanceResponse_size (98 + grpc_lb_v1_ServerList_size) |
||||||
|
#define grpc_lb_v1_InitialLoadBalanceResponse_size 90 |
||||||
|
#define grpc_lb_v1_Server_size 127 |
||||||
|
|
||||||
|
/* Message IDs (where set with "msgid" option) */ |
||||||
|
#ifdef PB_MSGID |
||||||
|
|
||||||
|
#define LOAD_BALANCER_MESSAGES \ |
||||||
|
|
||||||
|
|
||||||
|
#endif |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
} /* extern "C" */ |
||||||
|
#endif |
||||||
|
|
||||||
|
#endif |
@ -0,0 +1,191 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// Copyright 2015, Google Inc. |
||||||
|
// All rights reserved. |
||||||
|
// |
||||||
|
// Redistribution and use in source and binary forms, with or without |
||||||
|
// modification, are permitted provided that the following conditions are |
||||||
|
// met: |
||||||
|
// |
||||||
|
// * Redistributions of source code must retain the above copyright |
||||||
|
// notice, this list of conditions and the following disclaimer. |
||||||
|
// * Redistributions in binary form must reproduce the above |
||||||
|
// copyright notice, this list of conditions and the following disclaimer |
||||||
|
// in the documentation and/or other materials provided with the |
||||||
|
// distribution. |
||||||
|
// * Neither the name of Google Inc. nor the names of its |
||||||
|
// contributors may be used to endorse or promote products derived from |
||||||
|
// this software without specific prior written permission. |
||||||
|
// |
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using Grpc.Core.Internal; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace Grpc.Core.Internal.Tests |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Uses fake native call to test interaction of <c>AsyncCallServer</c> wrapping code with C core in different situations. |
||||||
|
/// </summary> |
||||||
|
public class AsyncCallServerTest |
||||||
|
{ |
||||||
|
Server server; |
||||||
|
FakeNativeCall fakeCall; |
||||||
|
AsyncCallServer<string, string> asyncCallServer; |
||||||
|
|
||||||
|
[SetUp] |
||||||
|
public void Init() |
||||||
|
{ |
||||||
|
var environment = GrpcEnvironment.AddRef(); |
||||||
|
|
||||||
|
// Create a fake server just so we have an instance to refer to. |
||||||
|
// The server won't actually be used at all. |
||||||
|
server = new Server() |
||||||
|
{ |
||||||
|
Ports = { { "localhost", 0, ServerCredentials.Insecure } } |
||||||
|
}; |
||||||
|
server.Start(); |
||||||
|
|
||||||
|
fakeCall = new FakeNativeCall(); |
||||||
|
asyncCallServer = new AsyncCallServer<string, string>( |
||||||
|
Marshallers.StringMarshaller.Serializer, Marshallers.StringMarshaller.Deserializer, |
||||||
|
environment, |
||||||
|
server); |
||||||
|
asyncCallServer.InitializeForTesting(fakeCall); |
||||||
|
} |
||||||
|
|
||||||
|
[TearDown] |
||||||
|
public void Cleanup() |
||||||
|
{ |
||||||
|
server.ShutdownAsync().Wait(); |
||||||
|
GrpcEnvironment.Release(); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void CancelNotificationAfterStartDisposes() |
||||||
|
{ |
||||||
|
var finishedTask = asyncCallServer.ServerSideCallAsync(); |
||||||
|
fakeCall.ReceivedCloseOnServerHandler(true, cancelled: true); |
||||||
|
AssertFinished(asyncCallServer, fakeCall, finishedTask); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void CancelNotificationAfterStartDisposesAfterPendingReadFinishes() |
||||||
|
{ |
||||||
|
var finishedTask = asyncCallServer.ServerSideCallAsync(); |
||||||
|
var requestStream = new ServerRequestStream<string, string>(asyncCallServer); |
||||||
|
|
||||||
|
var moveNextTask = requestStream.MoveNext(); |
||||||
|
|
||||||
|
fakeCall.ReceivedCloseOnServerHandler(true, cancelled: true); |
||||||
|
fakeCall.ReceivedMessageHandler(true, null); |
||||||
|
Assert.IsFalse(moveNextTask.Result); |
||||||
|
|
||||||
|
AssertFinished(asyncCallServer, fakeCall, finishedTask); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ReadAfterCancelNotificationCanSucceed() |
||||||
|
{ |
||||||
|
var finishedTask = asyncCallServer.ServerSideCallAsync(); |
||||||
|
var requestStream = new ServerRequestStream<string, string>(asyncCallServer); |
||||||
|
|
||||||
|
fakeCall.ReceivedCloseOnServerHandler(true, cancelled: true); |
||||||
|
|
||||||
|
// Check that starting a read after cancel notification has been processed is legal. |
||||||
|
var moveNextTask = requestStream.MoveNext(); |
||||||
|
Assert.IsFalse(moveNextTask.Result); |
||||||
|
|
||||||
|
AssertFinished(asyncCallServer, fakeCall, finishedTask); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ReadCompletionFailureClosesRequestStream() |
||||||
|
{ |
||||||
|
var finishedTask = asyncCallServer.ServerSideCallAsync(); |
||||||
|
var requestStream = new ServerRequestStream<string, string>(asyncCallServer); |
||||||
|
|
||||||
|
// if a read completion's success==false, the request stream will silently finish |
||||||
|
// and we rely on C core cancelling the call. |
||||||
|
var moveNextTask = requestStream.MoveNext(); |
||||||
|
fakeCall.ReceivedMessageHandler(false, null); |
||||||
|
Assert.IsFalse(moveNextTask.Result); |
||||||
|
|
||||||
|
fakeCall.ReceivedCloseOnServerHandler(true, cancelled: true); |
||||||
|
AssertFinished(asyncCallServer, fakeCall, finishedTask); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void WriteAfterCancelNotificationFails() |
||||||
|
{ |
||||||
|
var finishedTask = asyncCallServer.ServerSideCallAsync(); |
||||||
|
var requestStream = new ServerRequestStream<string, string>(asyncCallServer); |
||||||
|
var responseStream = new ServerResponseStream<string, string>(asyncCallServer); |
||||||
|
|
||||||
|
fakeCall.ReceivedCloseOnServerHandler(true, cancelled: true); |
||||||
|
|
||||||
|
// TODO(jtattermusch): should we throw a different exception type instead? |
||||||
|
Assert.Throws(typeof(InvalidOperationException), () => responseStream.WriteAsync("request1")); |
||||||
|
AssertFinished(asyncCallServer, fakeCall, finishedTask); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void WriteCompletionFailureThrows() |
||||||
|
{ |
||||||
|
var finishedTask = asyncCallServer.ServerSideCallAsync(); |
||||||
|
var responseStream = new ServerResponseStream<string, string>(asyncCallServer); |
||||||
|
|
||||||
|
var writeTask = responseStream.WriteAsync("request1"); |
||||||
|
fakeCall.SendCompletionHandler(false); |
||||||
|
// TODO(jtattermusch): should we throw a different exception type instead? |
||||||
|
Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await writeTask); |
||||||
|
|
||||||
|
fakeCall.ReceivedCloseOnServerHandler(true, cancelled: true); |
||||||
|
AssertFinished(asyncCallServer, fakeCall, finishedTask); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void WriteAndWriteStatusCanRunConcurrently() |
||||||
|
{ |
||||||
|
var finishedTask = asyncCallServer.ServerSideCallAsync(); |
||||||
|
var responseStream = new ServerResponseStream<string, string>(asyncCallServer); |
||||||
|
|
||||||
|
var writeTask = responseStream.WriteAsync("request1"); |
||||||
|
var writeStatusTask = asyncCallServer.SendStatusFromServerAsync(Status.DefaultSuccess, new Metadata(), null); |
||||||
|
|
||||||
|
fakeCall.SendCompletionHandler(true); |
||||||
|
fakeCall.SendStatusFromServerHandler(true); |
||||||
|
|
||||||
|
Assert.DoesNotThrowAsync(async () => await writeTask); |
||||||
|
Assert.DoesNotThrowAsync(async () => await writeStatusTask); |
||||||
|
|
||||||
|
fakeCall.ReceivedCloseOnServerHandler(true, cancelled: true); |
||||||
|
|
||||||
|
AssertFinished(asyncCallServer, fakeCall, finishedTask); |
||||||
|
} |
||||||
|
|
||||||
|
static void AssertFinished(AsyncCallServer<string, string> asyncCallServer, FakeNativeCall fakeCall, Task finishedTask) |
||||||
|
{ |
||||||
|
Assert.IsTrue(fakeCall.IsDisposed); |
||||||
|
Assert.IsTrue(finishedTask.IsCompleted); |
||||||
|
Assert.DoesNotThrow(() => finishedTask.Wait()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,184 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// Copyright 2015, Google Inc. |
||||||
|
// All rights reserved. |
||||||
|
// |
||||||
|
// Redistribution and use in source and binary forms, with or without |
||||||
|
// modification, are permitted provided that the following conditions are |
||||||
|
// met: |
||||||
|
// |
||||||
|
// * Redistributions of source code must retain the above copyright |
||||||
|
// notice, this list of conditions and the following disclaimer. |
||||||
|
// * Redistributions in binary form must reproduce the above |
||||||
|
// copyright notice, this list of conditions and the following disclaimer |
||||||
|
// in the documentation and/or other materials provided with the |
||||||
|
// distribution. |
||||||
|
// * Neither the name of Google Inc. nor the names of its |
||||||
|
// contributors may be used to endorse or promote products derived from |
||||||
|
// this software without specific prior written permission. |
||||||
|
// |
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using Grpc.Core.Internal; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace Grpc.Core.Internal.Tests |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// For testing purposes. |
||||||
|
/// </summary> |
||||||
|
internal class FakeNativeCall : INativeCall |
||||||
|
{ |
||||||
|
public UnaryResponseClientHandler UnaryResponseClientHandler |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public ReceivedStatusOnClientHandler ReceivedStatusOnClientHandler |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public ReceivedMessageHandler ReceivedMessageHandler |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public ReceivedResponseHeadersHandler ReceivedResponseHeadersHandler |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public SendCompletionHandler SendCompletionHandler |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public SendCompletionHandler SendStatusFromServerHandler |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public ReceivedCloseOnServerHandler ReceivedCloseOnServerHandler |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public bool IsCancelled |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public bool IsDisposed |
||||||
|
{ |
||||||
|
get; |
||||||
|
set; |
||||||
|
} |
||||||
|
|
||||||
|
public void Cancel() |
||||||
|
{ |
||||||
|
IsCancelled = true; |
||||||
|
} |
||||||
|
|
||||||
|
public void CancelWithStatus(Status status) |
||||||
|
{ |
||||||
|
IsCancelled = true; |
||||||
|
} |
||||||
|
|
||||||
|
public string GetPeer() |
||||||
|
{ |
||||||
|
return "PEER"; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartUnary(UnaryResponseClientHandler callback, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags) |
||||||
|
{ |
||||||
|
UnaryResponseClientHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartUnary(BatchContextSafeHandle ctx, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
|
||||||
|
public void StartClientStreaming(UnaryResponseClientHandler callback, MetadataArraySafeHandle metadataArray) |
||||||
|
{ |
||||||
|
UnaryResponseClientHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartServerStreaming(ReceivedStatusOnClientHandler callback, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags) |
||||||
|
{ |
||||||
|
ReceivedStatusOnClientHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartDuplexStreaming(ReceivedStatusOnClientHandler callback, MetadataArraySafeHandle metadataArray) |
||||||
|
{ |
||||||
|
ReceivedStatusOnClientHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartReceiveMessage(ReceivedMessageHandler callback) |
||||||
|
{ |
||||||
|
ReceivedMessageHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartReceiveInitialMetadata(ReceivedResponseHeadersHandler callback) |
||||||
|
{ |
||||||
|
ReceivedResponseHeadersHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartSendInitialMetadata(SendCompletionHandler callback, MetadataArraySafeHandle metadataArray) |
||||||
|
{ |
||||||
|
SendCompletionHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartSendMessage(SendCompletionHandler callback, byte[] payload, WriteFlags writeFlags, bool sendEmptyInitialMetadata) |
||||||
|
{ |
||||||
|
SendCompletionHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartSendCloseFromClient(SendCompletionHandler callback) |
||||||
|
{ |
||||||
|
SendCompletionHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartSendStatusFromServer(SendCompletionHandler callback, Status status, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadata, |
||||||
|
byte[] optionalPayload, WriteFlags writeFlags) |
||||||
|
{ |
||||||
|
SendStatusFromServerHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void StartServerSide(ReceivedCloseOnServerHandler callback) |
||||||
|
{ |
||||||
|
ReceivedCloseOnServerHandler = callback; |
||||||
|
} |
||||||
|
|
||||||
|
public void Dispose() |
||||||
|
{ |
||||||
|
IsDisposed = true; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
<?php |
||||||
|
/* |
||||||
|
* |
||||||
|
* Copyright 2016, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
$args = getopt('', ['metrics_server_address:', 'total_only::']); |
||||||
|
$parts = explode(':', $args['metrics_server_address']); |
||||||
|
$server_host = $parts[0]; |
||||||
|
$server_port = (count($parts) == 2) ? $parts[1] : ''; |
||||||
|
|
||||||
|
$socket = socket_create(AF_INET, SOCK_STREAM, 0); |
||||||
|
if (@!socket_connect($socket, $server_host, $server_port)) { |
||||||
|
echo "Cannot connect to merics server...\n"; |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
socket_write($socket, 'qps'); |
||||||
|
while ($out = socket_read($socket, 1024)) { |
||||||
|
echo "$out\n"; |
||||||
|
} |
||||||
|
socket_close($socket); |
@ -0,0 +1,116 @@ |
|||||||
|
<?php |
||||||
|
/* |
||||||
|
* |
||||||
|
* Copyright 2016, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
include_once('interop_client.php'); |
||||||
|
|
||||||
|
function stress_main($args) { |
||||||
|
mt_srand(); |
||||||
|
set_time_limit(0); |
||||||
|
|
||||||
|
// open socket to listen as metrics server |
||||||
|
$socket = socket_create(AF_INET, SOCK_STREAM, 0); |
||||||
|
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); |
||||||
|
if (@!socket_bind($socket, 'localhost', $args['metrics_port'])) { |
||||||
|
echo "Cannot create socket for metrics server...\n"; |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
socket_listen($socket); |
||||||
|
socket_set_nonblock($socket); |
||||||
|
|
||||||
|
$start_time = microtime(true); |
||||||
|
$count = 0; |
||||||
|
$deadline = $args['test_duration_secs'] ? |
||||||
|
($start_time + $args['test_duration_secs']) : false; |
||||||
|
$num_test_cases = count($args['test_cases']); |
||||||
|
$stub = false; |
||||||
|
|
||||||
|
while (true) { |
||||||
|
$current_time = microtime(true); |
||||||
|
if ($deadline && $current_time > $deadline) { |
||||||
|
break; |
||||||
|
} |
||||||
|
if ($client_connection = socket_accept($socket)) { |
||||||
|
// there is an incoming request, respond with qps metrics |
||||||
|
$input = socket_read($client_connection, 1024); |
||||||
|
$qps = round($count / ($current_time - $start_time)); |
||||||
|
socket_write($client_connection, "qps: $qps"); |
||||||
|
socket_close($client_connection); |
||||||
|
} else { |
||||||
|
// do actual work, run one interop test case |
||||||
|
$args['test_case'] = |
||||||
|
$args['test_cases'][mt_rand(0, $num_test_cases - 1)]; |
||||||
|
$stub = @interop_main($args, $stub); |
||||||
|
$count++; |
||||||
|
} |
||||||
|
} |
||||||
|
socket_close($socket); |
||||||
|
echo "Number of interop tests run in $args[test_duration_secs] seconds: $count.\n"; |
||||||
|
} |
||||||
|
|
||||||
|
// process command line arguments |
||||||
|
$raw_args = getopt('', |
||||||
|
['server_addresses::', |
||||||
|
'test_cases:', |
||||||
|
'metrics_port::', |
||||||
|
'test_duration_secs::', |
||||||
|
'num_channels_per_server::', |
||||||
|
'num_stubs_per_channel::']); |
||||||
|
|
||||||
|
$args = []; |
||||||
|
|
||||||
|
if (empty($raw_args['server_addresses'])) { |
||||||
|
$args['server_host'] = 'localhost'; |
||||||
|
$args['server_port'] = '8080'; |
||||||
|
} else { |
||||||
|
$parts = explode(':', $raw_args['server_addresses']); |
||||||
|
$args['server_host'] = $parts[0]; |
||||||
|
$args['server_port'] = (count($parts) == 2) ? $parts[1] : ''; |
||||||
|
} |
||||||
|
|
||||||
|
$args['metrics_port'] = empty($raw_args['metrics_port']) ? |
||||||
|
'8081' : $args['metrics_port']; |
||||||
|
|
||||||
|
$args['test_duration_secs'] = empty($raw_args['test_duration_secs']) || |
||||||
|
$raw_args['test_duration_secs'] == -1 ? |
||||||
|
false : $raw_args['test_duration_secs']; |
||||||
|
|
||||||
|
$test_cases = []; |
||||||
|
$test_case_strs = explode(',', $raw_args['test_cases']); |
||||||
|
foreach ($test_case_strs as $test_case_str) { |
||||||
|
$parts = explode(':', $test_case_str); |
||||||
|
$test_cases = array_merge($test_cases, array_fill(0, $parts[1], $parts[0])); |
||||||
|
} |
||||||
|
$args['test_cases'] = $test_cases; |
||||||
|
|
||||||
|
stress_main($args); |
@ -1,6 +0,0 @@ |
|||||||
grpc.lb.v0.InitialLoadBalanceRequest.name max_size:128 |
|
||||||
grpc.lb.v0.InitialLoadBalanceResponse.client_config max_size:64 |
|
||||||
grpc.lb.v0.InitialLoadBalanceResponse.load_balancer_delegate max_size:64 |
|
||||||
grpc.lb.v0.Server.ip_address max_size:46 |
|
||||||
grpc.lb.v0.Server.load_balance_token max_size:64 |
|
||||||
load_balancer.proto no_unions:true |
|
@ -0,0 +1,6 @@ |
|||||||
|
grpc.lb.v1.InitialLoadBalanceRequest.name max_size:128 |
||||||
|
grpc.lb.v1.InitialLoadBalanceResponse.client_config max_size:64 |
||||||
|
grpc.lb.v1.InitialLoadBalanceResponse.load_balancer_delegate max_size:64 |
||||||
|
grpc.lb.v1.Server.ip_address max_size:46 |
||||||
|
grpc.lb.v1.Server.load_balance_token max_size:64 |
||||||
|
load_balancer.proto no_unions:true |
@ -1,114 +0,0 @@ |
|||||||
# Copyright 2015, Google Inc. |
|
||||||
# All rights reserved. |
|
||||||
# |
|
||||||
# Redistribution and use in source and binary forms, with or without |
|
||||||
# modification, are permitted provided that the following conditions are |
|
||||||
# met: |
|
||||||
# |
|
||||||
# * Redistributions of source code must retain the above copyright |
|
||||||
# notice, this list of conditions and the following disclaimer. |
|
||||||
# * Redistributions in binary form must reproduce the above |
|
||||||
# copyright notice, this list of conditions and the following disclaimer |
|
||||||
# in the documentation and/or other materials provided with the |
|
||||||
# distribution. |
|
||||||
# * Neither the name of Google Inc. nor the names of its |
|
||||||
# contributors may be used to endorse or promote products derived from |
|
||||||
# this software without specific prior written permission. |
|
||||||
# |
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
||||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
||||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
||||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
||||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
||||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
||||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
||||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
||||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
||||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
||||||
|
|
||||||
import os |
|
||||||
import platform |
|
||||||
import shutil |
|
||||||
import sys |
|
||||||
import sysconfig |
|
||||||
|
|
||||||
import setuptools |
|
||||||
|
|
||||||
import commands |
|
||||||
import grpc_version |
|
||||||
|
|
||||||
try: |
|
||||||
from urllib2 import urlopen |
|
||||||
except ImportError: |
|
||||||
from urllib.request import urlopen |
|
||||||
|
|
||||||
PYTHON_STEM = os.path.dirname(os.path.abspath(__file__)) |
|
||||||
BINARIES_REPOSITORY = os.environ.get( |
|
||||||
'GRPC_PYTHON_BINARIES_REPOSITORY', |
|
||||||
'https://storage.googleapis.com/grpc-precompiled-binaries/python') |
|
||||||
USE_PRECOMPILED_BINARIES = bool(int(os.environ.get( |
|
||||||
'GRPC_PYTHON_USE_PRECOMPILED_BINARIES', '1'))) |
|
||||||
|
|
||||||
def _tagged_ext_name(base): |
|
||||||
uname = platform.uname() |
|
||||||
tags = ( |
|
||||||
grpc_version.VERSION, |
|
||||||
'py{}'.format(sysconfig.get_python_version()), |
|
||||||
uname[0], |
|
||||||
uname[4], |
|
||||||
) |
|
||||||
ucs = 'ucs{}'.format(sysconfig.get_config_var('Py_UNICODE_SIZE')) |
|
||||||
return '{base}-{tags}-{ucs}'.format( |
|
||||||
base=base, tags='-'.join(tags), ucs=ucs) |
|
||||||
|
|
||||||
|
|
||||||
class BuildTaggedExt(setuptools.Command): |
|
||||||
|
|
||||||
description = 'build the gRPC tagged extensions' |
|
||||||
user_options = [] |
|
||||||
|
|
||||||
def initialize_options(self): |
|
||||||
# distutils requires this override. |
|
||||||
pass |
|
||||||
|
|
||||||
def finalize_options(self): |
|
||||||
# distutils requires this override. |
|
||||||
pass |
|
||||||
|
|
||||||
def run(self): |
|
||||||
if 'linux' in sys.platform: |
|
||||||
self.run_command('build_ext') |
|
||||||
try: |
|
||||||
os.makedirs('dist/') |
|
||||||
except OSError: |
|
||||||
pass |
|
||||||
shutil.copyfile( |
|
||||||
os.path.join(PYTHON_STEM, 'grpc/_cython/cygrpc.so'), |
|
||||||
'dist/{}.so'.format(_tagged_ext_name('cygrpc'))) |
|
||||||
else: |
|
||||||
sys.stderr.write('nothing to do for build_tagged_ext\n') |
|
||||||
|
|
||||||
|
|
||||||
def update_setup_arguments(setup_arguments): |
|
||||||
if not USE_PRECOMPILED_BINARIES: |
|
||||||
sys.stderr.write('not using precompiled extension') |
|
||||||
return |
|
||||||
url = '{}/{}.so'.format(BINARIES_REPOSITORY, _tagged_ext_name('cygrpc')) |
|
||||||
target_path = os.path.join(PYTHON_STEM, 'grpc/_cython/cygrpc.so') |
|
||||||
try: |
|
||||||
extension = urlopen(url).read() |
|
||||||
except: |
|
||||||
sys.stderr.write( |
|
||||||
'could not download precompiled extension: {}\n'.format(url)) |
|
||||||
return |
|
||||||
try: |
|
||||||
with open(target_path, 'w') as target: |
|
||||||
target.write(extension) |
|
||||||
setup_arguments['ext_modules'] = [] |
|
||||||
except: |
|
||||||
sys.stderr.write( |
|
||||||
'could not write precompiled extension to directory: {} -> {}\n' |
|
||||||
.format(url, target_path)) |
|
||||||
return |
|
||||||
setup_arguments['package_data']['grpc._cython'].append('cygrpc.so') |
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue