clang-format

pull/16190/head
Muxi Yan 6 years ago
parent 6ae2ea643d
commit 4264ea2b55
  1. 10
      src/objective-c/GRPCClient/GRPCCall.h
  2. 34
      src/objective-c/GRPCClient/GRPCCall.m
  3. 26
      src/objective-c/GRPCClient/GRPCCallOptions.m
  4. 36
      src/objective-c/GRPCClient/private/GRPCChannel.m
  5. 35
      src/objective-c/GRPCClient/private/GRPCChannelPool.m
  6. 6
      src/objective-c/ProtoRPC/ProtoRPC.h
  7. 10
      src/objective-c/ProtoRPC/ProtoRPC.m
  8. 13
      src/objective-c/ProtoRPC/ProtoService.h
  9. 35
      src/objective-c/tests/ChannelTests/ChannelPoolTest.m
  10. 101
      src/objective-c/tests/GRPCClientTests.m
  11. 2
      src/objective-c/tests/InteropTests.m

@ -149,7 +149,7 @@ extern id const kGRPCHeadersKey;
extern id const kGRPCTrailersKey; extern id const kGRPCTrailersKey;
/** An object can implement this protocol to receive responses from server from a call. */ /** An object can implement this protocol to receive responses from server from a call. */
@protocol GRPCResponseHandler <NSObject> @protocol GRPCResponseHandler<NSObject>
@optional @optional
@ -188,10 +188,12 @@ extern id const kGRPCTrailersKey;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype) new NS_UNAVAILABLE;
/** Initialize with all properties. */ /** Initialize with all properties. */
- (instancetype)initWithHost:(NSString *)host path:(NSString *)path safety:(GRPCCallSafety)safety NS_DESIGNATED_INITIALIZER; - (instancetype)initWithHost:(NSString *)host
path:(NSString *)path
safety:(GRPCCallSafety)safety NS_DESIGNATED_INITIALIZER;
/** The host serving the RPC service. */ /** The host serving the RPC service. */
@property(copy, readonly) NSString *host; @property(copy, readonly) NSString *host;
@ -214,7 +216,7 @@ extern id const kGRPCTrailersKey;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype) new NS_UNAVAILABLE;
/** /**
* Designated initializer for a call. * Designated initializer for a call.

@ -28,8 +28,8 @@
#include <grpc/support/time.h> #include <grpc/support/time.h>
#import "GRPCCallOptions.h" #import "GRPCCallOptions.h"
#import "private/GRPCHost.h"
#import "private/GRPCConnectivityMonitor.h" #import "private/GRPCConnectivityMonitor.h"
#import "private/GRPCHost.h"
#import "private/GRPCRequestHeaders.h" #import "private/GRPCRequestHeaders.h"
#import "private/GRPCWrappedCall.h" #import "private/GRPCWrappedCall.h"
#import "private/NSData+GRPC.h" #import "private/NSData+GRPC.h"
@ -115,7 +115,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
_initialMetadataPublished = NO; _initialMetadataPublished = NO;
_pipe = [GRXBufferedPipe pipe]; _pipe = [GRXBufferedPipe pipe];
if (@available(iOS 8.0, *)) { if (@available(iOS 8.0, *)) {
_dispatchQueue = dispatch_queue_create(NULL, dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1)); _dispatchQueue = dispatch_queue_create(
NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1));
} else { } else {
// Fallback on earlier versions // Fallback on earlier versions
_dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); _dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
@ -129,7 +131,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions - (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions
responseHandler:(id<GRPCResponseHandler>)responseHandler { responseHandler:(id<GRPCResponseHandler>)responseHandler {
return [self initWithRequestOptions:requestOptions responseHandler:responseHandler callOptions:nil]; return
[self initWithRequestOptions:requestOptions responseHandler:responseHandler callOptions:nil];
} }
- (void)start { - (void)start {
@ -210,9 +213,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
error:[NSError errorWithDomain:kGRPCErrorDomain error:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled code:GRPCErrorCodeCancelled
userInfo:@{ userInfo:@{
NSLocalizedDescriptionKey : NSLocalizedDescriptionKey :
@"Canceled by app" @"Canceled by app"
}]]; }]];
} }
}); });
@ -255,13 +258,12 @@ const char *kCFStreamVarName = "grpc_cfstream";
} }
} }
- (void)issueClosedWithTrailingMetadata:(NSDictionary *)trailingMetadata - (void)issueClosedWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
error:(NSError *)error {
id<GRPCResponseHandler> handler = _handler; id<GRPCResponseHandler> handler = _handler;
NSDictionary *trailers = _call.responseTrailers; NSDictionary *trailers = _call.responseTrailers;
if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) { if ([handler respondsToSelector:@selector(closedWithTrailingMetadata:error:)]) {
dispatch_async(handler.dispatchQueue, ^{ dispatch_async(handler.dispatchQueue, ^{
[handler closedWithTrailingMetadata:trailers error:error]; [handler closedWithTrailingMetadata:trailers error:error];
}); });
} }
} }
@ -388,7 +390,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
requestsWriter:(GRXWriter *)requestWriter requestsWriter:(GRXWriter *)requestWriter
callOptions:(GRPCCallOptions *)callOptions { callOptions:(GRPCCallOptions *)callOptions {
if (host.length == 0 || path.length == 0) { if (host.length == 0 || path.length == 0) {
[NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil or empty."]; [NSException raise:NSInvalidArgumentException
format:@"Neither host nor path can be nil or empty."];
} }
if (safety > GRPCCallSafetyCacheableRequest) { if (safety > GRPCCallSafetyCacheableRequest) {
[NSException raise:NSInvalidArgumentException format:@"Invalid call safety value."]; [NSException raise:NSInvalidArgumentException format:@"Invalid call safety value."];
@ -457,7 +460,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
} }
- (void)cancel { - (void)cancel {
@synchronized (self) { @synchronized(self) {
if (!self.isWaitingForToken) { if (!self.isWaitingForToken) {
[self cancelCall]; [self cancelCall];
} else { } else {
@ -720,8 +723,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
[self maybeFinishWithError:[NSError errorWithDomain:kGRPCErrorDomain [self maybeFinishWithError:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeUnavailable code:GRPCErrorCodeUnavailable
userInfo:@{ userInfo:@{
NSLocalizedDescriptionKey : @"Failed to create call or channel." NSLocalizedDescriptionKey :
}]]; @"Failed to create call or channel."
}]];
return; return;
} }
@ -773,11 +777,11 @@ const char *kCFStreamVarName = "grpc_cfstream";
_callOptions = callOptions; _callOptions = callOptions;
} }
if (_callOptions.authTokenProvider != nil) { if (_callOptions.authTokenProvider != nil) {
@synchronized (self) { @synchronized(self) {
self.isWaitingForToken = YES; self.isWaitingForToken = YES;
} }
[self.tokenProvider getTokenWithHandler:^(NSString *token) { [self.tokenProvider getTokenWithHandler:^(NSString *token) {
@synchronized (self) { @synchronized(self) {
if (self.isWaitingForToken) { if (self.isWaitingForToken) {
if (token) { if (token) {
self->_fetchedOauth2AccessToken = [token copy]; self->_fetchedOauth2AccessToken = [token copy];

@ -110,7 +110,7 @@ static NSUInteger kDefaultChannelID = 0;
connectInitialBackoff:kDefaultConnectInitialBackoff connectInitialBackoff:kDefaultConnectInitialBackoff
connectMaxBackoff:kDefaultConnectMaxBackoff connectMaxBackoff:kDefaultConnectMaxBackoff
additionalChannelArgs:kDefaultAdditionalChannelArgs additionalChannelArgs:kDefaultAdditionalChannelArgs
PEMRootCertificates:kDefaultPEMRootCertificates PEMRootCertificates:kDefaultPEMRootCertificates
PEMPrivateKey:kDefaultPEMPrivateKey PEMPrivateKey:kDefaultPEMPrivateKey
PEMCertChain:kDefaultPEMCertChain PEMCertChain:kDefaultPEMCertChain
transportType:kDefaultTransportType transportType:kDefaultTransportType
@ -135,7 +135,7 @@ static NSUInteger kDefaultChannelID = 0;
connectInitialBackoff:(NSTimeInterval)connectInitialBackoff connectInitialBackoff:(NSTimeInterval)connectInitialBackoff
connectMaxBackoff:(NSTimeInterval)connectMaxBackoff connectMaxBackoff:(NSTimeInterval)connectMaxBackoff
additionalChannelArgs:(NSDictionary *)additionalChannelArgs additionalChannelArgs:(NSDictionary *)additionalChannelArgs
PEMRootCertificates:(NSString *)PEMRootCertificates PEMRootCertificates:(NSString *)PEMRootCertificates
PEMPrivateKey:(NSString *)PEMPrivateKey PEMPrivateKey:(NSString *)PEMPrivateKey
PEMCertChain:(NSString *)PEMCertChain PEMCertChain:(NSString *)PEMCertChain
transportType:(GRPCTransportType)transportType transportType:(GRPCTransportType)transportType
@ -158,7 +158,8 @@ static NSUInteger kDefaultChannelID = 0;
_connectMinTimeout = connectMinTimeout; _connectMinTimeout = connectMinTimeout;
_connectInitialBackoff = connectInitialBackoff; _connectInitialBackoff = connectInitialBackoff;
_connectMaxBackoff = connectMaxBackoff; _connectMaxBackoff = connectMaxBackoff;
_additionalChannelArgs = [[NSDictionary alloc] initWithDictionary:additionalChannelArgs copyItems:YES]; _additionalChannelArgs =
[[NSDictionary alloc] initWithDictionary:additionalChannelArgs copyItems:YES];
_PEMRootCertificates = [PEMRootCertificates copy]; _PEMRootCertificates = [PEMRootCertificates copy];
_PEMPrivateKey = [PEMPrivateKey copy]; _PEMPrivateKey = [PEMPrivateKey copy];
_PEMCertChain = [PEMCertChain copy]; _PEMCertChain = [PEMCertChain copy];
@ -188,7 +189,7 @@ static NSUInteger kDefaultChannelID = 0;
connectInitialBackoff:_connectInitialBackoff connectInitialBackoff:_connectInitialBackoff
connectMaxBackoff:_connectMaxBackoff connectMaxBackoff:_connectMaxBackoff
additionalChannelArgs:_additionalChannelArgs additionalChannelArgs:_additionalChannelArgs
PEMRootCertificates:_PEMRootCertificates PEMRootCertificates:_PEMRootCertificates
PEMPrivateKey:_PEMPrivateKey PEMPrivateKey:_PEMPrivateKey
PEMCertChain:_PEMCertChain PEMCertChain:_PEMCertChain
transportType:_transportType transportType:_transportType
@ -216,7 +217,7 @@ static NSUInteger kDefaultChannelID = 0;
connectInitialBackoff:_connectInitialBackoff connectInitialBackoff:_connectInitialBackoff
connectMaxBackoff:_connectMaxBackoff connectMaxBackoff:_connectMaxBackoff
additionalChannelArgs:[_additionalChannelArgs copy] additionalChannelArgs:[_additionalChannelArgs copy]
PEMRootCertificates:_PEMRootCertificates PEMRootCertificates:_PEMRootCertificates
PEMPrivateKey:_PEMPrivateKey PEMPrivateKey:_PEMPrivateKey
PEMCertChain:_PEMCertChain PEMCertChain:_PEMCertChain
transportType:_transportType transportType:_transportType
@ -240,8 +241,7 @@ static NSUInteger kDefaultChannelID = 0;
if (!(callOptions.connectInitialBackoff == _connectInitialBackoff)) return NO; if (!(callOptions.connectInitialBackoff == _connectInitialBackoff)) return NO;
if (!(callOptions.connectMaxBackoff == _connectMaxBackoff)) return NO; if (!(callOptions.connectMaxBackoff == _connectMaxBackoff)) return NO;
if (!(callOptions.additionalChannelArgs == _additionalChannelArgs || if (!(callOptions.additionalChannelArgs == _additionalChannelArgs ||
[callOptions.additionalChannelArgs [callOptions.additionalChannelArgs isEqualToDictionary:_additionalChannelArgs]))
isEqualToDictionary:_additionalChannelArgs]))
return NO; return NO;
if (!(callOptions.PEMRootCertificates == _PEMRootCertificates || if (!(callOptions.PEMRootCertificates == _PEMRootCertificates ||
[callOptions.PEMRootCertificates isEqualToString:_PEMRootCertificates])) [callOptions.PEMRootCertificates isEqualToString:_PEMRootCertificates]))
@ -256,8 +256,7 @@ static NSUInteger kDefaultChannelID = 0;
[callOptions.hostNameOverride isEqualToString:_hostNameOverride])) [callOptions.hostNameOverride isEqualToString:_hostNameOverride]))
return NO; return NO;
if (!(callOptions.transportType == _transportType)) return NO; if (!(callOptions.transportType == _transportType)) return NO;
if (!(callOptions.logContext == _logContext || if (!(callOptions.logContext == _logContext || [callOptions.logContext isEqual:_logContext]))
[callOptions.logContext isEqual:_logContext]))
return NO; return NO;
if (!(callOptions.channelPoolDomain == _channelPoolDomain || if (!(callOptions.channelPoolDomain == _channelPoolDomain ||
[callOptions.channelPoolDomain isEqualToString:_channelPoolDomain])) [callOptions.channelPoolDomain isEqualToString:_channelPoolDomain]))
@ -335,7 +334,7 @@ static NSUInteger kDefaultChannelID = 0;
connectInitialBackoff:kDefaultConnectInitialBackoff connectInitialBackoff:kDefaultConnectInitialBackoff
connectMaxBackoff:kDefaultConnectMaxBackoff connectMaxBackoff:kDefaultConnectMaxBackoff
additionalChannelArgs:kDefaultAdditionalChannelArgs additionalChannelArgs:kDefaultAdditionalChannelArgs
PEMRootCertificates:kDefaultPEMRootCertificates PEMRootCertificates:kDefaultPEMRootCertificates
PEMPrivateKey:kDefaultPEMPrivateKey PEMPrivateKey:kDefaultPEMPrivateKey
PEMCertChain:kDefaultPEMCertChain PEMCertChain:kDefaultPEMCertChain
transportType:kDefaultTransportType transportType:kDefaultTransportType
@ -362,7 +361,7 @@ static NSUInteger kDefaultChannelID = 0;
connectInitialBackoff:_connectInitialBackoff connectInitialBackoff:_connectInitialBackoff
connectMaxBackoff:_connectMaxBackoff connectMaxBackoff:_connectMaxBackoff
additionalChannelArgs:[_additionalChannelArgs copy] additionalChannelArgs:[_additionalChannelArgs copy]
PEMRootCertificates:_PEMRootCertificates PEMRootCertificates:_PEMRootCertificates
PEMPrivateKey:_PEMPrivateKey PEMPrivateKey:_PEMPrivateKey
PEMCertChain:_PEMCertChain PEMCertChain:_PEMCertChain
transportType:_transportType transportType:_transportType
@ -390,7 +389,7 @@ static NSUInteger kDefaultChannelID = 0;
connectInitialBackoff:_connectInitialBackoff connectInitialBackoff:_connectInitialBackoff
connectMaxBackoff:_connectMaxBackoff connectMaxBackoff:_connectMaxBackoff
additionalChannelArgs:[_additionalChannelArgs copy] additionalChannelArgs:[_additionalChannelArgs copy]
PEMRootCertificates:_PEMRootCertificates PEMRootCertificates:_PEMRootCertificates
PEMPrivateKey:_PEMPrivateKey PEMPrivateKey:_PEMPrivateKey
PEMCertChain:_PEMCertChain PEMCertChain:_PEMCertChain
transportType:_transportType transportType:_transportType
@ -482,7 +481,8 @@ static NSUInteger kDefaultChannelID = 0;
} }
- (void)setAdditionalChannelArgs:(NSDictionary *)additionalChannelArgs { - (void)setAdditionalChannelArgs:(NSDictionary *)additionalChannelArgs {
_additionalChannelArgs = [[NSDictionary alloc] initWithDictionary:additionalChannelArgs copyItems:YES]; _additionalChannelArgs =
[[NSDictionary alloc] initWithDictionary:additionalChannelArgs copyItems:YES];
} }
- (void)setPEMRootCertificates:(NSString *)PEMRootCertificates { - (void)setPEMRootCertificates:(NSString *)PEMRootCertificates {

@ -75,8 +75,12 @@ NSTimeInterval kChannelDestroyDelay = 30;
_refCount = 1; _refCount = 1;
_disconnected = NO; _disconnected = NO;
if (@available(iOS 8.0, *)) { if (@available(iOS 8.0, *)) {
_dispatchQueue = dispatch_queue_create(NULL, dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1)); _dispatchQueue = dispatch_queue_create(
_timerQueue = dispatch_queue_create(NULL, dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_DEFAULT, -1)); NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1));
_timerQueue =
dispatch_queue_create(NULL, dispatch_queue_attr_make_with_qos_class(
DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_DEFAULT, -1));
} else { } else {
_dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); _dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
_timerQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT); _timerQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
@ -101,7 +105,8 @@ NSTimeInterval kChannelDestroyDelay = 30;
self->_refCount--; self->_refCount--;
if (self->_refCount == 0) { if (self->_refCount == 0) {
self->_lastDispatch = [NSDate date]; self->_lastDispatch = [NSDate date];
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)kChannelDestroyDelay * 1e9); dispatch_time_t delay =
dispatch_time(DISPATCH_TIME_NOW, (int64_t)kChannelDestroyDelay * 1e9);
dispatch_after(delay, self->_timerQueue, ^{ dispatch_after(delay, self->_timerQueue, ^{
[self timerFire]; [self timerFire];
}); });
@ -123,7 +128,8 @@ NSTimeInterval kChannelDestroyDelay = 30;
- (void)timerFire { - (void)timerFire {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
if (self->_disconnected || self->_lastDispatch == nil || -[self->_lastDispatch timeIntervalSinceNow] < -kChannelDestroyDelay) { if (self->_disconnected || self->_lastDispatch == nil ||
-[self->_lastDispatch timeIntervalSinceNow] < -kChannelDestroyDelay) {
return; return;
} }
self->_lastDispatch = nil; self->_lastDispatch = nil;
@ -158,11 +164,12 @@ NSTimeInterval kChannelDestroyDelay = 30;
} }
grpc_slice path_slice = grpc_slice_from_copied_string(path.UTF8String); grpc_slice path_slice = grpc_slice_from_copied_string(path.UTF8String);
gpr_timespec deadline_ms = gpr_timespec deadline_ms =
timeout == 0 ? gpr_inf_future(GPR_CLOCK_REALTIME) timeout == 0
: gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), ? gpr_inf_future(GPR_CLOCK_REALTIME)
gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN)); : gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
call = grpc_channel_create_call( gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN));
self->_unmanagedChannel, NULL, GRPC_PROPAGATE_DEFAULTS, queue.unmanagedQueue, path_slice, call = grpc_channel_create_call(self->_unmanagedChannel, NULL, GRPC_PROPAGATE_DEFAULTS,
queue.unmanagedQueue, path_slice,
serverAuthority ? &host_slice : NULL, deadline_ms, NULL); serverAuthority ? &host_slice : NULL, deadline_ms, NULL);
if (serverAuthority) { if (serverAuthority) {
grpc_slice_unref(host_slice); grpc_slice_unref(host_slice);
@ -214,11 +221,14 @@ NSTimeInterval kChannelDestroyDelay = 30;
if ((self = [super init])) { if ((self = [super init])) {
_unmanagedChannel = unmanagedChannel; _unmanagedChannel = unmanagedChannel;
_configuration = configuration; _configuration = configuration;
_channelRef = [[GRPCChannelRef alloc] initWithDestroyDelay:kChannelDestroyDelay destroyChannelCallback:^{ _channelRef = [[GRPCChannelRef alloc] initWithDestroyDelay:kChannelDestroyDelay
[self destroyChannel]; destroyChannelCallback:^{
}]; [self destroyChannel];
}];
if (@available(iOS 8.0, *)) { if (@available(iOS 8.0, *)) {
_dispatchQueue = dispatch_queue_create(NULL, dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1)); _dispatchQueue = dispatch_queue_create(
NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1));
} else { } else {
_dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); _dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
} }

@ -52,10 +52,11 @@ extern const char *kCFStreamVarName;
#ifdef GRPC_COMPILE_WITH_CRONET #ifdef GRPC_COMPILE_WITH_CRONET
if (![GRPCCall isUsingCronet]) { if (![GRPCCall isUsingCronet]) {
#endif #endif
factory = [GRPCSecureChannelFactory factoryWithPEMRootCertificates:_callOptions.PEMRootCertificates factory = [GRPCSecureChannelFactory
privateKey:_callOptions.PEMPrivateKey factoryWithPEMRootCertificates:_callOptions.PEMRootCertificates
certChain:_callOptions.PEMCertChain privateKey:_callOptions.PEMPrivateKey
error:&error]; certChain:_callOptions.PEMCertChain
error:&error];
if (factory == nil) { if (factory == nil) {
NSLog(@"Error creating secure channel factory: %@", error); NSLog(@"Error creating secure channel factory: %@", error);
} }
@ -136,7 +137,8 @@ extern const char *kCFStreamVarName;
} }
- (nonnull id)copyWithZone:(nullable NSZone *)zone { - (nonnull id)copyWithZone:(nullable NSZone *)zone {
GRPCChannelConfiguration *newConfig = [[GRPCChannelConfiguration alloc] initWithHost:_host callOptions:_callOptions]; GRPCChannelConfiguration *newConfig =
[[GRPCChannelConfiguration alloc] initWithHost:_host callOptions:_callOptions];
return newConfig; return newConfig;
} }
@ -145,7 +147,8 @@ extern const char *kCFStreamVarName;
NSAssert([object isKindOfClass:[GRPCChannelConfiguration class]], @"Illegal :isEqual"); NSAssert([object isKindOfClass:[GRPCChannelConfiguration class]], @"Illegal :isEqual");
GRPCChannelConfiguration *obj = (GRPCChannelConfiguration *)object; GRPCChannelConfiguration *obj = (GRPCChannelConfiguration *)object;
if (!(obj.host == _host || [obj.host isEqualToString:_host])) return NO; if (!(obj.host == _host || [obj.host isEqualToString:_host])) return NO;
if (!(obj.callOptions == _callOptions || [obj.callOptions isChannelOptionsEqualTo:_callOptions])) return NO; if (!(obj.callOptions == _callOptions || [obj.callOptions isChannelOptionsEqualTo:_callOptions]))
return NO;
return YES; return YES;
} }
@ -201,11 +204,13 @@ extern const char *kCFStreamVarName;
- (void)removeChannel:(GRPCChannel *)channel { - (void)removeChannel:(GRPCChannel *)channel {
@synchronized(self) { @synchronized(self) {
[_channelPool enumerateKeysAndObjectsUsingBlock:^(GRPCChannelConfiguration * _Nonnull key, GRPCChannel * _Nonnull obj, BOOL * _Nonnull stop) { [_channelPool
if (obj == channel) { enumerateKeysAndObjectsUsingBlock:^(GRPCChannelConfiguration *_Nonnull key,
[self->_channelPool removeObjectForKey:key]; GRPCChannel *_Nonnull obj, BOOL *_Nonnull stop) {
} if (obj == channel) {
}]; [self->_channelPool removeObjectForKey:key];
}
}];
} }
} }
@ -217,9 +222,11 @@ extern const char *kCFStreamVarName;
- (void)removeAndCloseAllChannels { - (void)removeAndCloseAllChannels {
@synchronized(self) { @synchronized(self) {
[_channelPool enumerateKeysAndObjectsUsingBlock:^(GRPCChannelConfiguration * _Nonnull key, GRPCChannel * _Nonnull obj, BOOL * _Nonnull stop) { [_channelPool
[obj disconnect]; enumerateKeysAndObjectsUsingBlock:^(GRPCChannelConfiguration *_Nonnull key,
}]; GRPCChannel *_Nonnull obj, BOOL *_Nonnull stop) {
[obj disconnect];
}];
_channelPool = [NSMutableDictionary dictionary]; _channelPool = [NSMutableDictionary dictionary];
} }
} }

@ -24,7 +24,7 @@
@class GPBMessage; @class GPBMessage;
/** An object can implement this protocol to receive responses from server from a call. */ /** An object can implement this protocol to receive responses from server from a call. */
@protocol GRPCProtoResponseHandler <NSObject> @protocol GRPCProtoResponseHandler<NSObject>
@optional @optional
@ -59,7 +59,7 @@
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype) new NS_UNAVAILABLE;
/** /**
* Users should not use this initializer directly. Call objects will be created, initialized, and * Users should not use this initializer directly. Call objects will be created, initialized, and
@ -81,7 +81,7 @@
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype) new NS_UNAVAILABLE;
/** /**
* Users should not use this initializer directly. Call objects will be created, initialized, and * Users should not use this initializer directly. Call objects will be created, initialized, and

@ -88,7 +88,9 @@
_callOptions = [callOptions copy]; _callOptions = [callOptions copy];
_responseClass = responseClass; _responseClass = responseClass;
if (@available(iOS 8.0, *)) { if (@available(iOS 8.0, *)) {
_dispatchQueue = dispatch_queue_create(NULL, dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1)); _dispatchQueue = dispatch_queue_create(
NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1));
} else { } else {
_dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL); _dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL);
} }
@ -120,9 +122,9 @@
error:[NSError errorWithDomain:kGRPCErrorDomain error:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled code:GRPCErrorCodeCancelled
userInfo:@{ userInfo:@{
NSLocalizedDescriptionKey : NSLocalizedDescriptionKey :
@"Canceled by app" @"Canceled by app"
}]]; }]];
}); });
} }
_handler = nil; _handler = nil;

@ -30,14 +30,15 @@
__attribute__((deprecated("Please use GRPCProtoService."))) @interface ProtoService __attribute__((deprecated("Please use GRPCProtoService."))) @interface ProtoService
: NSObject : NSObject
- (instancetype)init NS_UNAVAILABLE; -
(instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype) new NS_UNAVAILABLE;
- - (instancetype)initWithHost:(NSString *)host
(instancetype)initWithHost : (NSString *)host packageName packageName:(NSString *)packageName
: (NSString *)packageName serviceName : (NSString *)serviceName callOptions serviceName:(NSString *)serviceName
: (GRPCCallOptions *)callOptions NS_DESIGNATED_INITIALIZER; callOptions:(GRPCCallOptions *)callOptions NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithHost:(NSString *)host - (instancetype)initWithHost:(NSString *)host
packageName:(NSString *)packageName packageName:(NSString *)packageName

@ -57,11 +57,9 @@ NSString *kDummyHost = @"dummy.host";
GRPCChannelConfiguration *config1 = GRPCChannelConfiguration *config1 =
[[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options1]; [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options1];
GRPCChannelPool *pool = [[GRPCChannelPool alloc] init]; GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
GRPCChannel *channel1 = GRPCChannel *channel1 = [pool channelWithConfiguration:config1];
[pool channelWithConfiguration:config1];
[pool removeChannel:channel1]; [pool removeChannel:channel1];
GRPCChannel *channel2 = GRPCChannel *channel2 = [pool channelWithConfiguration:config1];
[pool channelWithConfiguration:config1];
XCTAssertNotEqual(channel1, channel2); XCTAssertNotEqual(channel1, channel2);
} }
@ -74,18 +72,14 @@ extern NSTimeInterval kChannelDestroyDelay;
options1.transportType = GRPCTransportTypeInsecure; options1.transportType = GRPCTransportTypeInsecure;
GRPCChannelConfiguration *config1 = GRPCChannelConfiguration *config1 =
[[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options1]; [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options1];
GRPCChannelPool *pool = GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
[[GRPCChannelPool alloc] init]; GRPCChannel *channel1 = [pool channelWithConfiguration:config1];
GRPCChannel *channel1 =
[pool channelWithConfiguration:config1];
[channel1 unmanagedCallUnref]; [channel1 unmanagedCallUnref];
sleep(1); sleep(1);
GRPCChannel *channel2 = GRPCChannel *channel2 = [pool channelWithConfiguration:config1];
[pool channelWithConfiguration:config1];
XCTAssertEqual(channel1, channel2); XCTAssertEqual(channel1, channel2);
sleep((int)kChannelDestroyDelay + 2); sleep((int)kChannelDestroyDelay + 2);
GRPCChannel *channel3 = GRPCChannel *channel3 = [pool channelWithConfiguration:config1];
[pool channelWithConfiguration:config1];
XCTAssertEqual(channel1, channel3); XCTAssertEqual(channel1, channel3);
kChannelDestroyDelay = kOriginalInterval; kChannelDestroyDelay = kOriginalInterval;
} }
@ -96,12 +90,11 @@ extern NSTimeInterval kChannelDestroyDelay;
options1.transportType = GRPCTransportTypeInsecure; options1.transportType = GRPCTransportTypeInsecure;
GRPCCallOptions *options2 = [options1 copy]; GRPCCallOptions *options2 = [options1 copy];
GRPCChannelConfiguration *config1 = GRPCChannelConfiguration *config1 =
[[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options1]; [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options1];
GRPCChannelConfiguration *config2 = GRPCChannelConfiguration *config2 =
[[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options2]; [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options2];
GRPCChannelPool *pool = [[GRPCChannelPool alloc] init]; GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
GRPCChannel *channel1 = [pool channelWithConfiguration:config1]; GRPCChannel *channel1 = [pool channelWithConfiguration:config1];
[pool removeAndCloseAllChannels]; [pool removeAndCloseAllChannels];
GRPCChannel *channel2 = [pool channelWithConfiguration:config2]; GRPCChannel *channel2 = [pool channelWithConfiguration:config2];
@ -119,17 +112,13 @@ extern NSTimeInterval kChannelDestroyDelay;
[[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options2]; [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options2];
GRPCChannelPool *pool = [[GRPCChannelPool alloc] init]; GRPCChannelPool *pool = [[GRPCChannelPool alloc] init];
GRPCChannel *channel1 = GRPCChannel *channel1 = [pool channelWithConfiguration:config1];
[pool channelWithConfiguration:config1]; GRPCChannel *channel2 = [pool channelWithConfiguration:config2];
GRPCChannel *channel2 =
[pool channelWithConfiguration:config2];
XCTAssertNotEqual(channel1, channel2); XCTAssertNotEqual(channel1, channel2);
[pool removeAndCloseAllChannels]; [pool removeAndCloseAllChannels];
GRPCChannel *channel3 = GRPCChannel *channel3 = [pool channelWithConfiguration:config1];
[pool channelWithConfiguration:config1]; GRPCChannel *channel4 = [pool channelWithConfiguration:config2];
GRPCChannel *channel4 =
[pool channelWithConfiguration:config2];
XCTAssertNotEqual(channel1, channel3); XCTAssertNotEqual(channel1, channel3);
XCTAssertNotEqual(channel2, channel4); XCTAssertNotEqual(channel2, channel4);
} }

@ -446,42 +446,40 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
options.initialMetadata = headers; options.initialMetadata = headers;
GRPCCall2 *call = [[GRPCCall2 alloc] GRPCCall2 *call = [[GRPCCall2 alloc]
initWithRequestOptions:request initWithRequestOptions:request
responseHandler:[[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:^( responseHandler:[[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:^(
NSDictionary *initialMetadata) { NSDictionary *initialMetadata) {
NSString *userAgent = initialMetadata[@"x-grpc-test-echo-useragent"]; NSString *userAgent = initialMetadata[@"x-grpc-test-echo-useragent"];
// Test the regex is correct // Test the regex is correct
NSString *expectedUserAgent = @"Foo grpc-objc/"; NSString *expectedUserAgent = @"Foo grpc-objc/";
expectedUserAgent = expectedUserAgent =
[expectedUserAgent stringByAppendingString:GRPC_OBJC_VERSION_STRING]; [expectedUserAgent stringByAppendingString:GRPC_OBJC_VERSION_STRING];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@" grpc-c/"]; expectedUserAgent = [expectedUserAgent stringByAppendingString:@" grpc-c/"];
expectedUserAgent = expectedUserAgent =
[expectedUserAgent stringByAppendingString:GRPC_C_VERSION_STRING]; [expectedUserAgent stringByAppendingString:GRPC_C_VERSION_STRING];
expectedUserAgent = expectedUserAgent = [expectedUserAgent stringByAppendingString:@" (ios; chttp2; "];
[expectedUserAgent stringByAppendingString:@" (ios; chttp2; "]; expectedUserAgent = [expectedUserAgent
expectedUserAgent = [expectedUserAgent stringByAppendingString:[NSString stringWithUTF8String:grpc_g_stands_for()]];
stringByAppendingString:[NSString expectedUserAgent = [expectedUserAgent stringByAppendingString:@")"];
stringWithUTF8String:grpc_g_stands_for()]]; XCTAssertEqualObjects(userAgent, expectedUserAgent);
expectedUserAgent = [expectedUserAgent stringByAppendingString:@")"];
XCTAssertEqualObjects(userAgent, expectedUserAgent); NSError *error = nil;
// Change in format of user-agent field in a direction that does not match
NSError *error = nil; // the regex will likely cause problem for certain gRPC users. For details,
// Change in format of user-agent field in a direction that does not match // refer to internal doc https://goo.gl/c2diBc
// the regex will likely cause problem for certain gRPC users. For details, NSRegularExpression *regex = [NSRegularExpression
// refer to internal doc https://goo.gl/c2diBc regularExpressionWithPattern:
NSRegularExpression *regex = [NSRegularExpression @" grpc-[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?/[^ ,]+( \\([^)]*\\))?"
regularExpressionWithPattern: options:0
@" grpc-[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?/[^ ,]+( \\([^)]*\\))?" error:&error];
options:0
error:&error]; NSString *customUserAgent =
[regex stringByReplacingMatchesInString:userAgent
NSString *customUserAgent = [regex options:0
stringByReplacingMatchesInString:userAgent range:NSMakeRange(0, [userAgent length])
options:0 withTemplate:@""];
range:NSMakeRange(0, [userAgent length]) XCTAssertEqualObjects(customUserAgent, @"Foo");
withTemplate:@""]; [recvInitialMd fulfill];
XCTAssertEqualObjects(customUserAgent, @"Foo"); }
[recvInitialMd fulfill];
}
messageCallback:^(id message) { messageCallback:^(id message) {
XCTAssertNotNil(message); XCTAssertNotNil(message);
XCTAssertEqual([message length], 0, XCTAssertEqual([message length], 0,
@ -609,7 +607,7 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
options.transportType = GRPCTransportTypeInsecure; options.transportType = GRPCTransportTypeInsecure;
GRPCCall2 *call = [[GRPCCall2 alloc] GRPCCall2 *call = [[GRPCCall2 alloc]
initWithRequestOptions:requestOptions initWithRequestOptions:requestOptions
responseHandler:[[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil responseHandler:[[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil
messageCallback:^(id message) { messageCallback:^(id message) {
NSData *data = (NSData *)message; NSData *data = (NSData *)message;
XCTAssertNotNil(data, @"nil value received as response."); XCTAssertNotNil(data, @"nil value received as response.");
@ -739,19 +737,18 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
GRPCCall2 *call = [[GRPCCall2 alloc] GRPCCall2 *call = [[GRPCCall2 alloc]
initWithRequestOptions:requestOptions initWithRequestOptions:requestOptions
responseHandler: responseHandler:
[[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil [[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil
messageCallback:^(id data) { messageCallback:^(id data) {
XCTFail( XCTFail(@"Failure: response received; Expect: no response received.");
@"Failure: response received; Expect: no response received."); }
} closeCallback:^(NSDictionary *trailingMetadata, NSError *error) {
closeCallback:^(NSDictionary *trailingMetadata, NSError *error) { XCTAssertNotNil(error,
XCTAssertNotNil(error, @"Failure: no error received; Expect: receive "
@"Failure: no error received; Expect: receive " @"deadline exceeded.");
@"deadline exceeded."); XCTAssertEqual(error.code, GRPCErrorCodeDeadlineExceeded);
XCTAssertEqual(error.code, GRPCErrorCodeDeadlineExceeded); [completion fulfill];
[completion fulfill]; }]
}]
callOptions:options]; callOptions:options];
[call start]; [call start];
@ -837,7 +834,9 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
__weak XCTestExpectation *completion = [self expectationWithDescription:@"Timeout in a second."]; __weak XCTestExpectation *completion = [self expectationWithDescription:@"Timeout in a second."];
NSString *const kDummyAddress = [NSString stringWithFormat:@"127.0.0.1:10000"]; NSString *const kDummyAddress = [NSString stringWithFormat:@"127.0.0.1:10000"];
GRPCRequestOptions *requestOptions = GRPCRequestOptions *requestOptions =
[[GRPCRequestOptions alloc] initWithHost:kDummyAddress path:@"/dummy/path" safety:GRPCCallSafetyDefault]; [[GRPCRequestOptions alloc] initWithHost:kDummyAddress
path:@"/dummy/path"
safety:GRPCCallSafetyDefault];
GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init]; GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
options.connectMinTimeout = timeout; options.connectMinTimeout = timeout;
options.connectInitialBackoff = backoff; options.connectInitialBackoff = backoff;
@ -846,7 +845,7 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
NSDate *startTime = [NSDate date]; NSDate *startTime = [NSDate date];
GRPCCall2 *call = [[GRPCCall2 alloc] GRPCCall2 *call = [[GRPCCall2 alloc]
initWithRequestOptions:requestOptions initWithRequestOptions:requestOptions
responseHandler:[[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil responseHandler:[[ClientTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil
messageCallback:^(id data) { messageCallback:^(id data) {
XCTFail(@"Received message. Should not reach here."); XCTFail(@"Received message. Should not reach here.");
} }

@ -483,7 +483,7 @@ BOOL isRemoteInteropTest(NSString *host) {
id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index] id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index]
requestedResponseSize:responses[index]]; requestedResponseSize:responses[index]];
GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init]; GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
options.transportType = [[self class] transportType ]; options.transportType = [[self class] transportType];
options.PEMRootCertificates = [[self class] PEMRootCertificates]; options.PEMRootCertificates = [[self class] PEMRootCertificates];
options.hostNameOverride = [[self class] hostNameOverride]; options.hostNameOverride = [[self class] hostNameOverride];

Loading…
Cancel
Save