mirror of https://github.com/grpc/grpc.git
commit
5f399d173b
82 changed files with 1526 additions and 513 deletions
@ -1,5 +1,5 @@ |
||||
{ |
||||
"sdk": { |
||||
"version": "1.0.0-preview2-003121" |
||||
"version": "1.0.0-preview2-003131" |
||||
} |
||||
} |
@ -0,0 +1,181 @@ |
||||
/* |
||||
* |
||||
* Copyright 2017, 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. |
||||
* |
||||
*/ |
||||
|
||||
'use strict'; |
||||
|
||||
var _ = require('lodash'); |
||||
var client = require('./client'); |
||||
|
||||
/** |
||||
* Get a function that deserializes a specific type of protobuf. |
||||
* @param {function()} cls The constructor of the message type to deserialize |
||||
* @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings |
||||
* instead of Buffers. Defaults to false |
||||
* @param {bool=} longsAsStrings Deserialize long values as strings instead of |
||||
* objects. Defaults to true |
||||
* @return {function(Buffer):cls} The deserialization function |
||||
*/ |
||||
exports.deserializeCls = function deserializeCls(cls, binaryAsBase64, |
||||
longsAsStrings) { |
||||
/** |
||||
* Deserialize a buffer to a message object |
||||
* @param {Buffer} arg_buf The buffer to deserialize |
||||
* @return {cls} The resulting object |
||||
*/ |
||||
return function deserialize(arg_buf) { |
||||
// Convert to a native object with binary fields as Buffers (first argument)
|
||||
// and longs as strings (second argument)
|
||||
return cls.decode(arg_buf).toRaw(binaryAsBase64, longsAsStrings); |
||||
}; |
||||
}; |
||||
|
||||
var deserializeCls = exports.deserializeCls; |
||||
|
||||
/** |
||||
* Get a function that serializes objects to a buffer by protobuf class. |
||||
* @param {function()} Cls The constructor of the message type to serialize |
||||
* @return {function(Cls):Buffer} The serialization function |
||||
*/ |
||||
exports.serializeCls = function serializeCls(Cls) { |
||||
/** |
||||
* Serialize an object to a Buffer |
||||
* @param {Object} arg The object to serialize |
||||
* @return {Buffer} The serialized object |
||||
*/ |
||||
return function serialize(arg) { |
||||
return new Buffer(new Cls(arg).encode().toBuffer()); |
||||
}; |
||||
}; |
||||
|
||||
var serializeCls = exports.serializeCls; |
||||
|
||||
/** |
||||
* Get the fully qualified (dotted) name of a ProtoBuf.Reflect value. |
||||
* @param {ProtoBuf.Reflect.Namespace} value The value to get the name of |
||||
* @return {string} The fully qualified name of the value |
||||
*/ |
||||
exports.fullyQualifiedName = function fullyQualifiedName(value) { |
||||
if (value === null || value === undefined) { |
||||
return ''; |
||||
} |
||||
var name = value.name; |
||||
var parent_name = fullyQualifiedName(value.parent); |
||||
if (parent_name !== '') { |
||||
name = parent_name + '.' + name; |
||||
} |
||||
return name; |
||||
}; |
||||
|
||||
var fullyQualifiedName = exports.fullyQualifiedName; |
||||
|
||||
/** |
||||
* Return a map from method names to method attributes for the service. |
||||
* @param {ProtoBuf.Reflect.Service} service The service to get attributes for |
||||
* @param {Object=} options Options to apply to these attributes |
||||
* @return {Object} The attributes map |
||||
*/ |
||||
exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service, |
||||
options) { |
||||
var prefix = '/' + fullyQualifiedName(service) + '/'; |
||||
var binaryAsBase64, longsAsStrings; |
||||
if (options) { |
||||
binaryAsBase64 = options.binaryAsBase64; |
||||
longsAsStrings = options.longsAsStrings; |
||||
} |
||||
/* This slightly awkward construction is used to make sure we only use |
||||
lodash@3.10.1-compatible functions. A previous version used |
||||
_.fromPairs, which would be cleaner, but was introduced in lodash |
||||
version 4 */ |
||||
return _.zipObject(_.map(service.children, function(method) { |
||||
return _.camelCase(method.name); |
||||
}), _.map(service.children, function(method) { |
||||
return { |
||||
originalName: method.name, |
||||
path: prefix + method.name, |
||||
requestStream: method.requestStream, |
||||
responseStream: method.responseStream, |
||||
requestType: method.resolvedRequestType, |
||||
responseType: method.resolvedResponseType, |
||||
requestSerialize: serializeCls(method.resolvedRequestType.build()), |
||||
requestDeserialize: deserializeCls(method.resolvedRequestType.build(), |
||||
binaryAsBase64, longsAsStrings), |
||||
responseSerialize: serializeCls(method.resolvedResponseType.build()), |
||||
responseDeserialize: deserializeCls(method.resolvedResponseType.build(), |
||||
binaryAsBase64, longsAsStrings) |
||||
}; |
||||
})); |
||||
}; |
||||
|
||||
var getProtobufServiceAttrs = exports.getProtobufServiceAttrs; |
||||
|
||||
/** |
||||
* Load a gRPC object from an existing ProtoBuf.Reflect object. |
||||
* @param {ProtoBuf.Reflect.Namespace} value The ProtoBuf object to load. |
||||
* @param {Object=} options Options to apply to the loaded object |
||||
* @return {Object<string, *>} The resulting gRPC object |
||||
*/ |
||||
exports.loadObject = function loadObject(value, options) { |
||||
var result = {}; |
||||
if (!value) { |
||||
return value; |
||||
} |
||||
if (value.hasOwnProperty('ns')) { |
||||
return loadObject(value.ns, options); |
||||
} |
||||
if (value.className === 'Namespace') { |
||||
_.each(value.children, function(child) { |
||||
result[child.name] = loadObject(child, options); |
||||
}); |
||||
return result; |
||||
} else if (value.className === 'Service') { |
||||
return client.makeClientConstructor(getProtobufServiceAttrs(value, options), |
||||
options); |
||||
} else if (value.className === 'Message' || value.className === 'Enum') { |
||||
return value.build(); |
||||
} else { |
||||
return value; |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* The primary purpose of this method is to distinguish between reflection |
||||
* objects from different versions of ProtoBuf.js. This is just a heuristic, |
||||
* checking for properties that are (currently) specific to this version of |
||||
* ProtoBuf.js |
||||
* @param {Object} obj The object to check |
||||
* @return {boolean} Whether the object appears to be a Protobuf.js 5 |
||||
* ReflectionObject |
||||
*/ |
||||
exports.isProbablyProtobufJs5 = function isProbablyProtobufJs5(obj) { |
||||
return _.isArray(obj.children) && (typeof obj.build === 'function'); |
||||
}; |
@ -0,0 +1,170 @@ |
||||
/* |
||||
* |
||||
* Copyright 2017, 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. |
||||
* |
||||
*/ |
||||
|
||||
'use strict'; |
||||
|
||||
var _ = require('lodash'); |
||||
var client = require('./client'); |
||||
|
||||
/** |
||||
* Get a function that deserializes a specific type of protobuf. |
||||
* @param {function()} cls The constructor of the message type to deserialize |
||||
* @param {bool=} binaryAsBase64 Deserialize bytes fields as base64 strings |
||||
* instead of Buffers. Defaults to false |
||||
* @param {bool=} longsAsStrings Deserialize long values as strings instead of |
||||
* objects. Defaults to true |
||||
* @return {function(Buffer):cls} The deserialization function |
||||
*/ |
||||
exports.deserializeCls = function deserializeCls(cls, options) { |
||||
var conversion_options = { |
||||
defaults: true, |
||||
bytes: options.binaryAsBase64 ? String : Buffer, |
||||
longs: options.longsAsStrings ? String : null, |
||||
enums: options.enumsAsStrings ? String : null, |
||||
oneofs: true |
||||
}; |
||||
/** |
||||
* Deserialize a buffer to a message object |
||||
* @param {Buffer} arg_buf The buffer to deserialize |
||||
* @return {cls} The resulting object |
||||
*/ |
||||
return function deserialize(arg_buf) { |
||||
return cls.decode(arg_buf).toObject(conversion_options); |
||||
}; |
||||
}; |
||||
|
||||
var deserializeCls = exports.deserializeCls; |
||||
|
||||
/** |
||||
* Get a function that serializes objects to a buffer by protobuf class. |
||||
* @param {function()} Cls The constructor of the message type to serialize |
||||
* @return {function(Cls):Buffer} The serialization function |
||||
*/ |
||||
exports.serializeCls = function serializeCls(cls) { |
||||
/** |
||||
* Serialize an object to a Buffer |
||||
* @param {Object} arg The object to serialize |
||||
* @return {Buffer} The serialized object |
||||
*/ |
||||
return function serialize(arg) { |
||||
var message = cls.fromObject(arg); |
||||
return cls.encode(message).finish(); |
||||
}; |
||||
}; |
||||
|
||||
var serializeCls = exports.serializeCls; |
||||
|
||||
/** |
||||
* Get the fully qualified (dotted) name of a ProtoBuf.Reflect value. |
||||
* @param {ProtoBuf.ReflectionObject} value The value to get the name of |
||||
* @return {string} The fully qualified name of the value |
||||
*/ |
||||
exports.fullyQualifiedName = function fullyQualifiedName(value) { |
||||
if (value === null || value === undefined) { |
||||
return ''; |
||||
} |
||||
var name = value.name; |
||||
var parent_fqn = fullyQualifiedName(value.parent); |
||||
if (parent_fqn !== '') { |
||||
name = parent_fqn + '.' + name; |
||||
} |
||||
return name; |
||||
}; |
||||
|
||||
var fullyQualifiedName = exports.fullyQualifiedName; |
||||
|
||||
/** |
||||
* Return a map from method names to method attributes for the service. |
||||
* @param {ProtoBuf.Service} service The service to get attributes for |
||||
* @param {Object=} options Options to apply to these attributes |
||||
* @return {Object} The attributes map |
||||
*/ |
||||
exports.getProtobufServiceAttrs = function getProtobufServiceAttrs(service, |
||||
options) { |
||||
var prefix = '/' + fullyQualifiedName(service) + '/'; |
||||
service.resolveAll(); |
||||
return _.zipObject(_.map(service.methods, function(method) { |
||||
return _.camelCase(method.name); |
||||
}), _.map(service.methods, function(method) { |
||||
return { |
||||
originalName: method.name, |
||||
path: prefix + method.name, |
||||
requestStream: !!method.requestStream, |
||||
responseStream: !!method.responseStream, |
||||
requestType: method.resolvedRequestType, |
||||
responseType: method.resolvedResponseType, |
||||
requestSerialize: serializeCls(method.resolvedRequestType), |
||||
requestDeserialize: deserializeCls(method.resolvedRequestType, options), |
||||
responseSerialize: serializeCls(method.resolvedResponseType), |
||||
responseDeserialize: deserializeCls(method.resolvedResponseType, options) |
||||
}; |
||||
})); |
||||
}; |
||||
|
||||
var getProtobufServiceAttrs = exports.getProtobufServiceAttrs; |
||||
|
||||
exports.loadObject = function loadObject(value, options) { |
||||
var result = {}; |
||||
if (!value) { |
||||
return value; |
||||
} |
||||
if (value.hasOwnProperty('methods')) { |
||||
// It's a service object
|
||||
var service_attrs = getProtobufServiceAttrs(value, options); |
||||
return client.makeClientConstructor(service_attrs); |
||||
} |
||||
|
||||
if (value.hasOwnProperty('nested')) { |
||||
// It's a namespace or root object
|
||||
_.each(value.nested, function(nested, name) { |
||||
result[name] = loadObject(nested, options); |
||||
}); |
||||
return result; |
||||
} |
||||
|
||||
// Otherwise, it's not something we need to change
|
||||
return value; |
||||
}; |
||||
|
||||
/** |
||||
* The primary purpose of this method is to distinguish between reflection |
||||
* objects from different versions of ProtoBuf.js. This is just a heuristic, |
||||
* checking for properties that are (currently) specific to this version of |
||||
* ProtoBuf.js |
||||
* @param {Object} obj The object to check |
||||
* @return {boolean} Whether the object appears to be a Protobuf.js 6 |
||||
* ReflectionObject |
||||
*/ |
||||
exports.isProbablyProtobufJs6 = function isProbablyProtobufJs6(obj) { |
||||
return (typeof obj.root === 'object') && (typeof obj.resolve === 'function'); |
||||
}; |
@ -0,0 +1,58 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# 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. |
||||
|
||||
# Attempt to reproduce |
||||
# https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/1327 |
||||
|
||||
require_relative './end2end_common' |
||||
|
||||
def main |
||||
server_port = '' |
||||
OptionParser.new do |opts| |
||||
opts.on('--client_control_port=P', String) do |
||||
STDERR.puts 'client control port not used' |
||||
end |
||||
opts.on('--server_port=P', String) do |p| |
||||
server_port = p |
||||
end |
||||
end.parse! |
||||
|
||||
thd = Thread.new do |
||||
stub = Echo::EchoServer::Stub.new("localhost:#{server_port}", |
||||
:this_channel_is_insecure) |
||||
stub.echo(Echo::EchoRequest.new(request: 'hello')) |
||||
fail 'the clients rpc in this test shouldnt complete. ' \ |
||||
'expecting SIGINT to happen in the middle of the call' |
||||
end |
||||
thd.join |
||||
end |
||||
|
||||
main |
@ -0,0 +1,114 @@ |
||||
#!/usr/bin/env ruby |
||||
|
||||
# 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. |
||||
|
||||
require_relative './end2end_common' |
||||
|
||||
# Service that sleeps for a long time upon receiving an 'echo request' |
||||
# Also, this notifies @call_started_cv once it has received a request. |
||||
class SleepingEchoServerImpl < Echo::EchoServer::Service |
||||
def initialize(call_started, call_started_mu, call_started_cv) |
||||
@call_started = call_started |
||||
@call_started_mu = call_started_mu |
||||
@call_started_cv = call_started_cv |
||||
end |
||||
|
||||
def echo(echo_req, _) |
||||
@call_started_mu.synchronize do |
||||
@call_started.set_true |
||||
@call_started_cv.signal |
||||
end |
||||
sleep 1000 |
||||
Echo::EchoReply.new(response: echo_req.request) |
||||
end |
||||
end |
||||
|
||||
# Mutable boolean |
||||
class BoolHolder |
||||
attr_reader :val |
||||
|
||||
def init |
||||
@val = false |
||||
end |
||||
|
||||
def set_true |
||||
@val = true |
||||
end |
||||
end |
||||
|
||||
def main |
||||
STDERR.puts 'start server' |
||||
|
||||
call_started = BoolHolder.new |
||||
call_started_mu = Mutex.new |
||||
call_started_cv = ConditionVariable.new |
||||
|
||||
service_impl = SleepingEchoServerImpl.new(call_started, |
||||
call_started_mu, |
||||
call_started_cv) |
||||
server_runner = ServerRunner.new(service_impl) |
||||
server_port = server_runner.run |
||||
|
||||
STDERR.puts 'start client' |
||||
_, client_pid = start_client('killed_client_thread_client.rb', |
||||
server_port) |
||||
|
||||
call_started_mu.synchronize do |
||||
call_started_cv.wait(call_started_mu) until call_started.val |
||||
end |
||||
|
||||
# SIGINT the child process now that it's |
||||
# in the middle of an RPC (happening on a non-main thread) |
||||
Process.kill('SIGINT', client_pid) |
||||
STDERR.puts 'sent shutdown' |
||||
|
||||
begin |
||||
Timeout.timeout(10) do |
||||
Process.wait(client_pid) |
||||
end |
||||
rescue Timeout::Error |
||||
STDERR.puts "timeout wait for client pid #{client_pid}" |
||||
Process.kill('SIGKILL', client_pid) |
||||
Process.wait(client_pid) |
||||
STDERR.puts 'killed client child' |
||||
raise 'Timed out waiting for client process. ' \ |
||||
'It likely hangs when killed while in the middle of an rpc' |
||||
end |
||||
|
||||
client_exit_code = $CHILD_STATUS |
||||
if client_exit_code.termsig != 2 # SIGINT |
||||
fail 'expected client exit from SIGINT ' \ |
||||
"but got child status: #{client_exit_code}" |
||||
end |
||||
|
||||
server_runner.stop |
||||
end |
||||
|
||||
main |
@ -0,0 +1,142 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017, 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 <benchmark/benchmark.h> |
||||
#include <string.h> |
||||
#include <atomic> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include "test/cpp/microbenchmarks/helpers.h" |
||||
|
||||
extern "C" { |
||||
#include "src/core/lib/iomgr/ev_posix.h" |
||||
#include "src/core/lib/iomgr/port.h" |
||||
#include "src/core/lib/surface/completion_queue.h" |
||||
} |
||||
|
||||
struct grpc_pollset { |
||||
gpr_mu mu; |
||||
}; |
||||
|
||||
namespace grpc { |
||||
namespace testing { |
||||
|
||||
static void* make_tag(int i) { return (void*)(intptr_t)i; } |
||||
static grpc_completion_queue* g_cq; |
||||
static grpc_event_engine_vtable g_vtable; |
||||
|
||||
static __thread int g_thread_idx; |
||||
static __thread grpc_cq_completion g_cq_completion; |
||||
|
||||
static void pollset_shutdown(grpc_exec_ctx* exec_ctx, grpc_pollset* ps, |
||||
grpc_closure* closure) { |
||||
grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE); |
||||
} |
||||
|
||||
static void pollset_init(grpc_pollset* ps, gpr_mu** mu) { |
||||
gpr_mu_init(&ps->mu); |
||||
*mu = &ps->mu; |
||||
} |
||||
|
||||
static void pollset_destroy(grpc_pollset* ps) { gpr_mu_destroy(&ps->mu); } |
||||
|
||||
static grpc_error* pollset_kick(grpc_pollset* p, grpc_pollset_worker* worker) { |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
/* Callback when the tag is dequeued from the completion queue. Does nothing */ |
||||
static void cq_done_cb(grpc_exec_ctx* exec_ctx, void* done_arg, |
||||
grpc_cq_completion* cq_completion) {} |
||||
|
||||
/* Queues a completion tag. ZERO polling overhead */ |
||||
static grpc_error* pollset_work(grpc_exec_ctx* exec_ctx, grpc_pollset* ps, |
||||
grpc_pollset_worker** worker, gpr_timespec now, |
||||
gpr_timespec deadline) { |
||||
gpr_mu_unlock(&ps->mu); |
||||
grpc_cq_end_op(exec_ctx, g_cq, make_tag(g_thread_idx), GRPC_ERROR_NONE, |
||||
cq_done_cb, NULL, &g_cq_completion); |
||||
grpc_exec_ctx_flush(exec_ctx); |
||||
gpr_mu_lock(&ps->mu); |
||||
return GRPC_ERROR_NONE; |
||||
} |
||||
|
||||
static void init_engine_vtable() { |
||||
memset(&g_vtable, 0, sizeof(g_vtable)); |
||||
|
||||
g_vtable.pollset_size = sizeof(grpc_pollset); |
||||
g_vtable.pollset_init = pollset_init; |
||||
g_vtable.pollset_shutdown = pollset_shutdown; |
||||
g_vtable.pollset_destroy = pollset_destroy; |
||||
g_vtable.pollset_work = pollset_work; |
||||
g_vtable.pollset_kick = pollset_kick; |
||||
} |
||||
|
||||
static void setup() { |
||||
grpc_init(); |
||||
init_engine_vtable(); |
||||
grpc_set_event_engine_test_only(&g_vtable); |
||||
|
||||
g_cq = grpc_completion_queue_create(NULL); |
||||
} |
||||
|
||||
static void BM_Cq_Throughput(benchmark::State& state) { |
||||
TrackCounters track_counters; |
||||
gpr_timespec deadline = gpr_inf_past(GPR_CLOCK_MONOTONIC); |
||||
|
||||
if (state.thread_index == 0) { |
||||
setup(); |
||||
} |
||||
|
||||
while (state.KeepRunning()) { |
||||
g_thread_idx = state.thread_index; |
||||
void* dummy_tag = make_tag(g_thread_idx); |
||||
grpc_cq_begin_op(g_cq, dummy_tag); |
||||
grpc_completion_queue_next(g_cq, deadline, NULL); |
||||
} |
||||
|
||||
state.SetItemsProcessed(state.iterations()); |
||||
|
||||
if (state.thread_index == 0) { |
||||
grpc_completion_queue_shutdown(g_cq); |
||||
grpc_completion_queue_destroy(g_cq); |
||||
} |
||||
|
||||
track_counters.Finish(state); |
||||
} |
||||
|
||||
BENCHMARK(BM_Cq_Throughput)->ThreadRange(1, 16)->UseRealTime(); |
||||
|
||||
} // namespace testing
|
||||
} // namespace grpc
|
||||
|
||||
BENCHMARK_MAIN(); |
Loading…
Reference in new issue