mirror of https://github.com/grpc/grpc.git
commit
38ffd8ca84
39 changed files with 1447 additions and 97 deletions
@ -0,0 +1,55 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* 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 GRPCPP_IMPL_CODEGEN_MESSAGE_ALLOCATOR_H |
||||||
|
#define GRPCPP_IMPL_CODEGEN_MESSAGE_ALLOCATOR_H |
||||||
|
|
||||||
|
namespace grpc { |
||||||
|
namespace experimental { |
||||||
|
|
||||||
|
// This is per rpc struct for the allocator. We can potentially put the grpc
|
||||||
|
// call arena in here in the future.
|
||||||
|
template <typename RequestT, typename ResponseT> |
||||||
|
struct RpcAllocatorInfo { |
||||||
|
RequestT* request; |
||||||
|
ResponseT* response; |
||||||
|
// per rpc allocator internal state. MessageAllocator can set it when
|
||||||
|
// AllocateMessages is called and use it later.
|
||||||
|
void* allocator_state; |
||||||
|
}; |
||||||
|
|
||||||
|
// Implementations need to be thread-safe
|
||||||
|
template <typename RequestT, typename ResponseT> |
||||||
|
class MessageAllocator { |
||||||
|
public: |
||||||
|
virtual ~MessageAllocator() = default; |
||||||
|
// Allocate both request and response
|
||||||
|
virtual void AllocateMessages( |
||||||
|
RpcAllocatorInfo<RequestT, ResponseT>* info) = 0; |
||||||
|
// Optional: deallocate request early, called by
|
||||||
|
// ServerCallbackRpcController::ReleaseRequest
|
||||||
|
virtual void DeallocateRequest(RpcAllocatorInfo<RequestT, ResponseT>* info) {} |
||||||
|
// Deallocate response and request (if applicable)
|
||||||
|
virtual void DeallocateMessages( |
||||||
|
RpcAllocatorInfo<RequestT, ResponseT>* info) = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace experimental
|
||||||
|
} // namespace grpc
|
||||||
|
|
||||||
|
#endif // GRPCPP_IMPL_CODEGEN_MESSAGE_ALLOCATOR_H
|
@ -0,0 +1,24 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* 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 GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H |
||||||
|
#define GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H |
||||||
|
|
||||||
|
#include <grpcpp/impl/codegen/message_allocator.h> |
||||||
|
|
||||||
|
#endif // GRPCPP_SUPPORT_MESSAGE_ALLOCATOR_H
|
@ -0,0 +1,56 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#import <XCTest/XCTest.h> |
||||||
|
|
||||||
|
#import <GRPCClient/GRPCCallOptions.h> |
||||||
|
|
||||||
|
@interface StressTests : XCTestCase |
||||||
|
/**
|
||||||
|
* Host to send the RPCs to. The base implementation returns nil, which would make all tests to |
||||||
|
* fail. |
||||||
|
* Override in a subclass to perform these tests against a specific address. |
||||||
|
*/ |
||||||
|
+ (NSString *)host; |
||||||
|
|
||||||
|
/**
|
||||||
|
* Bytes of overhead of test proto responses due to encoding. This is used to excercise the behavior |
||||||
|
* when responses are just above or below the max response size. For some reason, the local and |
||||||
|
* remote servers enconde responses with different overhead (?), so this is defined per-subclass. |
||||||
|
*/ |
||||||
|
- (int32_t)encodingOverhead; |
||||||
|
|
||||||
|
/**
|
||||||
|
* The type of transport to be used. The base implementation returns default. Subclasses should |
||||||
|
* override to appropriate settings. |
||||||
|
*/ |
||||||
|
+ (GRPCTransportType)transportType; |
||||||
|
|
||||||
|
/**
|
||||||
|
* The root certificates to be used. The base implementation returns nil. Subclasses should override |
||||||
|
* to appropriate settings. |
||||||
|
*/ |
||||||
|
+ (NSString *)PEMRootCertificates; |
||||||
|
|
||||||
|
/**
|
||||||
|
* The root certificates to be used. The base implementation returns nil. Subclasses should override |
||||||
|
* to appropriate settings. |
||||||
|
*/ |
||||||
|
+ (NSString *)hostNameOverride; |
||||||
|
|
||||||
|
@end |
@ -0,0 +1,237 @@ |
|||||||
|
/* |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
#include "StressTests.h" |
||||||
|
|
||||||
|
#import <GRPCClient/GRPCCall+ChannelArg.h> |
||||||
|
#import <GRPCClient/GRPCCall+Tests.h> |
||||||
|
#import <GRPCClient/internal_testing/GRPCCall+InternalTests.h> |
||||||
|
#import <ProtoRPC/ProtoRPC.h> |
||||||
|
#import <RemoteTest/Messages.pbobjc.h> |
||||||
|
#import <RemoteTest/Test.pbobjc.h> |
||||||
|
#import <RemoteTest/Test.pbrpc.h> |
||||||
|
#import <RxLibrary/GRXBufferedPipe.h> |
||||||
|
#import <RxLibrary/GRXWriter+Immediate.h> |
||||||
|
#import <grpc/grpc.h> |
||||||
|
#import <grpc/support/log.h> |
||||||
|
|
||||||
|
#define TEST_TIMEOUT 32 |
||||||
|
|
||||||
|
extern const char *kCFStreamVarName; |
||||||
|
|
||||||
|
// Convenience class to use blocks as callbacks |
||||||
|
@interface MacTestsBlockCallbacks : NSObject<GRPCProtoResponseHandler> |
||||||
|
|
||||||
|
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback |
||||||
|
messageCallback:(void (^)(id))messageCallback |
||||||
|
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback; |
||||||
|
|
||||||
|
@end |
||||||
|
|
||||||
|
@implementation MacTestsBlockCallbacks { |
||||||
|
void (^_initialMetadataCallback)(NSDictionary *); |
||||||
|
void (^_messageCallback)(id); |
||||||
|
void (^_closeCallback)(NSDictionary *, NSError *); |
||||||
|
dispatch_queue_t _dispatchQueue; |
||||||
|
} |
||||||
|
|
||||||
|
- (instancetype)initWithInitialMetadataCallback:(void (^)(NSDictionary *))initialMetadataCallback |
||||||
|
messageCallback:(void (^)(id))messageCallback |
||||||
|
closeCallback:(void (^)(NSDictionary *, NSError *))closeCallback { |
||||||
|
if ((self = [super init])) { |
||||||
|
_initialMetadataCallback = initialMetadataCallback; |
||||||
|
_messageCallback = messageCallback; |
||||||
|
_closeCallback = closeCallback; |
||||||
|
_dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL); |
||||||
|
} |
||||||
|
return self; |
||||||
|
} |
||||||
|
|
||||||
|
- (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata { |
||||||
|
if (_initialMetadataCallback) { |
||||||
|
_initialMetadataCallback(initialMetadata); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
- (void)didReceiveProtoMessage:(GPBMessage *)message { |
||||||
|
if (_messageCallback) { |
||||||
|
_messageCallback(message); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error { |
||||||
|
if (_closeCallback) { |
||||||
|
_closeCallback(trailingMetadata, error); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
- (dispatch_queue_t)dispatchQueue { |
||||||
|
return _dispatchQueue; |
||||||
|
} |
||||||
|
|
||||||
|
@end |
||||||
|
|
||||||
|
@implementation StressTests { |
||||||
|
RMTTestService *_service; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)host { |
||||||
|
return nil; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)hostAddress { |
||||||
|
return nil; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)PEMRootCertificates { |
||||||
|
return nil; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)hostNameOverride { |
||||||
|
return nil; |
||||||
|
} |
||||||
|
|
||||||
|
- (int32_t)encodingOverhead { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
+ (void)setUp { |
||||||
|
setenv(kCFStreamVarName, "1", 1); |
||||||
|
} |
||||||
|
|
||||||
|
- (void)setUp { |
||||||
|
self.continueAfterFailure = NO; |
||||||
|
|
||||||
|
[GRPCCall resetHostSettings]; |
||||||
|
|
||||||
|
GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init]; |
||||||
|
options.transportType = [[self class] transportType]; |
||||||
|
options.PEMRootCertificates = [[self class] PEMRootCertificates]; |
||||||
|
options.hostNameOverride = [[self class] hostNameOverride]; |
||||||
|
_service = [RMTTestService serviceWithHost:[[self class] host] callOptions:options]; |
||||||
|
system([[NSString stringWithFormat:@"sudo ifconfig lo0 alias %@", [[self class] hostAddress]] |
||||||
|
UTF8String]); |
||||||
|
} |
||||||
|
|
||||||
|
- (void)tearDown { |
||||||
|
system([[NSString stringWithFormat:@"sudo ifconfig lo0 -alias %@", [[self class] hostAddress]] |
||||||
|
UTF8String]); |
||||||
|
} |
||||||
|
|
||||||
|
+ (GRPCTransportType)transportType { |
||||||
|
return GRPCTransportTypeChttp2BoringSSL; |
||||||
|
} |
||||||
|
|
||||||
|
- (void)testNetworkFlapWithV2API { |
||||||
|
NSMutableArray *completeExpectations = [NSMutableArray array]; |
||||||
|
NSMutableArray *calls = [NSMutableArray array]; |
||||||
|
int num_rpcs = 100; |
||||||
|
__block BOOL address_removed = FALSE; |
||||||
|
__block BOOL address_readded = FALSE; |
||||||
|
for (int i = 0; i < num_rpcs; ++i) { |
||||||
|
[completeExpectations |
||||||
|
addObject:[self expectationWithDescription: |
||||||
|
[NSString stringWithFormat:@"Received trailer for RPC %d", i]]]; |
||||||
|
|
||||||
|
RMTSimpleRequest *request = [RMTSimpleRequest message]; |
||||||
|
request.responseType = RMTPayloadType_Compressable; |
||||||
|
request.responseSize = 314159; |
||||||
|
request.payload.body = [NSMutableData dataWithLength:271828]; |
||||||
|
|
||||||
|
GRPCUnaryProtoCall *call = [_service |
||||||
|
unaryCallWithMessage:request |
||||||
|
responseHandler:[[MacTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil |
||||||
|
messageCallback:^(id message) { |
||||||
|
if (message) { |
||||||
|
RMTSimpleResponse *expectedResponse = |
||||||
|
[RMTSimpleResponse message]; |
||||||
|
expectedResponse.payload.type = RMTPayloadType_Compressable; |
||||||
|
expectedResponse.payload.body = |
||||||
|
[NSMutableData dataWithLength:314159]; |
||||||
|
XCTAssertEqualObjects(message, expectedResponse); |
||||||
|
} |
||||||
|
} |
||||||
|
closeCallback:^(NSDictionary *trailingMetadata, NSError *error) { |
||||||
|
|
||||||
|
@synchronized(self) { |
||||||
|
if (error == nil && !address_removed) { |
||||||
|
system([[NSString |
||||||
|
stringWithFormat:@"sudo ifconfig lo0 -alias %@", |
||||||
|
[[self class] hostAddress]] |
||||||
|
UTF8String]); |
||||||
|
address_removed = YES; |
||||||
|
} else if (error != nil && !address_readded) { |
||||||
|
system([ |
||||||
|
[NSString stringWithFormat:@"sudo ifconfig lo0 alias %@", |
||||||
|
[[self class] hostAddress]] |
||||||
|
UTF8String]); |
||||||
|
address_readded = YES; |
||||||
|
} |
||||||
|
} |
||||||
|
[completeExpectations[i] fulfill]; |
||||||
|
}] |
||||||
|
callOptions:nil]; |
||||||
|
[calls addObject:call]; |
||||||
|
} |
||||||
|
|
||||||
|
for (int i = 0; i < num_rpcs; ++i) { |
||||||
|
GRPCUnaryProtoCall *call = calls[i]; |
||||||
|
[call start]; |
||||||
|
[NSThread sleepForTimeInterval:0.1f]; |
||||||
|
} |
||||||
|
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
||||||
|
} |
||||||
|
|
||||||
|
- (void)testNetworkFlapWithV1API { |
||||||
|
NSMutableArray *completeExpectations = [NSMutableArray array]; |
||||||
|
int num_rpcs = 100; |
||||||
|
__block BOOL address_removed = FALSE; |
||||||
|
__block BOOL address_readded = FALSE; |
||||||
|
for (int i = 0; i < num_rpcs; ++i) { |
||||||
|
[completeExpectations |
||||||
|
addObject:[self expectationWithDescription: |
||||||
|
[NSString stringWithFormat:@"Received response for RPC %d", i]]]; |
||||||
|
|
||||||
|
RMTSimpleRequest *request = [RMTSimpleRequest message]; |
||||||
|
request.responseType = RMTPayloadType_Compressable; |
||||||
|
request.responseSize = 314159; |
||||||
|
request.payload.body = [NSMutableData dataWithLength:271828]; |
||||||
|
|
||||||
|
[_service unaryCallWithRequest:request |
||||||
|
handler:^(RMTSimpleResponse *response, NSError *error) { |
||||||
|
@synchronized(self) { |
||||||
|
if (error == nil && !address_removed) { |
||||||
|
system([[NSString stringWithFormat:@"sudo ifconfig lo0 -alias %@", |
||||||
|
[[self class] hostAddress]] |
||||||
|
UTF8String]); |
||||||
|
address_removed = YES; |
||||||
|
} else if (error != nil && !address_readded) { |
||||||
|
system([[NSString stringWithFormat:@"sudo ifconfig lo0 alias %@", |
||||||
|
[[self class] hostAddress]] |
||||||
|
UTF8String]); |
||||||
|
address_readded = YES; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
[completeExpectations[i] fulfill]; |
||||||
|
}]; |
||||||
|
|
||||||
|
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@end |
@ -0,0 +1,68 @@ |
|||||||
|
|
||||||
|
/* |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#import <GRPCClient/GRPCCall+Tests.h> |
||||||
|
#import <GRPCClient/internal_testing/GRPCCall+InternalTests.h> |
||||||
|
|
||||||
|
#import "StressTests.h" |
||||||
|
|
||||||
|
static NSString *const kHostAddress = @"10.0.0.1"; |
||||||
|
|
||||||
|
// The Protocol Buffers encoding overhead of local interop server. Acquired |
||||||
|
// by experiment. Adjust this when server's proto file changes. |
||||||
|
static int32_t kLocalInteropServerOverhead = 10; |
||||||
|
|
||||||
|
/** Tests in InteropTests.m, sending the RPCs to a local cleartext server. */ |
||||||
|
@interface StressTestsCleartext : StressTests |
||||||
|
@end |
||||||
|
|
||||||
|
@implementation StressTestsCleartext |
||||||
|
|
||||||
|
+ (NSString *)host { |
||||||
|
return [NSString stringWithFormat:@"%@:5050", kHostAddress]; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)hostAddress { |
||||||
|
return kHostAddress; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)PEMRootCertificates { |
||||||
|
return nil; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)hostNameOverride { |
||||||
|
return nil; |
||||||
|
} |
||||||
|
|
||||||
|
- (int32_t)encodingOverhead { |
||||||
|
return kLocalInteropServerOverhead; // bytes |
||||||
|
} |
||||||
|
|
||||||
|
- (void)setUp { |
||||||
|
[super setUp]; |
||||||
|
|
||||||
|
// Register test server as non-SSL. |
||||||
|
[GRPCCall useInsecureConnectionsForHost:[[self class] host]]; |
||||||
|
} |
||||||
|
|
||||||
|
+ (GRPCTransportType)transportType { |
||||||
|
return GRPCTransportTypeInsecure; |
||||||
|
} |
||||||
|
|
||||||
|
@end |
@ -0,0 +1,71 @@ |
|||||||
|
/* |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#import <GRPCClient/GRPCCall+Tests.h> |
||||||
|
#import <GRPCClient/internal_testing/GRPCCall+InternalTests.h> |
||||||
|
|
||||||
|
#include "StressTests.h" |
||||||
|
|
||||||
|
static NSString *const kHostAddress = @"10.0.0.1"; |
||||||
|
// The Protocol Buffers encoding overhead of local interop server. Acquired |
||||||
|
// by experiment. Adjust this when server's proto file changes. |
||||||
|
static int32_t kLocalInteropServerOverhead = 10; |
||||||
|
|
||||||
|
@interface StressTestsSSL : StressTests |
||||||
|
@end |
||||||
|
|
||||||
|
@implementation StressTestsSSL |
||||||
|
|
||||||
|
+ (NSString *)host { |
||||||
|
return [NSString stringWithFormat:@"%@:5051", kHostAddress]; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)hostAddress { |
||||||
|
return kHostAddress; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)PEMRootCertificates { |
||||||
|
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
||||||
|
NSString *certsPath = |
||||||
|
[bundle pathForResource:@"TestCertificates.bundle/test-certificates" ofType:@"pem"]; |
||||||
|
NSError *error; |
||||||
|
return [NSString stringWithContentsOfFile:certsPath encoding:NSUTF8StringEncoding error:&error]; |
||||||
|
} |
||||||
|
|
||||||
|
+ (NSString *)hostNameOverride { |
||||||
|
return @"foo.test.google.fr"; |
||||||
|
} |
||||||
|
|
||||||
|
- (int32_t)encodingOverhead { |
||||||
|
return kLocalInteropServerOverhead; // bytes |
||||||
|
} |
||||||
|
|
||||||
|
+ (GRPCTransportType)transportType { |
||||||
|
return GRPCTransportTypeChttp2BoringSSL; |
||||||
|
} |
||||||
|
|
||||||
|
- (void)setUp { |
||||||
|
[super setUp]; |
||||||
|
|
||||||
|
// Register test server certificates and name. |
||||||
|
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; |
||||||
|
NSString *certsPath = |
||||||
|
[bundle pathForResource:@"TestCertificates.bundle/test-certificates" ofType:@"pem"]; |
||||||
|
[GRPCCall useTestCertsPath:certsPath testName:@"foo.test.google.fr" forHost:[[self class] host]]; |
||||||
|
} |
||||||
|
@end |
@ -0,0 +1,405 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <algorithm> |
||||||
|
#include <functional> |
||||||
|
#include <memory> |
||||||
|
#include <mutex> |
||||||
|
#include <sstream> |
||||||
|
#include <thread> |
||||||
|
|
||||||
|
#include <google/protobuf/arena.h> |
||||||
|
|
||||||
|
#include <gtest/gtest.h> |
||||||
|
|
||||||
|
#include <grpcpp/channel.h> |
||||||
|
#include <grpcpp/client_context.h> |
||||||
|
#include <grpcpp/create_channel.h> |
||||||
|
#include <grpcpp/server.h> |
||||||
|
#include <grpcpp/server_builder.h> |
||||||
|
#include <grpcpp/server_context.h> |
||||||
|
#include <grpcpp/support/client_callback.h> |
||||||
|
#include <grpcpp/support/message_allocator.h> |
||||||
|
|
||||||
|
#include "src/core/lib/iomgr/iomgr.h" |
||||||
|
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
||||||
|
#include "test/core/util/port.h" |
||||||
|
#include "test/core/util/test_config.h" |
||||||
|
#include "test/cpp/util/test_credentials_provider.h" |
||||||
|
|
||||||
|
// MAYBE_SKIP_TEST is a macro to determine if this particular test configuration
|
||||||
|
// should be skipped based on a decision made at SetUp time. In particular, any
|
||||||
|
// callback tests can only be run if the iomgr can run in the background or if
|
||||||
|
// the transport is in-process.
|
||||||
|
#define MAYBE_SKIP_TEST \ |
||||||
|
do { \
|
||||||
|
if (do_not_test_) { \
|
||||||
|
return; \
|
||||||
|
} \
|
||||||
|
} while (0) |
||||||
|
|
||||||
|
namespace grpc { |
||||||
|
namespace testing { |
||||||
|
namespace { |
||||||
|
|
||||||
|
class CallbackTestServiceImpl |
||||||
|
: public EchoTestService::ExperimentalCallbackService { |
||||||
|
public: |
||||||
|
explicit CallbackTestServiceImpl() {} |
||||||
|
|
||||||
|
void SetFreeRequest() { free_request_ = true; } |
||||||
|
|
||||||
|
void SetAllocatorMutator( |
||||||
|
std::function<void(void* allocator_state, const EchoRequest* req, |
||||||
|
EchoResponse* resp)> |
||||||
|
mutator) { |
||||||
|
allocator_mutator_ = mutator; |
||||||
|
} |
||||||
|
|
||||||
|
void Echo(ServerContext* context, const EchoRequest* request, |
||||||
|
EchoResponse* response, |
||||||
|
experimental::ServerCallbackRpcController* controller) override { |
||||||
|
response->set_message(request->message()); |
||||||
|
if (free_request_) { |
||||||
|
controller->FreeRequest(); |
||||||
|
} else if (allocator_mutator_) { |
||||||
|
allocator_mutator_(controller->GetAllocatorState(), request, response); |
||||||
|
} |
||||||
|
controller->Finish(Status::OK); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
bool free_request_ = false; |
||||||
|
std::function<void(void* allocator_state, const EchoRequest* req, |
||||||
|
EchoResponse* resp)> |
||||||
|
allocator_mutator_; |
||||||
|
}; |
||||||
|
|
||||||
|
enum class Protocol { INPROC, TCP }; |
||||||
|
|
||||||
|
class TestScenario { |
||||||
|
public: |
||||||
|
TestScenario(Protocol protocol, const grpc::string& creds_type) |
||||||
|
: protocol(protocol), credentials_type(creds_type) {} |
||||||
|
void Log() const; |
||||||
|
Protocol protocol; |
||||||
|
const grpc::string credentials_type; |
||||||
|
}; |
||||||
|
|
||||||
|
static std::ostream& operator<<(std::ostream& out, |
||||||
|
const TestScenario& scenario) { |
||||||
|
return out << "TestScenario{protocol=" |
||||||
|
<< (scenario.protocol == Protocol::INPROC ? "INPROC" : "TCP") |
||||||
|
<< "," << scenario.credentials_type << "}"; |
||||||
|
} |
||||||
|
|
||||||
|
void TestScenario::Log() const { |
||||||
|
std::ostringstream out; |
||||||
|
out << *this; |
||||||
|
gpr_log(GPR_INFO, "%s", out.str().c_str()); |
||||||
|
} |
||||||
|
|
||||||
|
class MessageAllocatorEnd2endTestBase |
||||||
|
: public ::testing::TestWithParam<TestScenario> { |
||||||
|
protected: |
||||||
|
MessageAllocatorEnd2endTestBase() { |
||||||
|
GetParam().Log(); |
||||||
|
if (GetParam().protocol == Protocol::TCP) { |
||||||
|
if (!grpc_iomgr_run_in_background()) { |
||||||
|
do_not_test_ = true; |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
~MessageAllocatorEnd2endTestBase() = default; |
||||||
|
|
||||||
|
void CreateServer( |
||||||
|
experimental::MessageAllocator<EchoRequest, EchoResponse>* allocator) { |
||||||
|
ServerBuilder builder; |
||||||
|
|
||||||
|
auto server_creds = GetCredentialsProvider()->GetServerCredentials( |
||||||
|
GetParam().credentials_type); |
||||||
|
if (GetParam().protocol == Protocol::TCP) { |
||||||
|
picked_port_ = grpc_pick_unused_port_or_die(); |
||||||
|
server_address_ << "localhost:" << picked_port_; |
||||||
|
builder.AddListeningPort(server_address_.str(), server_creds); |
||||||
|
} |
||||||
|
callback_service_.SetMessageAllocatorFor_Echo(allocator); |
||||||
|
builder.RegisterService(&callback_service_); |
||||||
|
|
||||||
|
server_ = builder.BuildAndStart(); |
||||||
|
is_server_started_ = true; |
||||||
|
} |
||||||
|
|
||||||
|
void ResetStub() { |
||||||
|
ChannelArguments args; |
||||||
|
auto channel_creds = GetCredentialsProvider()->GetChannelCredentials( |
||||||
|
GetParam().credentials_type, &args); |
||||||
|
switch (GetParam().protocol) { |
||||||
|
case Protocol::TCP: |
||||||
|
channel_ = |
||||||
|
CreateCustomChannel(server_address_.str(), channel_creds, args); |
||||||
|
break; |
||||||
|
case Protocol::INPROC: |
||||||
|
channel_ = server_->InProcessChannel(args); |
||||||
|
break; |
||||||
|
default: |
||||||
|
assert(false); |
||||||
|
} |
||||||
|
stub_ = EchoTestService::NewStub(channel_); |
||||||
|
} |
||||||
|
|
||||||
|
void TearDown() override { |
||||||
|
if (is_server_started_) { |
||||||
|
server_->Shutdown(); |
||||||
|
} |
||||||
|
if (picked_port_ > 0) { |
||||||
|
grpc_recycle_unused_port(picked_port_); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void SendRpcs(int num_rpcs) { |
||||||
|
grpc::string test_string(""); |
||||||
|
for (int i = 0; i < num_rpcs; i++) { |
||||||
|
EchoRequest request; |
||||||
|
EchoResponse response; |
||||||
|
ClientContext cli_ctx; |
||||||
|
|
||||||
|
test_string += grpc::string(1024, 'x'); |
||||||
|
request.set_message(test_string); |
||||||
|
grpc::string val; |
||||||
|
cli_ctx.set_compression_algorithm(GRPC_COMPRESS_GZIP); |
||||||
|
|
||||||
|
std::mutex mu; |
||||||
|
std::condition_variable cv; |
||||||
|
bool done = false; |
||||||
|
stub_->experimental_async()->Echo( |
||||||
|
&cli_ctx, &request, &response, |
||||||
|
[&request, &response, &done, &mu, &cv, val](Status s) { |
||||||
|
GPR_ASSERT(s.ok()); |
||||||
|
|
||||||
|
EXPECT_EQ(request.message(), response.message()); |
||||||
|
std::lock_guard<std::mutex> l(mu); |
||||||
|
done = true; |
||||||
|
cv.notify_one(); |
||||||
|
}); |
||||||
|
std::unique_lock<std::mutex> l(mu); |
||||||
|
while (!done) { |
||||||
|
cv.wait(l); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
bool do_not_test_{false}; |
||||||
|
bool is_server_started_{false}; |
||||||
|
int picked_port_{0}; |
||||||
|
std::shared_ptr<Channel> channel_; |
||||||
|
std::unique_ptr<EchoTestService::Stub> stub_; |
||||||
|
CallbackTestServiceImpl callback_service_; |
||||||
|
std::unique_ptr<Server> server_; |
||||||
|
std::ostringstream server_address_; |
||||||
|
}; |
||||||
|
|
||||||
|
class NullAllocatorTest : public MessageAllocatorEnd2endTestBase {}; |
||||||
|
|
||||||
|
TEST_P(NullAllocatorTest, SimpleRpc) { |
||||||
|
MAYBE_SKIP_TEST; |
||||||
|
CreateServer(nullptr); |
||||||
|
ResetStub(); |
||||||
|
SendRpcs(1); |
||||||
|
} |
||||||
|
|
||||||
|
class SimpleAllocatorTest : public MessageAllocatorEnd2endTestBase { |
||||||
|
public: |
||||||
|
class SimpleAllocator |
||||||
|
: public experimental::MessageAllocator<EchoRequest, EchoResponse> { |
||||||
|
public: |
||||||
|
void AllocateMessages( |
||||||
|
experimental::RpcAllocatorInfo<EchoRequest, EchoResponse>* info) { |
||||||
|
allocation_count++; |
||||||
|
info->request = new EchoRequest; |
||||||
|
info->response = new EchoResponse; |
||||||
|
info->allocator_state = info; |
||||||
|
} |
||||||
|
void DeallocateRequest( |
||||||
|
experimental::RpcAllocatorInfo<EchoRequest, EchoResponse>* info) { |
||||||
|
request_deallocation_count++; |
||||||
|
delete info->request; |
||||||
|
info->request = nullptr; |
||||||
|
} |
||||||
|
void DeallocateMessages( |
||||||
|
experimental::RpcAllocatorInfo<EchoRequest, EchoResponse>* info) { |
||||||
|
messages_deallocation_count++; |
||||||
|
delete info->request; |
||||||
|
delete info->response; |
||||||
|
} |
||||||
|
|
||||||
|
int allocation_count = 0; |
||||||
|
int request_deallocation_count = 0; |
||||||
|
int messages_deallocation_count = 0; |
||||||
|
}; |
||||||
|
}; |
||||||
|
|
||||||
|
TEST_P(SimpleAllocatorTest, SimpleRpc) { |
||||||
|
MAYBE_SKIP_TEST; |
||||||
|
const int kRpcCount = 10; |
||||||
|
std::unique_ptr<SimpleAllocator> allocator(new SimpleAllocator); |
||||||
|
CreateServer(allocator.get()); |
||||||
|
ResetStub(); |
||||||
|
SendRpcs(kRpcCount); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->allocation_count); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->messages_deallocation_count); |
||||||
|
EXPECT_EQ(0, allocator->request_deallocation_count); |
||||||
|
} |
||||||
|
|
||||||
|
TEST_P(SimpleAllocatorTest, RpcWithEarlyFreeRequest) { |
||||||
|
MAYBE_SKIP_TEST; |
||||||
|
const int kRpcCount = 10; |
||||||
|
std::unique_ptr<SimpleAllocator> allocator(new SimpleAllocator); |
||||||
|
callback_service_.SetFreeRequest(); |
||||||
|
CreateServer(allocator.get()); |
||||||
|
ResetStub(); |
||||||
|
SendRpcs(kRpcCount); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->allocation_count); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->messages_deallocation_count); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->request_deallocation_count); |
||||||
|
} |
||||||
|
|
||||||
|
TEST_P(SimpleAllocatorTest, RpcWithReleaseRequest) { |
||||||
|
MAYBE_SKIP_TEST; |
||||||
|
const int kRpcCount = 10; |
||||||
|
std::unique_ptr<SimpleAllocator> allocator(new SimpleAllocator); |
||||||
|
std::vector<EchoRequest*> released_requests; |
||||||
|
auto mutator = [&released_requests](void* allocator_state, |
||||||
|
const EchoRequest* req, |
||||||
|
EchoResponse* resp) { |
||||||
|
auto* info = |
||||||
|
static_cast<experimental::RpcAllocatorInfo<EchoRequest, EchoResponse>*>( |
||||||
|
allocator_state); |
||||||
|
EXPECT_EQ(req, info->request); |
||||||
|
EXPECT_EQ(resp, info->response); |
||||||
|
EXPECT_EQ(allocator_state, info->allocator_state); |
||||||
|
released_requests.push_back(info->request); |
||||||
|
info->request = nullptr; |
||||||
|
}; |
||||||
|
callback_service_.SetAllocatorMutator(mutator); |
||||||
|
CreateServer(allocator.get()); |
||||||
|
ResetStub(); |
||||||
|
SendRpcs(kRpcCount); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->allocation_count); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->messages_deallocation_count); |
||||||
|
EXPECT_EQ(0, allocator->request_deallocation_count); |
||||||
|
EXPECT_EQ(static_cast<unsigned>(kRpcCount), released_requests.size()); |
||||||
|
for (auto* req : released_requests) { |
||||||
|
delete req; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class ArenaAllocatorTest : public MessageAllocatorEnd2endTestBase { |
||||||
|
public: |
||||||
|
class ArenaAllocator |
||||||
|
: public experimental::MessageAllocator<EchoRequest, EchoResponse> { |
||||||
|
public: |
||||||
|
void AllocateMessages( |
||||||
|
experimental::RpcAllocatorInfo<EchoRequest, EchoResponse>* info) { |
||||||
|
allocation_count++; |
||||||
|
auto* arena = new google::protobuf::Arena; |
||||||
|
info->allocator_state = arena; |
||||||
|
info->request = |
||||||
|
google::protobuf::Arena::CreateMessage<EchoRequest>(arena); |
||||||
|
info->response = |
||||||
|
google::protobuf::Arena::CreateMessage<EchoResponse>(arena); |
||||||
|
} |
||||||
|
void DeallocateRequest( |
||||||
|
experimental::RpcAllocatorInfo<EchoRequest, EchoResponse>* info) { |
||||||
|
GPR_ASSERT(0); |
||||||
|
} |
||||||
|
void DeallocateMessages( |
||||||
|
experimental::RpcAllocatorInfo<EchoRequest, EchoResponse>* info) { |
||||||
|
deallocation_count++; |
||||||
|
auto* arena = |
||||||
|
static_cast<google::protobuf::Arena*>(info->allocator_state); |
||||||
|
delete arena; |
||||||
|
} |
||||||
|
|
||||||
|
int allocation_count = 0; |
||||||
|
int deallocation_count = 0; |
||||||
|
}; |
||||||
|
}; |
||||||
|
|
||||||
|
TEST_P(ArenaAllocatorTest, SimpleRpc) { |
||||||
|
MAYBE_SKIP_TEST; |
||||||
|
const int kRpcCount = 10; |
||||||
|
std::unique_ptr<ArenaAllocator> allocator(new ArenaAllocator); |
||||||
|
CreateServer(allocator.get()); |
||||||
|
ResetStub(); |
||||||
|
SendRpcs(kRpcCount); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->allocation_count); |
||||||
|
EXPECT_EQ(kRpcCount, allocator->deallocation_count); |
||||||
|
} |
||||||
|
|
||||||
|
std::vector<TestScenario> CreateTestScenarios(bool test_insecure) { |
||||||
|
std::vector<TestScenario> scenarios; |
||||||
|
std::vector<grpc::string> credentials_types{ |
||||||
|
GetCredentialsProvider()->GetSecureCredentialsTypeList()}; |
||||||
|
auto insec_ok = [] { |
||||||
|
// Only allow insecure credentials type when it is registered with the
|
||||||
|
// provider. User may create providers that do not have insecure.
|
||||||
|
return GetCredentialsProvider()->GetChannelCredentials( |
||||||
|
kInsecureCredentialsType, nullptr) != nullptr; |
||||||
|
}; |
||||||
|
if (test_insecure && insec_ok()) { |
||||||
|
credentials_types.push_back(kInsecureCredentialsType); |
||||||
|
} |
||||||
|
GPR_ASSERT(!credentials_types.empty()); |
||||||
|
|
||||||
|
Protocol parr[]{Protocol::INPROC, Protocol::TCP}; |
||||||
|
for (Protocol p : parr) { |
||||||
|
for (const auto& cred : credentials_types) { |
||||||
|
// TODO(vjpai): Test inproc with secure credentials when feasible
|
||||||
|
if (p == Protocol::INPROC && |
||||||
|
(cred != kInsecureCredentialsType || !insec_ok())) { |
||||||
|
continue; |
||||||
|
} |
||||||
|
scenarios.emplace_back(p, cred); |
||||||
|
} |
||||||
|
} |
||||||
|
return scenarios; |
||||||
|
} |
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P(NullAllocatorTest, NullAllocatorTest, |
||||||
|
::testing::ValuesIn(CreateTestScenarios(true))); |
||||||
|
INSTANTIATE_TEST_CASE_P(SimpleAllocatorTest, SimpleAllocatorTest, |
||||||
|
::testing::ValuesIn(CreateTestScenarios(true))); |
||||||
|
INSTANTIATE_TEST_CASE_P(ArenaAllocatorTest, ArenaAllocatorTest, |
||||||
|
::testing::ValuesIn(CreateTestScenarios(true))); |
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace testing
|
||||||
|
} // namespace grpc
|
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
grpc::testing::TestEnvironment env(argc, argv); |
||||||
|
// The grpc_init is to cover the MAYBE_SKIP_TEST.
|
||||||
|
grpc_init(); |
||||||
|
::testing::InitGoogleTest(&argc, argv); |
||||||
|
int ret = RUN_ALL_TESTS(); |
||||||
|
grpc_shutdown(); |
||||||
|
return ret; |
||||||
|
} |
Loading…
Reference in new issue