clang-format

pull/16190/head
Muxi Yan 6 years ago
parent f0cbcde731
commit 680b53f7ad
  1. 4
      src/objective-c/GRPCClient/GRPCCall.h
  2. 59
      src/objective-c/GRPCClient/GRPCCall.m
  3. 3
      src/objective-c/GRPCClient/private/GRPCChannel.h
  4. 18
      src/objective-c/GRPCClient/private/GRPCChannel.m
  5. 4
      src/objective-c/GRPCClient/private/GRPCChannelPool.h
  6. 58
      src/objective-c/GRPCClient/private/GRPCChannelPool.m
  7. 6
      src/objective-c/GRPCClient/private/GRPCCronetChannelFactory.m
  8. 3
      src/objective-c/GRPCClient/private/GRPCHost.m
  9. 3
      src/objective-c/GRPCClient/private/GRPCInsecureChannelFactory.m
  10. 15
      src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m
  11. 7
      src/objective-c/GRPCClient/private/GRPCWrappedCall.m
  12. 3
      src/objective-c/GRPCClient/private/utilities.h
  13. 50
      src/objective-c/ProtoRPC/ProtoRPC.m
  14. 102
      src/objective-c/tests/ChannelTests/ChannelPoolTest.m
  15. 4
      src/objective-c/tests/ChannelTests/ChannelTests.m
  16. 3
      src/objective-c/tests/InteropTests.m

