mirror of https://github.com/grpc/grpc.git
commit
a7888f0f1a
81 changed files with 2143 additions and 573 deletions
@ -1,71 +1,105 @@ |
||||
"""Generates and compiles C++ grpc stubs from proto_library rules.""" |
||||
|
||||
load("//bazel:generate_cc.bzl", "generate_cc") |
||||
load("//bazel:protobuf.bzl", "well_known_proto_libs") |
||||
|
||||
def cc_grpc_library(name, srcs, deps, proto_only, well_known_protos, generate_mocks = False, use_external = False, **kwargs): |
||||
"""Generates C++ grpc classes from a .proto file. |
||||
def cc_grpc_library( |
||||
name, |
||||
srcs, |
||||
deps, |
||||
proto_only = False, |
||||
well_known_protos = False, |
||||
generate_mocks = False, |
||||
use_external = False, |
||||
grpc_only = False, |
||||
**kwargs): |
||||
"""Generates C++ grpc classes for services defined in a proto file. |
||||
|
||||
Assumes the generated classes will be used in cc_api_version = 2. |
||||
If grpc_only is True, this rule is compatible with proto_library and |
||||
cc_proto_library native rules such that it expects proto_library target |
||||
as srcs argument and generates only grpc library classes, expecting |
||||
protobuf messages classes library (cc_proto_library target) to be passed in |
||||
deps argument. By default grpc_only is False which makes this rule to behave |
||||
in a backwards-compatible mode (trying to generate both proto and grpc |
||||
classes). |
||||
|
||||
Arguments: |
||||
name: name of rule. |
||||
srcs: a single proto_library, which wraps the .proto files with services. |
||||
deps: a list of C++ proto_library (or cc_proto_library) which provides |
||||
the compiled code of any message that the services depend on. |
||||
well_known_protos: Should this library additionally depend on well known |
||||
protos |
||||
use_external: When True the grpc deps are prefixed with //external. This |
||||
allows grpc to be used as a dependency in other bazel projects. |
||||
generate_mocks: When True, Google Mock code for client stub is generated. |
||||
**kwargs: rest of arguments, e.g., compatible_with and visibility. |
||||
""" |
||||
if len(srcs) > 1: |
||||
fail("Only one srcs value supported", "srcs") |
||||
Assumes the generated classes will be used in cc_api_version = 2. |
||||
|
||||
proto_target = "_" + name + "_only" |
||||
codegen_target = "_" + name + "_codegen" |
||||
codegen_grpc_target = "_" + name + "_grpc_codegen" |
||||
proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(':') == -1] |
||||
proto_deps += [dep.split(':')[0] + ':' + "_" + dep.split(':')[1] + "_only" for dep in deps if dep.find(':') != -1] |
||||
Args: |
||||
name (str): Name of rule. |
||||
srcs (list): A single .proto file which contains services definitions, |
||||
or if grpc_only parameter is True, a single proto_library which |
||||
contains services descriptors. |
||||
deps (list): A list of C++ proto_library (or cc_proto_library) which |
||||
provides the compiled code of any message that the services depend on. |
||||
proto_only (bool): If True, create only C++ proto classes library, |
||||
avoid creating C++ grpc classes library (expect it in deps). |
||||
Deprecated, use native cc_proto_library instead. False by default. |
||||
well_known_protos (bool): Should this library additionally depend on |
||||
well known protos. Deprecated, the well known protos should be |
||||
specified as explicit dependencies of the proto_library target |
||||
(passed in srcs parameter) instead. False by default. |
||||
generate_mocks (bool): when True, Google Mock code for client stub is |
||||
generated. False by default. |
||||
use_external (bool): Not used. |
||||
grpc_only (bool): if True, generate only grpc library, expecting |
||||
protobuf messages library (cc_proto_library target) to be passed as |
||||
deps. False by default (will become True by default eventually). |
||||
**kwargs: rest of arguments, e.g., compatible_with and visibility |
||||
""" |
||||
if len(srcs) > 1: |
||||
fail("Only one srcs value supported", "srcs") |
||||
if grpc_only and proto_only: |
||||
fail("A mutualy exclusive configuration is specified: grpc_only = True and proto_only = True") |
||||
|
||||
native.proto_library( |
||||
name = proto_target, |
||||
srcs = srcs, |
||||
deps = proto_deps, |
||||
**kwargs |
||||
) |
||||
extra_deps = [] |
||||
proto_targets = [] |
||||
|
||||
generate_cc( |
||||
name = codegen_target, |
||||
srcs = [proto_target], |
||||
well_known_protos = well_known_protos, |
||||
**kwargs |
||||
) |
||||
if not grpc_only: |
||||
proto_target = "_" + name + "_only" |
||||
cc_proto_target = name if proto_only else "_" + name + "_cc_proto" |
||||
|
||||
if not proto_only: |
||||
plugin = "@com_github_grpc_grpc//:grpc_cpp_plugin" |
||||
generate_cc( |
||||
name = codegen_grpc_target, |
||||
srcs = [proto_target], |
||||
plugin = plugin, |
||||
well_known_protos = well_known_protos, |
||||
generate_mocks = generate_mocks, |
||||
**kwargs |
||||
) |
||||
grpc_deps = ["@com_github_grpc_grpc//:grpc++_codegen_proto", |
||||
"//external:protobuf"] |
||||
native.cc_library( |
||||
name = name, |
||||
srcs = [":" + codegen_grpc_target, ":" + codegen_target], |
||||
hdrs = [":" + codegen_grpc_target, ":" + codegen_target], |
||||
deps = deps + grpc_deps, |
||||
**kwargs |
||||
) |
||||
else: |
||||
native.cc_library( |
||||
name = name, |
||||
srcs = [":" + codegen_target], |
||||
hdrs = [":" + codegen_target], |
||||
deps = deps + ["//external:protobuf"], |
||||
**kwargs |
||||
) |
||||
proto_deps = ["_" + dep + "_only" for dep in deps if dep.find(":") == -1] |
||||
proto_deps += [dep.split(":")[0] + ":" + "_" + dep.split(":")[1] + "_only" for dep in deps if dep.find(":") != -1] |
||||
if well_known_protos: |
||||
proto_deps += well_known_proto_libs() |
||||
|
||||
native.proto_library( |
||||
name = proto_target, |
||||
srcs = srcs, |
||||
deps = proto_deps, |
||||
**kwargs |
||||
) |
||||
|
||||
native.cc_proto_library( |
||||
name = cc_proto_target, |
||||
deps = [":" + proto_target], |
||||
**kwargs |
||||
) |
||||
extra_deps.append(":" + cc_proto_target) |
||||
proto_targets.append(proto_target) |
||||
else: |
||||
if not srcs: |
||||
fail("srcs cannot be empty", "srcs") |
||||
proto_targets += srcs |
||||
|
||||
if not proto_only: |
||||
codegen_grpc_target = "_" + name + "_grpc_codegen" |
||||
generate_cc( |
||||
name = codegen_grpc_target, |
||||
srcs = proto_targets, |
||||
plugin = "@com_github_grpc_grpc//:grpc_cpp_plugin", |
||||
well_known_protos = well_known_protos, |
||||
generate_mocks = generate_mocks, |
||||
**kwargs |
||||
) |
||||
|
||||
native.cc_library( |
||||
name = name, |
||||
srcs = [":" + codegen_grpc_target], |
||||
hdrs = [":" + codegen_grpc_target], |
||||
deps = deps + |
||||
extra_deps + |
||||
["@com_github_grpc_grpc//:grpc++_codegen_proto"], |
||||
**kwargs |
||||
) |
||||
|
@ -0,0 +1,154 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_INTERNAL_COMPILER_CPP_PLUGIN_H |
||||
#define GRPC_INTERNAL_COMPILER_CPP_PLUGIN_H |
||||
|
||||
#include <memory> |
||||
#include <sstream> |
||||
|
||||
#include "src/compiler/config.h" |
||||
|
||||
#include "src/compiler/cpp_generator.h" |
||||
#include "src/compiler/generator_helpers.h" |
||||
#include "src/compiler/protobuf_plugin.h" |
||||
|
||||
// Cpp Generator for Protobug IDL
|
||||
class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
||||
public: |
||||
CppGrpcGenerator() {} |
||||
virtual ~CppGrpcGenerator() {} |
||||
|
||||
virtual bool Generate(const grpc::protobuf::FileDescriptor* file, |
||||
const grpc::string& parameter, |
||||
grpc::protobuf::compiler::GeneratorContext* context, |
||||
grpc::string* error) const { |
||||
if (file->options().cc_generic_services()) { |
||||
*error = |
||||
"cpp grpc proto compiler plugin does not work with generic " |
||||
"services. To generate cpp grpc APIs, please set \"" |
||||
"cc_generic_service = false\"."; |
||||
return false; |
||||
} |
||||
|
||||
grpc_cpp_generator::Parameters generator_parameters; |
||||
generator_parameters.use_system_headers = true; |
||||
generator_parameters.generate_mock_code = false; |
||||
generator_parameters.include_import_headers = false; |
||||
|
||||
ProtoBufFile pbfile(file); |
||||
|
||||
if (!parameter.empty()) { |
||||
std::vector<grpc::string> parameters_list = |
||||
grpc_generator::tokenize(parameter, ","); |
||||
for (auto parameter_string = parameters_list.begin(); |
||||
parameter_string != parameters_list.end(); parameter_string++) { |
||||
std::vector<grpc::string> param = |
||||
grpc_generator::tokenize(*parameter_string, "="); |
||||
if (param[0] == "services_namespace") { |
||||
generator_parameters.services_namespace = param[1]; |
||||
} else if (param[0] == "use_system_headers") { |
||||
if (param[1] == "true") { |
||||
generator_parameters.use_system_headers = true; |
||||
} else if (param[1] == "false") { |
||||
generator_parameters.use_system_headers = false; |
||||
} else { |
||||
*error = grpc::string("Invalid parameter: ") + *parameter_string; |
||||
return false; |
||||
} |
||||
} else if (param[0] == "grpc_search_path") { |
||||
generator_parameters.grpc_search_path = param[1]; |
||||
} else if (param[0] == "generate_mock_code") { |
||||
if (param[1] == "true") { |
||||
generator_parameters.generate_mock_code = true; |
||||
} else if (param[1] != "false") { |
||||
*error = grpc::string("Invalid parameter: ") + *parameter_string; |
||||
return false; |
||||
} |
||||
} else if (param[0] == "gmock_search_path") { |
||||
generator_parameters.gmock_search_path = param[1]; |
||||
} else if (param[0] == "additional_header_includes") { |
||||
generator_parameters.additional_header_includes = |
||||
grpc_generator::tokenize(param[1], ":"); |
||||
} else if (param[0] == "message_header_extension") { |
||||
generator_parameters.message_header_extension = param[1]; |
||||
} else if (param[0] == "include_import_headers") { |
||||
if (param[1] == "true") { |
||||
generator_parameters.include_import_headers = true; |
||||
} else if (param[1] != "false") { |
||||
*error = grpc::string("Invalid parameter: ") + *parameter_string; |
||||
return false; |
||||
} |
||||
} else { |
||||
*error = grpc::string("Unknown parameter: ") + *parameter_string; |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
|
||||
grpc::string file_name = grpc_generator::StripProto(file->name()); |
||||
|
||||
grpc::string header_code = |
||||
grpc_cpp_generator::GetHeaderPrologue(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetHeaderIncludes(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetHeaderServices(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetHeaderEpilogue(&pbfile, generator_parameters); |
||||
std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( |
||||
context->Open(file_name + ".grpc.pb.h")); |
||||
grpc::protobuf::io::CodedOutputStream header_coded_out(header_output.get()); |
||||
header_coded_out.WriteRaw(header_code.data(), header_code.size()); |
||||
|
||||
grpc::string source_code = |
||||
grpc_cpp_generator::GetSourcePrologue(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetSourceIncludes(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetSourceServices(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetSourceEpilogue(&pbfile, generator_parameters); |
||||
std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( |
||||
context->Open(file_name + ".grpc.pb.cc")); |
||||
grpc::protobuf::io::CodedOutputStream source_coded_out(source_output.get()); |
||||
source_coded_out.WriteRaw(source_code.data(), source_code.size()); |
||||
|
||||
if (!generator_parameters.generate_mock_code) { |
||||
return true; |
||||
} |
||||
grpc::string mock_code = |
||||
grpc_cpp_generator::GetMockPrologue(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetMockIncludes(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetMockServices(&pbfile, generator_parameters) + |
||||
grpc_cpp_generator::GetMockEpilogue(&pbfile, generator_parameters); |
||||
std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> mock_output( |
||||
context->Open(file_name + "_mock.grpc.pb.h")); |
||||
grpc::protobuf::io::CodedOutputStream mock_coded_out(mock_output.get()); |
||||
mock_coded_out.WriteRaw(mock_code.data(), mock_code.size()); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
private: |
||||
// Insert the given code into the given file at the given insertion point.
|
||||
void Insert(grpc::protobuf::compiler::GeneratorContext* context, |
||||
const grpc::string& filename, const grpc::string& insertion_point, |
||||
const grpc::string& code) const { |
||||
std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
||||
context->OpenForInsert(filename, insertion_point)); |
||||
grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
||||
coded_out.WriteRaw(code.data(), code.size()); |
||||
} |
||||
}; |
||||
|
||||
#endif // GRPC_INTERNAL_COMPILER_CPP_PLUGIN_H
|
@ -0,0 +1,240 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2019 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
using System.Runtime.InteropServices; |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
using System.Buffers; |
||||
#endif |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
public class DefaultDeserializationContextTest |
||||
{ |
||||
FakeBufferReaderManager fakeBufferReaderManager; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
fakeBufferReaderManager = new FakeBufferReaderManager(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
fakeBufferReaderManager.Dispose(); |
||||
} |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
[TestCase] |
||||
public void PayloadAsReadOnlySequence_ZeroSegmentPayload() |
||||
{ |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {})); |
||||
|
||||
Assert.AreEqual(0, context.PayloadLength); |
||||
|
||||
var sequence = context.PayloadAsReadOnlySequence(); |
||||
|
||||
Assert.AreEqual(ReadOnlySequence<byte>.Empty, sequence); |
||||
Assert.IsTrue(sequence.IsEmpty); |
||||
Assert.IsTrue(sequence.IsSingleSegment); |
||||
} |
||||
|
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void PayloadAsReadOnlySequence_SingleSegmentPayload(int segmentLength) |
||||
{ |
||||
var origBuffer = GetTestBuffer(segmentLength); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
var sequence = context.PayloadAsReadOnlySequence(); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, sequence.Length); |
||||
Assert.AreEqual(origBuffer.Length, sequence.First.Length); |
||||
Assert.IsTrue(sequence.IsSingleSegment); |
||||
CollectionAssert.AreEqual(origBuffer, sequence.First.ToArray()); |
||||
} |
||||
|
||||
[TestCase(0, 5, 10)] |
||||
[TestCase(1, 1, 1)] |
||||
[TestCase(10, 100, 1000)] |
||||
[TestCase(100, 100, 10)] |
||||
[TestCase(1000, 1000, 1000)] |
||||
public void PayloadAsReadOnlySequence_MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3) |
||||
{ |
||||
var origBuffer1 = GetTestBuffer(segmentLen1); |
||||
var origBuffer2 = GetTestBuffer(segmentLen2); |
||||
var origBuffer3 = GetTestBuffer(segmentLen3); |
||||
int totalLen = origBuffer1.Length + origBuffer2.Length + origBuffer3.Length; |
||||
|
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer1, origBuffer2, origBuffer3 })); |
||||
|
||||
Assert.AreEqual(totalLen, context.PayloadLength); |
||||
|
||||
var sequence = context.PayloadAsReadOnlySequence(); |
||||
|
||||
Assert.AreEqual(totalLen, sequence.Length); |
||||
|
||||
var segmentEnumerator = sequence.GetEnumerator(); |
||||
|
||||
Assert.IsTrue(segmentEnumerator.MoveNext()); |
||||
CollectionAssert.AreEqual(origBuffer1, segmentEnumerator.Current.ToArray()); |
||||
|
||||
Assert.IsTrue(segmentEnumerator.MoveNext()); |
||||
CollectionAssert.AreEqual(origBuffer2, segmentEnumerator.Current.ToArray()); |
||||
|
||||
Assert.IsTrue(segmentEnumerator.MoveNext()); |
||||
CollectionAssert.AreEqual(origBuffer3, segmentEnumerator.Current.ToArray()); |
||||
|
||||
Assert.IsFalse(segmentEnumerator.MoveNext()); |
||||
} |
||||
#endif |
||||
|
||||
[TestCase] |
||||
public void NullPayloadNotAllowed() |
||||
{ |
||||
var context = new DefaultDeserializationContext(); |
||||
Assert.Throws(typeof(InvalidOperationException), () => context.Initialize(fakeBufferReaderManager.CreateNullPayloadBufferReader())); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void PayloadAsNewByteBuffer_ZeroSegmentPayload() |
||||
{ |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {})); |
||||
|
||||
Assert.AreEqual(0, context.PayloadLength); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
Assert.AreEqual(0, payload.Length); |
||||
} |
||||
|
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void PayloadAsNewByteBuffer_SingleSegmentPayload(int segmentLength) |
||||
{ |
||||
var origBuffer = GetTestBuffer(segmentLength); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
CollectionAssert.AreEqual(origBuffer, payload); |
||||
} |
||||
|
||||
[TestCase(0, 5, 10)] |
||||
[TestCase(1, 1, 1)] |
||||
[TestCase(10, 100, 1000)] |
||||
[TestCase(100, 100, 10)] |
||||
[TestCase(1000, 1000, 1000)] |
||||
public void PayloadAsNewByteBuffer_MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3) |
||||
{ |
||||
var origBuffer1 = GetTestBuffer(segmentLen1); |
||||
var origBuffer2 = GetTestBuffer(segmentLen2); |
||||
var origBuffer3 = GetTestBuffer(segmentLen3); |
||||
|
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer1, origBuffer2, origBuffer3 })); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
|
||||
var concatenatedOrigBuffers = new List<byte>(); |
||||
concatenatedOrigBuffers.AddRange(origBuffer1); |
||||
concatenatedOrigBuffers.AddRange(origBuffer2); |
||||
concatenatedOrigBuffers.AddRange(origBuffer3); |
||||
|
||||
Assert.AreEqual(concatenatedOrigBuffers.Count, context.PayloadLength); |
||||
Assert.AreEqual(concatenatedOrigBuffers.Count, payload.Length); |
||||
CollectionAssert.AreEqual(concatenatedOrigBuffers, payload); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void GetPayloadMultipleTimesIsIllegal() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
var payload = context.PayloadAsNewBuffer(); |
||||
CollectionAssert.AreEqual(origBuffer, payload); |
||||
|
||||
// Getting payload multiple times is illegal |
||||
Assert.Throws(typeof(InvalidOperationException), () => context.PayloadAsNewBuffer()); |
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
Assert.Throws(typeof(InvalidOperationException), () => context.PayloadAsReadOnlySequence()); |
||||
#endif |
||||
} |
||||
|
||||
[TestCase] |
||||
public void ResetContextAndReinitialize() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
var context = new DefaultDeserializationContext(); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer)); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, context.PayloadLength); |
||||
|
||||
// Reset invalidates context |
||||
context.Reset(); |
||||
|
||||
Assert.AreEqual(0, context.PayloadLength); |
||||
Assert.Throws(typeof(NullReferenceException), () => context.PayloadAsNewBuffer()); |
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
Assert.Throws(typeof(NullReferenceException), () => context.PayloadAsReadOnlySequence()); |
||||
#endif |
||||
|
||||
// Previously reset context can be initialized again |
||||
var origBuffer2 = GetTestBuffer(50); |
||||
context.Initialize(fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer2)); |
||||
|
||||
Assert.AreEqual(origBuffer2.Length, context.PayloadLength); |
||||
CollectionAssert.AreEqual(origBuffer2, context.PayloadAsNewBuffer()); |
||||
} |
||||
|
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,118 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading.Tasks; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
// Creates instances of fake IBufferReader. All created instances will become invalid once Dispose is called. |
||||
internal class FakeBufferReaderManager : IDisposable |
||||
{ |
||||
List<GCHandle> pinnedHandles = new List<GCHandle>(); |
||||
bool disposed = false; |
||||
public IBufferReader CreateSingleSegmentBufferReader(byte[] data) |
||||
{ |
||||
return CreateMultiSegmentBufferReader(new List<byte[]> { data }); |
||||
} |
||||
|
||||
public IBufferReader CreateMultiSegmentBufferReader(IEnumerable<byte[]> dataSegments) |
||||
{ |
||||
GrpcPreconditions.CheckState(!disposed); |
||||
GrpcPreconditions.CheckNotNull(dataSegments); |
||||
var segments = new List<GCHandle>(); |
||||
foreach (var data in dataSegments) |
||||
{ |
||||
GrpcPreconditions.CheckNotNull(data); |
||||
segments.Add(GCHandle.Alloc(data, GCHandleType.Pinned)); |
||||
} |
||||
pinnedHandles.AddRange(segments); // all the allocated GCHandles will be freed on Dispose() |
||||
return new FakeBufferReader(segments); |
||||
} |
||||
|
||||
public IBufferReader CreateNullPayloadBufferReader() |
||||
{ |
||||
GrpcPreconditions.CheckState(!disposed); |
||||
return new FakeBufferReader(null); |
||||
} |
||||
|
||||
public void Dispose() |
||||
{ |
||||
if (!disposed) |
||||
{ |
||||
disposed = true; |
||||
for (int i = 0; i < pinnedHandles.Count; i++) |
||||
{ |
||||
pinnedHandles[i].Free(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private class FakeBufferReader : IBufferReader |
||||
{ |
||||
readonly List<GCHandle> bufferSegments; |
||||
readonly int? totalLength; |
||||
readonly IEnumerator<GCHandle> segmentEnumerator; |
||||
|
||||
public FakeBufferReader(List<GCHandle> bufferSegments) |
||||
{ |
||||
this.bufferSegments = bufferSegments; |
||||
this.totalLength = ComputeTotalLength(bufferSegments); |
||||
this.segmentEnumerator = bufferSegments?.GetEnumerator(); |
||||
} |
||||
|
||||
public int? TotalLength => totalLength; |
||||
|
||||
public bool TryGetNextSlice(out Slice slice) |
||||
{ |
||||
GrpcPreconditions.CheckNotNull(bufferSegments); |
||||
if (!segmentEnumerator.MoveNext()) |
||||
{ |
||||
slice = default(Slice); |
||||
return false; |
||||
} |
||||
|
||||
var segment = segmentEnumerator.Current; |
||||
int sliceLen = ((byte[]) segment.Target).Length; |
||||
slice = new Slice(segment.AddrOfPinnedObject(), sliceLen); |
||||
return true; |
||||
} |
||||
|
||||
static int? ComputeTotalLength(List<GCHandle> bufferSegments) |
||||
{ |
||||
if (bufferSegments == null) |
||||
{ |
||||
return null; |
||||
} |
||||
|
||||
int sum = 0; |
||||
foreach (var segment in bufferSegments) |
||||
{ |
||||
var data = (byte[]) segment.Target; |
||||
sum += data.Length; |
||||
} |
||||
return sum; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,121 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
public class FakeBufferReaderManagerTest |
||||
{ |
||||
FakeBufferReaderManager fakeBufferReaderManager; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
fakeBufferReaderManager = new FakeBufferReaderManager(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
fakeBufferReaderManager.Dispose(); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void NullPayload() |
||||
{ |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateNullPayloadBufferReader(); |
||||
Assert.IsFalse(fakeBufferReader.TotalLength.HasValue); |
||||
Assert.Throws(typeof(ArgumentNullException), () => fakeBufferReader.TryGetNextSlice(out Slice slice)); |
||||
} |
||||
[TestCase] |
||||
public void ZeroSegmentPayload() |
||||
{ |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {}); |
||||
Assert.AreEqual(0, fakeBufferReader.TotalLength.Value); |
||||
Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice)); |
||||
} |
||||
|
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(30)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void SingleSegmentPayload(int bufferLen) |
||||
{ |
||||
var origBuffer = GetTestBuffer(bufferLen); |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateSingleSegmentBufferReader(origBuffer); |
||||
Assert.AreEqual(origBuffer.Length, fakeBufferReader.TotalLength.Value); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice)); |
||||
AssertSliceDataEqual(origBuffer, slice); |
||||
|
||||
Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice2)); |
||||
} |
||||
|
||||
[TestCase(0, 5, 10)] |
||||
[TestCase(1, 1, 1)] |
||||
[TestCase(10, 100, 1000)] |
||||
[TestCase(100, 100, 10)] |
||||
[TestCase(1000, 1000, 1000)] |
||||
public void MultiSegmentPayload(int segmentLen1, int segmentLen2, int segmentLen3) |
||||
{ |
||||
var origBuffer1 = GetTestBuffer(segmentLen1); |
||||
var origBuffer2 = GetTestBuffer(segmentLen2); |
||||
var origBuffer3 = GetTestBuffer(segmentLen3); |
||||
var fakeBufferReader = fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer1, origBuffer2, origBuffer3 }); |
||||
|
||||
Assert.AreEqual(origBuffer1.Length + origBuffer2.Length + origBuffer3.Length, fakeBufferReader.TotalLength.Value); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice1)); |
||||
AssertSliceDataEqual(origBuffer1, slice1); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice2)); |
||||
AssertSliceDataEqual(origBuffer2, slice2); |
||||
|
||||
Assert.IsTrue(fakeBufferReader.TryGetNextSlice(out Slice slice3)); |
||||
AssertSliceDataEqual(origBuffer3, slice3); |
||||
|
||||
Assert.IsFalse(fakeBufferReader.TryGetNextSlice(out Slice slice4)); |
||||
} |
||||
|
||||
private void AssertSliceDataEqual(byte[] expected, Slice actual) |
||||
{ |
||||
var actualSliceData = new byte[actual.Length]; |
||||
actual.CopyTo(new ArraySegment<byte>(actualSliceData)); |
||||
CollectionAssert.AreEqual(expected, actualSliceData); |
||||
} |
||||
|
||||
// create a buffer of given size and fill it with some data |
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,151 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
using System.Runtime.InteropServices; |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
using System.Buffers; |
||||
#endif |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
// Converts IBufferReader into instances of ReadOnlySequence<byte> |
||||
// Objects representing the sequence segments are cached to decrease GC load. |
||||
public class ReusableSliceBufferTest |
||||
{ |
||||
FakeBufferReaderManager fakeBufferReaderManager; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
fakeBufferReaderManager = new FakeBufferReaderManager(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
fakeBufferReaderManager.Dispose(); |
||||
} |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
[TestCase] |
||||
public void NullPayload() |
||||
{ |
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
Assert.Throws(typeof(ArgumentNullException), () => sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateNullPayloadBufferReader())); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void ZeroSegmentPayload() |
||||
{ |
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
var sequence = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> {})); |
||||
|
||||
Assert.AreEqual(ReadOnlySequence<byte>.Empty, sequence); |
||||
Assert.IsTrue(sequence.IsEmpty); |
||||
Assert.IsTrue(sequence.IsSingleSegment); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void SegmentsAreCached() |
||||
{ |
||||
var bufferSegments1 = Enumerable.Range(0, 100).Select((_) => GetTestBuffer(50)).ToList(); |
||||
var bufferSegments2 = Enumerable.Range(0, 100).Select((_) => GetTestBuffer(50)).ToList(); |
||||
|
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
|
||||
var sequence1 = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments1)); |
||||
var memoryManagers1 = GetMemoryManagersForSequenceSegments(sequence1); |
||||
|
||||
sliceBuffer.Invalidate(); |
||||
|
||||
var sequence2 = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments2)); |
||||
var memoryManagers2 = GetMemoryManagersForSequenceSegments(sequence2); |
||||
|
||||
// check memory managers are identical objects (i.e. they've been reused) |
||||
CollectionAssert.AreEquivalent(memoryManagers1, memoryManagers2); |
||||
} |
||||
|
||||
[TestCase] |
||||
public void MultiSegmentPayload_LotsOfSegments() |
||||
{ |
||||
var bufferSegments = Enumerable.Range(0, ReusableSliceBuffer.MaxCachedSegments + 100).Select((_) => GetTestBuffer(10)).ToList(); |
||||
|
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
var sequence = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments)); |
||||
|
||||
int index = 0; |
||||
foreach (var memory in sequence) |
||||
{ |
||||
CollectionAssert.AreEqual(bufferSegments[index], memory.ToArray()); |
||||
index ++; |
||||
} |
||||
} |
||||
|
||||
[TestCase] |
||||
public void InvalidateMakesSequenceUnusable() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
|
||||
var sliceBuffer = new ReusableSliceBuffer(); |
||||
var sequence = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List<byte[]> { origBuffer })); |
||||
|
||||
Assert.AreEqual(origBuffer.Length, sequence.Length); |
||||
|
||||
sliceBuffer.Invalidate(); |
||||
|
||||
// Invalidate with make the returned sequence completely unusable and broken, users must not use it beyond the deserializer functions. |
||||
Assert.Throws(typeof(ArgumentOutOfRangeException), () => { var first = sequence.First; }); |
||||
} |
||||
|
||||
private List<MemoryManager<byte>> GetMemoryManagersForSequenceSegments(ReadOnlySequence<byte> sequence) |
||||
{ |
||||
var result = new List<MemoryManager<byte>>(); |
||||
foreach (var memory in sequence) |
||||
{ |
||||
Assert.IsTrue(MemoryMarshal.TryGetMemoryManager(memory, out MemoryManager<byte> memoryManager)); |
||||
result.Add(memoryManager); |
||||
} |
||||
return result; |
||||
} |
||||
#else |
||||
[TestCase] |
||||
public void OnlySupportedOnNetCore() |
||||
{ |
||||
// Test case needs to exist to make C# sanity test happy. |
||||
} |
||||
#endif |
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,83 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2018 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
using System.Runtime.InteropServices; |
||||
|
||||
namespace Grpc.Core.Internal.Tests |
||||
{ |
||||
public class SliceTest |
||||
{ |
||||
[TestCase(0)] |
||||
[TestCase(1)] |
||||
[TestCase(10)] |
||||
[TestCase(100)] |
||||
[TestCase(1000)] |
||||
public void SliceFromNativePtr_CopyToArraySegment(int bufferLength) |
||||
{ |
||||
var origBuffer = GetTestBuffer(bufferLength); |
||||
var gcHandle = GCHandle.Alloc(origBuffer, GCHandleType.Pinned); |
||||
try |
||||
{ |
||||
var slice = new Slice(gcHandle.AddrOfPinnedObject(), origBuffer.Length); |
||||
Assert.AreEqual(bufferLength, slice.Length); |
||||
|
||||
var newBuffer = new byte[bufferLength]; |
||||
slice.CopyTo(new ArraySegment<byte>(newBuffer)); |
||||
CollectionAssert.AreEqual(origBuffer, newBuffer); |
||||
} |
||||
finally |
||||
{ |
||||
gcHandle.Free(); |
||||
} |
||||
} |
||||
|
||||
[TestCase] |
||||
public void SliceFromNativePtr_CopyToArraySegmentTooSmall() |
||||
{ |
||||
var origBuffer = GetTestBuffer(100); |
||||
var gcHandle = GCHandle.Alloc(origBuffer, GCHandleType.Pinned); |
||||
try |
||||
{ |
||||
var slice = new Slice(gcHandle.AddrOfPinnedObject(), origBuffer.Length); |
||||
var tooSmall = new byte[origBuffer.Length - 1]; |
||||
Assert.Catch(typeof(ArgumentException), () => slice.CopyTo(new ArraySegment<byte>(tooSmall))); |
||||
} |
||||
finally |
||||
{ |
||||
gcHandle.Free(); |
||||
} |
||||
} |
||||
|
||||
// create a buffer of given size and fill it with some data |
||||
private byte[] GetTestBuffer(int length) |
||||
{ |
||||
var testBuffer = new byte[length]; |
||||
for (int i = 0; i < testBuffer.Length; i++) |
||||
{ |
||||
testBuffer[i] = (byte) i; |
||||
} |
||||
return testBuffer; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,148 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2019 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
|
||||
using Grpc.Core.Utils; |
||||
using System; |
||||
using System.Threading; |
||||
|
||||
using System.Buffers; |
||||
|
||||
namespace Grpc.Core.Internal |
||||
{ |
||||
internal class ReusableSliceBuffer |
||||
{ |
||||
public const int MaxCachedSegments = 1024; // ~4MB payload for 4K slices |
||||
|
||||
readonly SliceSegment[] cachedSegments = new SliceSegment[MaxCachedSegments]; |
||||
int populatedSegmentCount; |
||||
|
||||
public ReadOnlySequence<byte> PopulateFrom(IBufferReader bufferReader) |
||||
{ |
||||
populatedSegmentCount = 0; |
||||
long offset = 0; |
||||
SliceSegment prevSegment = null; |
||||
while (bufferReader.TryGetNextSlice(out Slice slice)) |
||||
{ |
||||
// Initialize cached segment if still null or just allocate a new segment if we already reached MaxCachedSegments |
||||
var current = populatedSegmentCount < cachedSegments.Length ? cachedSegments[populatedSegmentCount] : new SliceSegment(); |
||||
if (current == null) |
||||
{ |
||||
current = cachedSegments[populatedSegmentCount] = new SliceSegment(); |
||||
} |
||||
|
||||
current.Reset(slice, offset); |
||||
prevSegment?.SetNext(current); |
||||
|
||||
populatedSegmentCount ++; |
||||
offset += slice.Length; |
||||
prevSegment = current; |
||||
} |
||||
|
||||
// Not necessary for ending the ReadOnlySequence, but for making sure we |
||||
// don't keep more than MaxCachedSegments alive. |
||||
prevSegment?.SetNext(null); |
||||
|
||||
if (populatedSegmentCount == 0) |
||||
{ |
||||
return ReadOnlySequence<byte>.Empty; |
||||
} |
||||
|
||||
var firstSegment = cachedSegments[0]; |
||||
var lastSegment = prevSegment; |
||||
return new ReadOnlySequence<byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length); |
||||
} |
||||
|
||||
public void Invalidate() |
||||
{ |
||||
if (populatedSegmentCount == 0) |
||||
{ |
||||
return; |
||||
} |
||||
var segment = cachedSegments[0]; |
||||
while (segment != null) |
||||
{ |
||||
segment.Reset(new Slice(IntPtr.Zero, 0), 0); |
||||
var nextSegment = (SliceSegment) segment.Next; |
||||
segment.SetNext(null); |
||||
segment = nextSegment; |
||||
} |
||||
populatedSegmentCount = 0; |
||||
} |
||||
|
||||
// Represents a segment in ReadOnlySequence |
||||
// Segment is backed by Slice and the instances are reusable. |
||||
private class SliceSegment : ReadOnlySequenceSegment<byte> |
||||
{ |
||||
readonly SliceMemoryManager pointerMemoryManager = new SliceMemoryManager(); |
||||
|
||||
public void Reset(Slice slice, long runningIndex) |
||||
{ |
||||
pointerMemoryManager.Reset(slice); |
||||
Memory = pointerMemoryManager.Memory; // maybe not always necessary |
||||
RunningIndex = runningIndex; |
||||
} |
||||
|
||||
public void SetNext(ReadOnlySequenceSegment<byte> next) |
||||
{ |
||||
Next = next; |
||||
} |
||||
} |
||||
|
||||
// Allow creating instances of Memory<byte> from Slice. |
||||
// Represents a chunk of native memory, but doesn't manage its lifetime. |
||||
// Instances of this class are reuseable - they can be reset to point to a different memory chunk. |
||||
// That is important to make the instances cacheable (rather then creating new instances |
||||
// the old ones will be reused to reduce GC pressure). |
||||
private class SliceMemoryManager : MemoryManager<byte> |
||||
{ |
||||
private Slice slice; |
||||
|
||||
public void Reset(Slice slice) |
||||
{ |
||||
this.slice = slice; |
||||
} |
||||
|
||||
public void Reset() |
||||
{ |
||||
Reset(new Slice(IntPtr.Zero, 0)); |
||||
} |
||||
|
||||
public override Span<byte> GetSpan() |
||||
{ |
||||
return slice.ToSpanUnsafe(); |
||||
} |
||||
|
||||
public override MemoryHandle Pin(int elementIndex = 0) |
||||
{ |
||||
throw new NotSupportedException(); |
||||
} |
||||
|
||||
public override void Unpin() |
||||
{ |
||||
} |
||||
|
||||
protected override void Dispose(bool disposing) |
||||
{ |
||||
// NOP |
||||
} |
||||
} |
||||
} |
||||
} |
||||
#endif |
@ -0,0 +1,68 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2019 The gRPC Authors |
||||
// |
||||
// Licensed under the Apache License, Version 2.0 (the "License"); |
||||
// you may not use this file except in compliance with the License. |
||||
// You may obtain a copy of the License at |
||||
// |
||||
// http://www.apache.org/licenses/LICENSE-2.0 |
||||
// |
||||
// Unless required by applicable law or agreed to in writing, software |
||||
// distributed under the License is distributed on an "AS IS" BASIS, |
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
// See the License for the specific language governing permissions and |
||||
// limitations under the License. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core.Internal |
||||
{ |
||||
/// <summary> |
||||
/// Slice of native memory. |
||||
/// Rough equivalent of grpc_slice (but doesn't support inlined slices, just a pointer to data and length) |
||||
/// </summary> |
||||
internal struct Slice |
||||
{ |
||||
private readonly IntPtr dataPtr; |
||||
private readonly int length; |
||||
|
||||
public Slice(IntPtr dataPtr, int length) |
||||
{ |
||||
this.dataPtr = dataPtr; |
||||
this.length = length; |
||||
} |
||||
|
||||
public int Length => length; |
||||
|
||||
// copies data of the slice to given span. |
||||
// there needs to be enough space in the destination buffer |
||||
public void CopyTo(ArraySegment<byte> destination) |
||||
{ |
||||
Marshal.Copy(dataPtr, destination.Array, destination.Offset, length); |
||||
} |
||||
|
||||
#if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY |
||||
public Span<byte> ToSpanUnsafe() |
||||
{ |
||||
unsafe |
||||
{ |
||||
return new Span<byte>((byte*) dataPtr, length); |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
/// <summary> |
||||
/// Returns a <see cref="System.String"/> that represents the current <see cref="Grpc.Core.Internal.Slice"/>. |
||||
/// </summary> |
||||
public override string ToString() |
||||
{ |
||||
return string.Format("[Slice: dataPtr={0}, length={1}]", dataPtr, length); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/linux/grpc_run_tests_matrix.sh" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests linux php --inner_jobs 16 -j 2 --internal_ci --max_time=3600" |
||||
} |
@ -0,0 +1,30 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/linux/grpc_run_tests_matrix.sh" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests linux python --inner_jobs 16 -j 2 --internal_ci --max_time=3600" |
||||
} |
@ -0,0 +1,30 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/linux/grpc_run_tests_matrix.sh" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests linux ruby --inner_jobs 16 -j 2 --internal_ci --max_time=3600" |
||||
} |
@ -0,0 +1,31 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/macos/grpc_run_tests_matrix.sh" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/GrpcTesting-d0eeee2db331.json" |
||||
timeout_mins: 120 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests macos corelang --internal_ci -j 1 --inner_jobs 4 --max_time=3600" |
||||
} |
@ -0,0 +1,31 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/macos/grpc_run_tests_matrix.sh" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/GrpcTesting-d0eeee2db331.json" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests macos csharp --internal_ci -j 1 --inner_jobs 4 --max_time=3600" |
||||
} |
@ -0,0 +1,31 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/macos/grpc_run_tests_matrix.sh" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/GrpcTesting-d0eeee2db331.json" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests macos grpc-node --internal_ci -j 1 --inner_jobs 4 --max_time=3600" |
||||
} |
@ -0,0 +1,31 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/macos/grpc_run_tests_matrix.sh" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/GrpcTesting-d0eeee2db331.json" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests macos php --internal_ci -j 1 --inner_jobs 4 --max_time=3600" |
||||
} |
@ -0,0 +1,31 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/macos/grpc_run_tests_matrix.sh" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/GrpcTesting-d0eeee2db331.json" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests macos python --internal_ci -j 1 --inner_jobs 4 --max_time=3600" |
||||
} |
@ -0,0 +1,31 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/macos/grpc_run_tests_matrix.sh" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/GrpcTesting-d0eeee2db331.json" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests macos ruby --internal_ci -j 1 --inner_jobs 4 --max_time=3600" |
||||
} |
@ -0,0 +1,30 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/windows/grpc_run_tests_matrix.bat" |
||||
timeout_mins: 120 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests windows c -j 1 --inner_jobs 8 --internal_ci --max_time=3600" |
||||
} |
@ -0,0 +1,30 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/windows/grpc_run_tests_matrix.bat" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests windows csharp -j 1 --inner_jobs 8 --internal_ci --max_time=3600" |
||||
} |
@ -0,0 +1,30 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/windows/grpc_run_tests_matrix.bat" |
||||
timeout_mins: 60 |
||||
action { |
||||
define_artifacts { |
||||
regex: "**/*sponge_log.*" |
||||
regex: "github/grpc/reports/**" |
||||
} |
||||
} |
||||
|
||||
env_vars { |
||||
key: "RUN_TESTS_FLAGS" |
||||
value: "-f basictests windows python -j 1 --inner_jobs 8 --internal_ci --max_time=3600" |
||||
} |
@ -0,0 +1,32 @@ |
||||
# Copyright 2019 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
# Config file for the internal CI (in protobuf text format) |
||||
|
||||
# Location of the continuous shell script in repository. |
||||
build_file: "grpc/tools/internal_ci/windows/bazel_rbe.bat" |
||||
|
||||
timeout_mins: 60 |
||||
|
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/GrpcTesting-d0eeee2db331.json" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/resultstore_api_key" |
||||
gfile_resources: "/bigstore/grpc-testing-secrets/gcp_credentials/rbe-windows-credentials.json" |
||||
|
||||
bazel_setting { |
||||
# In order for Kokoro to recognize this as a bazel build and publish the bazel resultstore link, |
||||
# the bazel_setting section needs to be present and "upsalite_frontend_address" needs to be |
||||
# set. The rest of configuration from bazel_setting is unused (we configure everything when bazel |
||||
# command is invoked). |
||||
upsalite_frontend_address: "https://source.cloud.google.com" |
||||
} |
Loading…
Reference in new issue