From 6b6ab2bdc9d03ffe85398451342a140320aac1dd Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Thu, 15 Nov 2018 12:22:38 -0800 Subject: [PATCH] Back to NSAssert --- src/objective-c/GRPCClient/GRPCCall.m | 37 +++++++++---------- .../GRPCClient/private/GRPCChannel.m | 22 +++++------ .../GRPCClient/private/GRPCChannelPool.m | 6 +-- src/objective-c/GRPCClient/private/GRPCHost.m | 2 +- .../private/GRPCSecureChannelFactory.m | 2 +- .../GRPCClient/private/GRPCWrappedCall.m | 4 +- .../GRPCClient/private/utilities.h | 19 ++-------- 7 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m index 4068c3b4608..788afeb6c3c 100644 --- a/src/objective-c/GRPCClient/GRPCCall.m +++ b/src/objective-c/GRPCClient/GRPCCall.m @@ -68,8 +68,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; @implementation GRPCRequestOptions - (instancetype)initWithHost:(NSString *)host path:(NSString *)path safety:(GRPCCallSafety)safety { - GRPCAssert(host.length != 0 && path.length != 0, NSInvalidArgumentException, - @"Host and Path cannot be empty"); + NSAssert(host.length != 0 && path.length != 0, @"Host and Path cannot be empty"); if ((self = [super init])) { _host = [host copy]; _path = [path copy]; @@ -116,11 +115,11 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions responseHandler:(id)responseHandler callOptions:(GRPCCallOptions *_Nullable)callOptions { - GRPCAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0, - NSInvalidArgumentException, @"Neither host nor path can be nil."); - GRPCAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, + NSAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0, + @"Neither host nor path can be nil."); + NSAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, @"Invalid call safety value."); - GRPCAssert(responseHandler != nil, NSInvalidArgumentException, @"Response handler required."); + NSAssert(responseHandler != nil, @"Response handler required."); if ((self = [super init])) { _requestOptions = [requestOptions copy]; @@ -157,8 +156,8 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (void)start { dispatch_async(_dispatchQueue, ^{ - GRPCAssert(!self->_started, NSInternalInconsistencyException, @"Call already started."); - GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call already canceled."); + NSAssert(!self->_started, @"Call already started."); + NSAssert(!self->_canceled, @"Call already canceled."); self->_started = YES; if (!self->_callOptions) { self->_callOptions = [[GRPCCallOptions alloc] init]; @@ -218,7 +217,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (void)cancel { dispatch_async(_dispatchQueue, ^{ - GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call already canceled."); + NSAssert(!self->_canceled, @"Call already canceled."); self->_canceled = YES; if (self->_call) { [self->_call cancel]; @@ -247,9 +246,8 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (void)writeData:(NSData *)data { dispatch_async(_dispatchQueue, ^{ - GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call arleady canceled."); - GRPCAssert(!self->_finished, NSInternalInconsistencyException, - @"Call is half-closed before sending data."); + NSAssert(!self->_canceled, @"Call arleady canceled."); + NSAssert(!self->_finished, @"Call is half-closed before sending data."); if (self->_pipe) { [self->_pipe writeValue:data]; } @@ -258,9 +256,9 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (void)finish { dispatch_async(_dispatchQueue, ^{ - GRPCAssert(self->_started, NSInternalInconsistencyException, @"Call not started."); - GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call arleady canceled."); - GRPCAssert(!self->_finished, NSInternalInconsistencyException, @"Call already half-closed."); + NSAssert(self->_started, @"Call not started."); + NSAssert(!self->_canceled, @"Call arleady canceled."); + NSAssert(!self->_finished, @"Call already half-closed."); if (self->_pipe) { [self->_pipe writesFinishedWithError:nil]; } @@ -409,10 +407,10 @@ const char *kCFStreamVarName = "grpc_cfstream"; requestsWriter:(GRXWriter *)requestWriter callOptions:(GRPCCallOptions *)callOptions { // Purposely using pointer rather than length ([host length] == 0) for backwards compatibility. - GRPCAssert(host && path, NSInvalidArgumentException, @"Neither host nor path can be nil."); - GRPCAssert(safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, + NSAssert(host && path, @"Neither host nor path can be nil."); + NSAssert(safety <= GRPCCallSafetyCacheableRequest, @"Invalid call safety value."); - GRPCAssert(requestWriter.state == GRXWriterStateNotStarted, NSInvalidArgumentException, + NSAssert(requestWriter.state == GRXWriterStateNotStarted, @"The requests writer can't be already started."); if ((self = [super init])) { _host = [host copy]; @@ -798,8 +796,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; _callOptions = callOptions; } - GRPCAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil, - NSInvalidArgumentException, + NSAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil, @"authTokenProvider and oauth2AccessToken cannot be set at the same time"); if (_callOptions.authTokenProvider != nil) { @synchronized(self) { diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m index 2933ca7044a..ab084fba480 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannel.m +++ b/src/objective-c/GRPCClient/private/GRPCChannel.m @@ -40,8 +40,8 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30; @implementation GRPCChannelConfiguration - (nullable instancetype)initWithHost:(NSString *)host callOptions:(GRPCCallOptions *)callOptions { - GRPCAssert(host.length, NSInvalidArgumentException, @"Host must not be empty."); - GRPCAssert(callOptions != nil, NSInvalidArgumentException, @"callOptions must not be empty."); + NSAssert(host.length, @"Host must not be empty."); + NSAssert(callOptions != nil, @"callOptions must not be empty."); if ((self = [super init])) { _host = [host copy]; _callOptions = [callOptions copy]; @@ -193,9 +193,9 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30; - (nullable instancetype)initWithChannelConfiguration: (GRPCChannelConfiguration *)channelConfiguration destroyDelay:(NSTimeInterval)destroyDelay { - GRPCAssert(channelConfiguration != nil, NSInvalidArgumentException, + NSAssert(channelConfiguration != nil, @"channelConfiguration must not be empty."); - GRPCAssert(destroyDelay > 0, NSInvalidArgumentException, @"destroyDelay must be greater than 0."); + NSAssert(destroyDelay > 0, @"destroyDelay must be greater than 0."); if ((self = [super init])) { _configuration = [channelConfiguration copy]; if (@available(iOS 8.0, *)) { @@ -208,7 +208,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30; // Create gRPC core channel object. NSString *host = channelConfiguration.host; - GRPCAssert(host.length != 0, NSInvalidArgumentException, @"host cannot be nil"); + NSAssert(host.length != 0, @"host cannot be nil"); NSDictionary *channelArgs; if (channelConfiguration.callOptions.additionalChannelArgs.count != 0) { NSMutableDictionary *args = [channelConfiguration.channelArgs mutableCopy]; @@ -233,22 +233,22 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30; completionQueue:(GRPCCompletionQueue *)queue callOptions:(GRPCCallOptions *)callOptions disconnected:(BOOL *)disconnected { - GRPCAssert(path.length, NSInvalidArgumentException, @"path must not be empty."); - GRPCAssert(queue, NSInvalidArgumentException, @"completionQueue must not be empty."); - GRPCAssert(callOptions, NSInvalidArgumentException, @"callOptions must not be empty."); + NSAssert(path.length, @"path must not be empty."); + NSAssert(queue, @"completionQueue must not be empty."); + NSAssert(callOptions, @"callOptions must not be empty."); __block BOOL isDisconnected = NO; __block grpc_call *call = NULL; dispatch_sync(_dispatchQueue, ^{ if (self->_disconnected) { isDisconnected = YES; } else { - GRPCAssert(self->_unmanagedChannel != NULL, NSInternalInconsistencyException, + NSAssert(self->_unmanagedChannel != NULL, @"Channel should have valid unmanaged channel."); NSString *serverAuthority = callOptions.transportType == GRPCTransportTypeCronet ? nil : callOptions.serverAuthority; NSTimeInterval timeout = callOptions.timeout; - GRPCAssert(timeout >= 0, NSInvalidArgumentException, @"Invalid timeout"); + NSAssert(timeout >= 0, @"Invalid timeout"); grpc_slice host_slice = grpc_empty_slice(); if (serverAuthority) { host_slice = grpc_slice_from_copied_string(serverAuthority.UTF8String); @@ -291,7 +291,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30; - (void)unref { NSLog(@"unref"); dispatch_async(_dispatchQueue, ^{ - GRPCAssert(self->_refcount > 0, NSInternalInconsistencyException, @"Illegal reference count."); + NSAssert(self->_refcount > 0, @"Illegal reference count."); self->_refcount--; if (self->_refcount == 0 && !self->_disconnected) { // Start timer. diff --git a/src/objective-c/GRPCClient/private/GRPCChannelPool.m b/src/objective-c/GRPCClient/private/GRPCChannelPool.m index 85ca527277c..6745010bd49 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannelPool.m +++ b/src/objective-c/GRPCClient/private/GRPCChannelPool.m @@ -44,7 +44,7 @@ static dispatch_once_t gInitChannelPool; + (nullable instancetype)sharedInstance { dispatch_once(&gInitChannelPool, ^{ gChannelPool = [[GRPCChannelPool alloc] init]; - GRPCAssert(gChannelPool != nil, NSMallocException, @"Cannot initialize global channel pool."); + NSAssert(gChannelPool != nil, @"Cannot initialize global channel pool."); }); return gChannelPool; } @@ -69,8 +69,8 @@ static dispatch_once_t gInitChannelPool; - (GRPCChannel *)channelWithHost:(NSString *)host callOptions:(GRPCCallOptions *)callOptions destroyDelay:(NSTimeInterval)destroyDelay { - GRPCAssert(host.length > 0, NSInvalidArgumentException, @"Host must not be empty."); - GRPCAssert(callOptions != nil, NSInvalidArgumentException, @"callOptions must not be empty."); + NSAssert(host.length > 0, @"Host must not be empty."); + NSAssert(callOptions != nil, @"callOptions must not be empty."); GRPCChannel *channel; GRPCChannelConfiguration *configuration = [[GRPCChannelConfiguration alloc] initWithHost:host callOptions:callOptions]; diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 5fc7427b689..b693b926bad 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -122,7 +122,7 @@ static NSMutableDictionary *gHostCache; if (_transportType == GRPCTransportTypeInsecure) { options.transportType = GRPCTransportTypeInsecure; } else { - GRPCAssert(_transportType == GRPCTransportTypeDefault, NSInvalidArgumentException, + NSAssert(_transportType == GRPCTransportTypeDefault, @"Invalid transport type"); options.transportType = GRPCTransportTypeCronet; } diff --git a/src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m b/src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m index 57bd40d6787..f5e7a2b9e20 100644 --- a/src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m +++ b/src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m @@ -83,7 +83,7 @@ NS_ASSUME_NONNULL_BEGIN if (errorPtr) { *errorPtr = defaultRootsError; } - GRPCAssertWithArgument( + NSAssert( defaultRootsASCII, NSObjectNotAvailableException, @"Could not read gRPCCertificates.bundle/roots.pem. This file, " "with the root certificates, is needed to establish secure (TLS) connections. " diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m index aa7d60786ea..ae7f07f1192 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m @@ -249,7 +249,7 @@ - (instancetype)initWithHost:(NSString *)host path:(NSString *)path callOptions:(GRPCCallOptions *)callOptions { - GRPCAssert(host.length != 0 && path.length != 0, NSInvalidArgumentException, + NSAssert(host.length != 0 && path.length != 0, @"path and host cannot be nil."); if ((self = [super init])) { @@ -261,6 +261,7 @@ do { _channel = [[GRPCChannelPool sharedInstance] channelWithHost:host callOptions:callOptions]; if (_channel == nil) { + NSAssert(_channel != nil, @"Failed to get a channel for the host."); NSLog(@"Failed to get a channel for the host."); return nil; } @@ -272,6 +273,7 @@ // connectivity monitor disconnection). } while (_call == NULL && disconnected); if (_call == nil) { + NSAssert(_channel != nil, @"Failed to get a channel for the host."); NSLog(@"Failed to create a call."); return nil; } diff --git a/src/objective-c/GRPCClient/private/utilities.h b/src/objective-c/GRPCClient/private/utilities.h index 3e51730d637..8f6baadcf7b 100644 --- a/src/objective-c/GRPCClient/private/utilities.h +++ b/src/objective-c/GRPCClient/private/utilities.h @@ -18,19 +18,6 @@ #import -/** Raise exception when condition not met. Disregard NS_BLOCK_ASSERTIONS. */ -#define GRPCAssert(condition, errorType, errorString) \ - do { \ - if (!(condition)) { \ - [NSException raise:(errorType)format:(errorString)]; \ - } \ - } while (0) - -/** The same as GRPCAssert but allows arguments to be put in the raised string. - */ -#define GRPCAssertWithArgument(condition, errorType, errorFormat, ...) \ - do { \ - if (!(condition)) { \ - [NSException raise:(errorType)format:(errorFormat), __VA_ARGS__]; \ - } \ - } while (0) +/** Raise exception when condition not met. */ +#define GRPCAssert(condition, errorString) \ + NSAssert(condition, errorString)