@ -340,8 +340,8 @@ NS_ASSUME_NONNULL_END
* and the port number, for example @"localhost:5050".
*/
- (null_unspecified instancetype)initWithHost:(null_unspecified NSString *)host
path:(null_unspecified NSString *)path
requestsWriter:(null_unspecified GRXWriter *)requestWriter;
path:(null_unspecified NSString *)path
requestsWriter:(null_unspecified GRXWriter *)requestWriter;
/**
* Finishes the request side of this call, notifies the server that the RPC should be cancelled, and

@ -122,9 +122,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
responseHandler:(id<GRPCResponseHandler>)responseHandler
callOptions:(GRPCCallOptions *)callOptions {
NSAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0,
@"Neither host nor path can be nil.");
NSAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest,
@"Invalid call safety value.");
@"Neither host nor path can be nil.");
NSAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, @"Invalid call safety value.");
NSAssert(responseHandler != nil, @"Response handler required.");
if (requestOptions.host.length == 0 || requestOptions.path.length == 0) {
return nil;
@ -136,7 +135,6 @@ const char *kCFStreamVarName = "grpc_cfstream";
return nil;
}
if ((self = [super init])) {
_requestOptions = [requestOptions copy];
if (callOptions == nil) {
@ -159,7 +157,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
#endif
_dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
}
dispatch_set_target_queue(_dispatchQueue ,responseHandler.dispatchQueue);
dispatch_set_target_queue(_dispatchQueue, responseHandler.dispatchQueue);
_started = NO;
_canceled = NO;
_finished = NO;
@ -176,7 +174,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)start {
GRPCCall *call = nil;
@synchronized (self) {
@synchronized(self) {
NSAssert(!_started, @"Call already started.");
NSAssert(!_canceled, @"Call already canceled.");
if (_started) {
@ -192,10 +190,10 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
_call = [[GRPCCall alloc] initWithHost:_requestOptions.host
path:_requestOptions.path
callSafety:_requestOptions.safety
requestsWriter:_pipe
callOptions:_callOptions];
path:_requestOptions.path
callSafety:_requestOptions.safety
requestsWriter:_pipe
callOptions:_callOptions];
if (_callOptions.initialMetadata) {
[_call.requestHeaders addEntriesFromDictionary:_callOptions.initialMetadata];
}
@ -203,7 +201,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
void (^valueHandler)(id value) = ^(id value) {
@synchronized (self) {
@synchronized(self) {
if (self->_handler) {
if (!self->_initialMetadataPublished) {
self->_initialMetadataPublished = YES;
@ -223,7 +221,6 @@ const char *kCFStreamVarName = "grpc_cfstream";
[self issueInitialMetadata:self->_call.responseHeaders];
}
[self issueClosedWithTrailingMetadata:self->_call.responseTrailers error:errorOrNil];
}
// Clearing _call must happen *after* dispatching close in order to get trailing
// metadata from _call.
@ -237,14 +234,13 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
};
id<GRXWriteable> responseWriteable =
[[GRXWriteable alloc] initWithValueHandler:valueHandler
completionHandler:completionHandler];
[[GRXWriteable alloc] initWithValueHandler:valueHandler completionHandler:completionHandler];
[call startWithWriteable:responseWriteable];
}
- (void)cancel {
GRPCCall *call = nil;
@synchronized (self) {
@synchronized(self) {
if (_canceled) {
return;
}
@ -259,7 +255,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
dispatch_async(_dispatchQueue, ^{
// Copy to local so that block is freed after cancellation completes.
id<GRPCResponseHandler> copiedHandler = nil;
@synchronized (self) {
@synchronized(self) {
copiedHandler = self->_handler;
self->_handler = nil;
}
@ -268,9 +264,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
error:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled
userInfo:@{
NSLocalizedDescriptionKey :
@"Canceled by app"
}]];
NSLocalizedDescriptionKey :
@"Canceled by app"
}]];
});
}
}
@ -322,11 +318,12 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
- (void)issueInitialMetadata:(NSDictionary *)initialMetadata {
@synchronized (self) {
if (initialMetadata != nil && [_handler respondsToSelector:@selector(receivedInitialMetadata:)]) {
@synchronized(self) {
if (initialMetadata != nil &&
[_handler respondsToSelector:@selector(receivedInitialMetadata:)]) {
dispatch_async(_dispatchQueue, ^{
id<GRPCResponseHandler> handler = nil;
@synchronized (self) {
@synchronized(self) {
handler = self->_handler;
}
[handler receivedInitialMetadata:initialMetadata];
@ -336,11 +333,11 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
- (void)issueMessage:(id)message {
@synchronized (self) {
@synchronized(self) {
if (message != nil && [_handler respondsToSelector:@selector(receivedRawMessage:)]) {
dispatch_async(_dispatchQueue, ^{
id<GRPCResponseHandler> handler = nil;
@synchronized (self) {
@synchronized(self) {
handler = self->_handler;
}
[handler receivedRawMessage:message];
@ -350,17 +347,16 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
- (void)issueClosedWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
@synchronized (self) {
@synchronized(self) {
if ([_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(_dispatchQueue, ^{
id<GRPCResponseHandler> handler = nil;
@synchronized (self) {
@synchronized(self) {
handler = self->_handler;
// Clean up _handler so that no more responses are reported to the handler.
self->_handler = nil;
}
[handler closedWithTrailingMetadata:trailingMetadata
error:error];
[handler closedWithTrailingMetadata:trailingMetadata error:error];
});
}
}
@ -489,10 +485,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
callOptions:(GRPCCallOptions *)callOptions {
// Purposely using pointer rather than length ([host length] == 0) for backwards compatibility.
NSAssert(host != nil && path != nil, @"Neither host nor path can be nil.");
NSAssert(safety <= GRPCCallSafetyCacheableRequest,
@"Invalid call safety value.");
NSAssert(safety <= GRPCCallSafetyCacheableRequest, @"Invalid call safety value.");
NSAssert(requestWriter.state == GRXWriterStateNotStarted,
@"The requests writer can't be already started.");
@"The requests writer can't be already started.");
if (!host || !path) {
return nil;
}
@ -888,7 +883,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
}
NSAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil,
@"authTokenProvider and oauth2AccessToken cannot be set at the same time");
@"authTokenProvider and oauth2AccessToken cannot be set at the same time");
if (_callOptions.authTokenProvider != nil) {
@synchronized(self) {
self.isWaitingForToken = YES;

@ -64,8 +64,7 @@ NS_ASSUME_NONNULL_BEGIN
* Create a channel with remote \a host and signature \a channelConfigurations.
*/
- (nullable instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration
NS_DESIGNATED_INITIALIZER;
(GRPCChannelConfiguration *)channelConfiguration NS_DESIGNATED_INITIALIZER;
/**
* Create a grpc core call object (grpc_call) from this channel. If no call is created, NULL is

@ -178,10 +178,8 @@
grpc_channel *_unmanagedChannel;
}
- (instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration {
NSAssert(channelConfiguration != nil,
@"channelConfiguration must not be empty.");
- (instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration {
NSAssert(channelConfiguration != nil, @"channelConfiguration must not be empty.");
if (channelConfiguration == nil) return nil;
if ((self = [super init])) {
@ -221,12 +219,11 @@
grpc_call *call = NULL;
@synchronized(self) {
NSAssert(_unmanagedChannel != NULL,
@"Channel should have valid unmanaged channel.");
NSAssert(_unmanagedChannel != NULL, @"Channel should have valid unmanaged channel.");
if (_unmanagedChannel == NULL) return NULL;
NSString *serverAuthority =
callOptions.transportType == GRPCTransportTypeCronet ? nil : callOptions.serverAuthority;
callOptions.transportType == GRPCTransportTypeCronet ? nil : callOptions.serverAuthority;
NSTimeInterval timeout = callOptions.timeout;
NSAssert(timeout >= 0, @"Invalid timeout");
if (timeout < 0) return NULL;
@ -236,10 +233,9 @@
}
grpc_slice path_slice = grpc_slice_from_copied_string(path.UTF8String);
gpr_timespec deadline_ms =
timeout == 0
? gpr_inf_future(GPR_CLOCK_REALTIME)
: gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN));
timeout == 0 ? gpr_inf_future(GPR_CLOCK_REALTIME)
: gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN));
call = grpc_channel_create_call(_unmanagedChannel, NULL, GRPC_PROPAGATE_DEFAULTS,
queue.unmanagedQueue, path_slice,
serverAuthority ? &host_slice : NULL, deadline_ms, NULL);

@ -42,10 +42,10 @@ NS_ASSUME_NONNULL_BEGIN
/**
* Initialize with an actual channel object \a channel and a reference to the channel pool.
*/
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration
- (nullable instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration
channelPool:(GRPCChannelPool *)channelPool;
/**
* Create a grpc core call object (grpc_call) from this channel. If channel is disconnected, get a
* new channel object from the channel pool.

@ -40,7 +40,7 @@ static dispatch_once_t gInitChannelPool;
/** When all calls of a channel are destroyed, destroy the channel after this much seconds. */
static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
@interface GRPCChannelPool()
@interface GRPCChannelPool ()
- (GRPCChannel *)refChannelWithConfiguration:(GRPCChannelConfiguration *)configuration;
@ -74,7 +74,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
}
- (grpc_call *)unmanagedCallWithPath:(NSString *)path
completionQueue:(GRPCCompletionQueue *)queue
completionQueue:(GRPCCompletionQueue *)queue
callOptions:(GRPCCallOptions *)callOptions {
NSAssert(path.length > 0, @"path must not be empty.");
NSAssert(queue != nil, @"completionQueue must not be empty.");
@ -90,7 +90,8 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
}
NSAssert(_wrappedChannel != nil, @"Unable to get a raw channel for proxy.");
}
call = [_wrappedChannel unmanagedCallWithPath:path completionQueue:queue callOptions:callOptions];
call =
[_wrappedChannel unmanagedCallWithPath:path completionQueue:queue callOptions:callOptions];
if (call != NULL) {
[_unmanagedCalls addObject:[NSValue valueWithPointer:call]];
}
@ -100,7 +101,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (void)unrefUnmanagedCall:(grpc_call *)unmanagedCall {
if (unmanagedCall == nil) return;
grpc_call_unref(unmanagedCall);
BOOL timedDestroy = NO;
@synchronized(self) {
@ -125,7 +126,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (GRPCChannel *)wrappedChannel {
GRPCChannel *channel = nil;
@synchronized (self) {
@synchronized(self) {
channel = _wrappedChannel;
}
return channel;
@ -148,7 +149,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
/**
* A convenience value type for cached channel.
*/
@interface GRPCChannelRecord : NSObject <NSCopying>
@interface GRPCChannelRecord : NSObject<NSCopying>
/** Pointer to the raw channel. May be nil when the channel has been destroyed. */
@property GRPCChannel *channel;
@ -197,14 +198,14 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
if (@available(iOS 8.0, macOS 10.10, *)) {
_dispatchQueue = dispatch_queue_create(
NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0));
NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0));
} else {
#else
{
{
#endif
_dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
}
_dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
}
_destroyDelay = kDefaultChannelDestroyDelay;
// Connectivity monitor is not required for CFStream
@ -221,7 +222,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
NSAssert(callOptions != nil, @"callOptions must not be empty.");
if (host.length == 0) return nil;
if (callOptions == nil) return nil;
GRPCPooledChannel *channelProxy = nil;
GRPCChannelConfiguration *configuration =
[[GRPCChannelConfiguration alloc] initWithHost:host callOptions:callOptions];
@ -229,8 +230,8 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
GRPCChannelRecord *record = _channelPool[configuration];
if (record == nil) {
record = [[GRPCChannelRecord alloc] init];
record.proxy = [[GRPCPooledChannel alloc] initWithChannelConfiguration:configuration
channelPool:self];
record.proxy =
[[GRPCPooledChannel alloc] initWithChannelConfiguration:configuration channelPool:self];
record.timedDestroyDate = nil;
_channelPool[configuration] = record;
channelProxy = record.proxy;
@ -247,7 +248,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (GRPCChannel *)refChannelWithConfiguration:(GRPCChannelConfiguration *)configuration {
GRPCChannel *ret = nil;
@synchronized (self) {
@synchronized(self) {
NSAssert(configuration != nil, @"configuration cannot be empty.");
if (configuration == nil) return nil;
@ -271,7 +272,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
}
- (void)unrefChannelWithConfiguration:(GRPCChannelConfiguration *)configuration {
@synchronized (self) {
@synchronized(self) {
GRPCChannelRecord *record = _channelPool[configuration];
NSAssert(record != nil, @"No record corresponding to a proxy.");
if (record == nil) return;
@ -282,9 +283,8 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
NSDate *now = [NSDate date];
record.timedDestroyDate = now;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_destroyDelay * NSEC_PER_SEC)),
_dispatchQueue,
^{
@synchronized (self) {
_dispatchQueue, ^{
@synchronized(self) {
if (now == record.timedDestroyDate) {
// Destroy the raw channel and reset related records.
record.timedDestroyDate = nil;
@ -292,7 +292,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
record.channel = nil;
}
}
});
});
}
}
}
@ -300,16 +300,18 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (void)disconnectAllChannels {
NSMutableSet<GRPCPooledChannel *> *proxySet = [NSMutableSet set];
@synchronized (self) {
[_channelPool enumerateKeysAndObjectsUsingBlock:^(GRPCChannelConfiguration * _Nonnull key, GRPCChannelRecord * _Nonnull obj, BOOL * _Nonnull stop) {
obj.channel = nil;
obj.timedDestroyDate = nil;
obj.refcount = 0;
[proxySet addObject:obj.proxy];
}];
@synchronized(self) {
[_channelPool
enumerateKeysAndObjectsUsingBlock:^(GRPCChannelConfiguration *_Nonnull key,
GRPCChannelRecord *_Nonnull obj, BOOL *_Nonnull stop) {
obj.channel = nil;
obj.timedDestroyDate = nil;
obj.refcount = 0;
[proxySet addObject:obj.proxy];
}];
}
// Disconnect proxies
[proxySet enumerateObjectsUsingBlock:^(GRPCPooledChannel * _Nonnull obj, BOOL * _Nonnull stop) {
[proxySet enumerateObjectsUsingBlock:^(GRPCPooledChannel *_Nonnull obj, BOOL *_Nonnull stop) {
[obj disconnect];
}];
}

@ -50,8 +50,7 @@
return self;
}
- (grpc_channel *)createChannelWithHost:(NSString *)host
channelArgs:(NSDictionary *)args {
- (grpc_channel *)createChannelWithHost:(NSString *)host channelArgs:(NSDictionary *)args {
grpc_channel_args *channelArgs = GRPCBuildChannelArgs(args);
grpc_channel *unmanagedChannel =
grpc_cronet_secure_channel_create(_cronetEngine, host.UTF8String, channelArgs, NULL);
@ -71,8 +70,7 @@
return nil;
}
- (grpc_channel *)createChannelWithHost:(NSString *)host
channelArgs:(NSDictionary *)args {
- (grpc_channel *)createChannelWithHost:(NSString *)host channelArgs:(NSDictionary *)args {
[NSException raise:NSInvalidArgumentException
format:@"Must enable macro GRPC_COMPILE_WITH_CRONET to build Cronet channel."];
return NULL;

@ -122,8 +122,7 @@ static NSMutableDictionary *gHostCache;
if (_transportType == GRPCTransportTypeInsecure) {
options.transportType = GRPCTransportTypeInsecure;
} else {
NSAssert(_transportType == GRPCTransportTypeDefault,
@"Invalid transport type");
NSAssert(_transportType == GRPCTransportTypeDefault, @"Invalid transport type");
options.transportType = GRPCTransportTypeCronet;
}
} else

@ -32,8 +32,7 @@
return instance;
}
- (grpc_channel *)createChannelWithHost:(NSString *)host
channelArgs:(NSDictionary *)args {
- (grpc_channel *)createChannelWithHost:(NSString *)host channelArgs:(NSDictionary *)args {
grpc_channel_args *coreChannelArgs = GRPCBuildChannelArgs([args copy]);
grpc_channel *unmanagedChannel =
grpc_insecure_channel_create(host.UTF8String, coreChannelArgs, NULL);

@ -29,9 +29,9 @@
}
+ (instancetype)factoryWithPEMRootCertificates:(NSString *)rootCerts
privateKey:(NSString *)privateKey
certChain:(NSString *)certChain
error:(NSError **)errorPtr {
privateKey:(NSString *)privateKey
certChain:(NSString *)certChain
error:(NSError **)errorPtr {
return [[self alloc] initWithPEMRootCerts:rootCerts
privateKey:privateKey
certChain:certChain
@ -50,9 +50,9 @@
}
- (instancetype)initWithPEMRootCerts:(NSString *)rootCerts
privateKey:(NSString *)privateKey
certChain:(NSString *)certChain
error:(NSError **)errorPtr {
privateKey:(NSString *)privateKey
certChain:(NSString *)certChain
error:(NSError **)errorPtr {
static NSData *defaultRootsASCII;
static NSError *defaultRootsError;
static dispatch_once_t loading;
@ -115,8 +115,7 @@
return self;
}
- (grpc_channel *)createChannelWithHost:(NSString *)host
channelArgs:(NSDictionary *)args {
- (grpc_channel *)createChannelWithHost:(NSString *)host channelArgs:(NSDictionary *)args {
grpc_channel_args *coreChannelArgs = GRPCBuildChannelArgs([args copy]);
grpc_channel *unmanagedChannel =
grpc_secure_channel_create(_channelCreds, host.UTF8String, coreChannelArgs, NULL);

@ -249,8 +249,7 @@
- (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
callOptions:(GRPCCallOptions *)callOptions {
NSAssert(host.length != 0 && path.length != 0,
@"path and host cannot be nil.");
NSAssert(host.length != 0 && path.length != 0, @"path and host cannot be nil.");
if ((self = [super init])) {
// Each completion queue consumes one thread. There's a trade to be made between creating and
@ -263,9 +262,7 @@
NSLog(@"Failed to get a channel for the host.");
return nil;
}
_call = [_channel unmanagedCallWithPath:path
completionQueue:_queue
callOptions:callOptions];
_call = [_channel unmanagedCallWithPath:path completionQueue:_queue callOptions:callOptions];
if (_call == nil) {
NSAssert(_channel != nil, @"Failed to get a channel for the host.");
NSLog(@"Failed to create a call.");

@ -19,5 +19,4 @@
#import <Foundation/Foundation.h>
/** Raise exception when condition not met. */
#define GRPCAssert(condition, errorString) \
NSAssert(condition, errorString)
#define GRPCAssert(condition, errorString) NSAssert(condition, errorString)

@ -138,21 +138,21 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
call = _call;
_call = nil;
if ([_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(_handler.dispatchQueue, ^{
id<GRPCProtoResponseHandler> handler = nil;
@synchronized(self) {
handler = self->_handler;
self->_handler = nil;
}
[handler closedWithTrailingMetadata:nil
error:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled
userInfo:@{
NSLocalizedDescriptionKey :
@"Canceled by app"
}]];
});
}
dispatch_async(_handler.dispatchQueue, ^{
id<GRPCProtoResponseHandler> handler = nil;
@synchronized(self) {
handler = self->_handler;
self->_handler = nil;
}
[handler closedWithTrailingMetadata:nil
error:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled
userInfo:@{
NSLocalizedDescriptionKey :
@"Canceled by app"
}]];
});
}
}
[call cancel];
}
@ -179,11 +179,11 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
}
- (void)receivedInitialMetadata:(NSDictionary *)initialMetadata {
@synchronized (self) {
@synchronized(self) {
if (initialMetadata != nil && [_handler respondsToSelector:@selector(initialMetadata:)]) {
dispatch_async(_dispatchQueue, ^{
id<GRPCProtoResponseHandler> handler = nil;
@synchronized (self) {
@synchronized(self) {
handler = self->_handler;
}
[handler receivedInitialMetadata:initialMetadata];
@ -197,19 +197,20 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
NSError *error = nil;
GPBMessage *parsed = [_responseClass parseFromData:message error:&error];
@synchronized (self) {
@synchronized(self) {
if (parsed && [_handler respondsToSelector:@selector(receivedProtoMessage:)]) {
dispatch_async(_dispatchQueue, ^{
id<GRPCProtoResponseHandler> handler = nil;
@synchronized (self) {
@synchronized(self) {
handler = self->_handler;
}
[handler receivedProtoMessage:parsed];
});
} else if (!parsed && [_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]){
} else if (!parsed &&
[_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(_dispatchQueue, ^{
id<GRPCProtoResponseHandler> handler = nil;
@synchronized (self) {
@synchronized(self) {
handler = self->_handler;
self->_handler = nil;
}
@ -222,13 +223,12 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
}
}
- (void)closedWithTrailingMetadata:(NSDictionary *)trailingMetadata
error:(NSError *)error {
@synchronized (self) {
- (void)closedWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
@synchronized(self) {
if ([_handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(_dispatchQueue, ^{
id<GRPCProtoResponseHandler> handler = nil;
@synchronized (self) {
@synchronized(self) {
handler = self->_handler;
self->_handler = nil;
}

@ -41,12 +41,12 @@ NSString *kDummyPath = @"/dummy/path";
- (void)testCreateChannelAndCall {
GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
GRPCCallOptions *options = [[GRPCCallOptions alloc] init];
GRPCPooledChannel *channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options];
GRPCPooledChannel *channel =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options];
XCTAssertNil(channel.wrappedChannel);
GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
grpc_call *call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq callOptions:options];
grpc_call *call =
[channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
XCTAssert(call != NULL);
XCTAssertNotNil(channel.wrappedChannel);
[channel unrefUnmanagedCall:call];
@ -60,26 +60,22 @@ NSString *kDummyPath = @"/dummy/path";
GRPCMutableCallOptions *options3 = [options1 mutableCopy];
options3.transportType = GRPCTransportTypeInsecure;
GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
GRPCPooledChannel *channel1 = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options1];
grpc_call *call1 = [channel1 unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options1];
GRPCPooledChannel *channel2 = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options2];
grpc_call *call2 = [channel2 unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options2];
GRPCPooledChannel *channel3 = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options3];
grpc_call *call3 = [channel3 unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options3];
GRPCPooledChannel *channel4 = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost2
callOptions:options1];
grpc_call *call4 = [channel4 unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options1];
GRPCPooledChannel *channel1 =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options1];
grpc_call *call1 =
[channel1 unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options1];
GRPCPooledChannel *channel2 =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options2];
grpc_call *call2 =
[channel2 unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options2];
GRPCPooledChannel *channel3 =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options3];
grpc_call *call3 =
[channel3 unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options3];
GRPCPooledChannel *channel4 =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost2 callOptions:options1];
grpc_call *call4 =
[channel4 unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options1];
XCTAssertEqual(channel1.wrappedChannel, channel2.wrappedChannel);
XCTAssertNotEqual(channel1.wrappedChannel, channel3.wrappedChannel);
XCTAssertNotEqual(channel1.wrappedChannel, channel4.wrappedChannel);
@ -96,32 +92,26 @@ NSString *kDummyPath = @"/dummy/path";
GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
pool.destroyDelay = kDestroyDelay;
GRPCCallOptions *options = [[GRPCCallOptions alloc] init];
GRPCPooledChannel *channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options];
GRPCPooledChannel *channel =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options];
GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
grpc_call *call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq callOptions:options];
grpc_call *call =
[channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
GRPCChannel *wrappedChannel = channel.wrappedChannel;
[channel unrefUnmanagedCall:call];
// Confirm channel is not destroyed at this time
call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
call = [channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
XCTAssertEqual(wrappedChannel, channel.wrappedChannel);
[channel unrefUnmanagedCall:call];
sleep(kDestroyDelay + 1);
// Confirm channel is new at this time
call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
call = [channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
XCTAssertNotEqual(wrappedChannel, channel.wrappedChannel);
// Confirm the new channel can create call
call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
call = [channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
XCTAssert(call != NULL);
[channel unrefUnmanagedCall:call];
}
@ -129,31 +119,26 @@ NSString *kDummyPath = @"/dummy/path";
- (void)testPoolDisconnection {
GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
GRPCCallOptions *options = [[GRPCCallOptions alloc] init];
GRPCPooledChannel *channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options];
GRPCPooledChannel *channel =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options];
GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
grpc_call *call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
grpc_call *call =
[channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
XCTAssertNotNil(channel.wrappedChannel);
GRPCChannel *wrappedChannel = channel.wrappedChannel;
// Test a new channel is created by requesting a channel from pool
[pool disconnectAllChannels];
channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options];
call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options];
call = [channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
XCTAssertNotNil(channel.wrappedChannel);
XCTAssertNotEqual(wrappedChannel, channel.wrappedChannel);
wrappedChannel = channel.wrappedChannel;
// Test a new channel is created by requesting a new call from the previous proxy
[pool disconnectAllChannels];
grpc_call *call2 = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
grpc_call *call2 =
[channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
XCTAssertNotNil(channel.wrappedChannel);
XCTAssertNotEqual(channel.wrappedChannel, wrappedChannel);
[channel unrefUnmanagedCall:call];
@ -163,20 +148,17 @@ NSString *kDummyPath = @"/dummy/path";
- (void)testUnrefCallFromStaleChannel {
GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
GRPCCallOptions *options = [[GRPCCallOptions alloc] init];
GRPCPooledChannel *channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options];
GRPCPooledChannel *channel =
(GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options];
GRPCCompletionQueue *cq = [GRPCCompletionQueue completionQueue];
grpc_call *call = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
grpc_call *call =
[channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
[pool disconnectAllChannels];
channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost
callOptions:options];
channel = (GRPCPooledChannel *)[pool channelWithHost:kDummyHost callOptions:options];
grpc_call *call2 = [channel unmanagedCallWithPath:kDummyPath
completionQueue:cq
callOptions:options];
grpc_call *call2 =
[channel unmanagedCallWithPath:kDummyPath completionQueue:cq callOptions:options];
// Test unref the call of a stale channel will not cause the current channel going into timed
// destroy state
XCTAssertNotNil(channel.wrappedChannel);

@ -45,8 +45,8 @@
long _grpcCallCounter;
}
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration {
return nil;
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration
*)channelConfiguration { return nil;
}
- (instancetype)initWithCreateExpectation:(XCTestExpectation *)createExpectation

@ -114,8 +114,7 @@ BOOL isRemoteInteropTest(NSString *host) {
}
}
- (void)closedWithTrailingMetadata:(NSDictionary *)trailingMetadata
error:(NSError *)error {
- (void)closedWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
if (_closeCallback) {
_closeCallback(trailingMetadata, error);
}

Loading…
Cancel
Save