Back to NSAssert

pull/16190/head
Muxi Yan 6 years ago
parent 7500528b15
commit 6b6ab2bdc9
  1. 37
      src/objective-c/GRPCClient/GRPCCall.m
  2. 22
      src/objective-c/GRPCClient/private/GRPCChannel.m
  3. 6
      src/objective-c/GRPCClient/private/GRPCChannelPool.m
  4. 2
      src/objective-c/GRPCClient/private/GRPCHost.m
  5. 2
      src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m
  6. 4
      src/objective-c/GRPCClient/private/GRPCWrappedCall.m
  7. 19
      src/objective-c/GRPCClient/private/utilities.h

@ -68,8 +68,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
@implementation GRPCRequestOptions @implementation GRPCRequestOptions
- (instancetype)initWithHost:(NSString *)host path:(NSString *)path safety:(GRPCCallSafety)safety { - (instancetype)initWithHost:(NSString *)host path:(NSString *)path safety:(GRPCCallSafety)safety {
GRPCAssert(host.length != 0 && path.length != 0, NSInvalidArgumentException, NSAssert(host.length != 0 && path.length != 0, @"Host and Path cannot be empty");
@"Host and Path cannot be empty");
if ((self = [super init])) { if ((self = [super init])) {
_host = [host copy]; _host = [host copy];
_path = [path copy]; _path = [path copy];
@ -116,11 +115,11 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions - (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions
responseHandler:(id<GRPCResponseHandler>)responseHandler responseHandler:(id<GRPCResponseHandler>)responseHandler
callOptions:(GRPCCallOptions *_Nullable)callOptions { callOptions:(GRPCCallOptions *_Nullable)callOptions {
GRPCAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0, NSAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0,
NSInvalidArgumentException, @"Neither host nor path can be nil."); @"Neither host nor path can be nil.");
GRPCAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, NSAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest,
@"Invalid call safety value."); @"Invalid call safety value.");
GRPCAssert(responseHandler != nil, NSInvalidArgumentException, @"Response handler required."); NSAssert(responseHandler != nil, @"Response handler required.");
if ((self = [super init])) { if ((self = [super init])) {
_requestOptions = [requestOptions copy]; _requestOptions = [requestOptions copy];
@ -157,8 +156,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)start { - (void)start {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
GRPCAssert(!self->_started, NSInternalInconsistencyException, @"Call already started."); NSAssert(!self->_started, @"Call already started.");
GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call already canceled."); NSAssert(!self->_canceled, @"Call already canceled.");
self->_started = YES; self->_started = YES;
if (!self->_callOptions) { if (!self->_callOptions) {
self->_callOptions = [[GRPCCallOptions alloc] init]; self->_callOptions = [[GRPCCallOptions alloc] init];
@ -218,7 +217,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)cancel { - (void)cancel {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call already canceled."); NSAssert(!self->_canceled, @"Call already canceled.");
self->_canceled = YES; self->_canceled = YES;
if (self->_call) { if (self->_call) {
[self->_call cancel]; [self->_call cancel];
@ -247,9 +246,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)writeData:(NSData *)data { - (void)writeData:(NSData *)data {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call arleady canceled."); NSAssert(!self->_canceled, @"Call arleady canceled.");
GRPCAssert(!self->_finished, NSInternalInconsistencyException, NSAssert(!self->_finished, @"Call is half-closed before sending data.");
@"Call is half-closed before sending data.");
if (self->_pipe) { if (self->_pipe) {
[self->_pipe writeValue:data]; [self->_pipe writeValue:data];
} }
@ -258,9 +256,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)finish { - (void)finish {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
GRPCAssert(self->_started, NSInternalInconsistencyException, @"Call not started."); NSAssert(self->_started, @"Call not started.");
GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call arleady canceled."); NSAssert(!self->_canceled, @"Call arleady canceled.");
GRPCAssert(!self->_finished, NSInternalInconsistencyException, @"Call already half-closed."); NSAssert(!self->_finished, @"Call already half-closed.");
if (self->_pipe) { if (self->_pipe) {
[self->_pipe writesFinishedWithError:nil]; [self->_pipe writesFinishedWithError:nil];
} }
@ -409,10 +407,10 @@ const char *kCFStreamVarName = "grpc_cfstream";
requestsWriter:(GRXWriter *)requestWriter requestsWriter:(GRXWriter *)requestWriter
callOptions:(GRPCCallOptions *)callOptions { callOptions:(GRPCCallOptions *)callOptions {
// Purposely using pointer rather than length ([host length] == 0) for backwards compatibility. // Purposely using pointer rather than length ([host length] == 0) for backwards compatibility.
GRPCAssert(host && path, NSInvalidArgumentException, @"Neither host nor path can be nil."); NSAssert(host && path, @"Neither host nor path can be nil.");
GRPCAssert(safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, NSAssert(safety <= GRPCCallSafetyCacheableRequest,
@"Invalid call safety value."); @"Invalid call safety value.");
GRPCAssert(requestWriter.state == GRXWriterStateNotStarted, NSInvalidArgumentException, NSAssert(requestWriter.state == GRXWriterStateNotStarted,
@"The requests writer can't be already started."); @"The requests writer can't be already started.");
if ((self = [super init])) { if ((self = [super init])) {
_host = [host copy]; _host = [host copy];
@ -798,8 +796,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
_callOptions = callOptions; _callOptions = callOptions;
} }
GRPCAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil, NSAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil,
NSInvalidArgumentException,
@"authTokenProvider and oauth2AccessToken cannot be set at the same time"); @"authTokenProvider and oauth2AccessToken cannot be set at the same time");
if (_callOptions.authTokenProvider != nil) { if (_callOptions.authTokenProvider != nil) {
@synchronized(self) { @synchronized(self) {

@ -40,8 +40,8 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
@implementation GRPCChannelConfiguration @implementation GRPCChannelConfiguration
- (nullable instancetype)initWithHost:(NSString *)host callOptions:(GRPCCallOptions *)callOptions { - (nullable instancetype)initWithHost:(NSString *)host callOptions:(GRPCCallOptions *)callOptions {
GRPCAssert(host.length, NSInvalidArgumentException, @"Host must not be empty."); NSAssert(host.length, @"Host must not be empty.");
GRPCAssert(callOptions != nil, NSInvalidArgumentException, @"callOptions must not be empty."); NSAssert(callOptions != nil, @"callOptions must not be empty.");
if ((self = [super init])) { if ((self = [super init])) {
_host = [host copy]; _host = [host copy];
_callOptions = [callOptions copy]; _callOptions = [callOptions copy];
@ -193,9 +193,9 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (nullable instancetype)initWithChannelConfiguration: - (nullable instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration (GRPCChannelConfiguration *)channelConfiguration
destroyDelay:(NSTimeInterval)destroyDelay { destroyDelay:(NSTimeInterval)destroyDelay {
GRPCAssert(channelConfiguration != nil, NSInvalidArgumentException, NSAssert(channelConfiguration != nil,
@"channelConfiguration must not be empty."); @"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])) { if ((self = [super init])) {
_configuration = [channelConfiguration copy]; _configuration = [channelConfiguration copy];
if (@available(iOS 8.0, *)) { if (@available(iOS 8.0, *)) {
@ -208,7 +208,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
// Create gRPC core channel object. // Create gRPC core channel object.
NSString *host = channelConfiguration.host; NSString *host = channelConfiguration.host;
GRPCAssert(host.length != 0, NSInvalidArgumentException, @"host cannot be nil"); NSAssert(host.length != 0, @"host cannot be nil");
NSDictionary *channelArgs; NSDictionary *channelArgs;
if (channelConfiguration.callOptions.additionalChannelArgs.count != 0) { if (channelConfiguration.callOptions.additionalChannelArgs.count != 0) {
NSMutableDictionary *args = [channelConfiguration.channelArgs mutableCopy]; NSMutableDictionary *args = [channelConfiguration.channelArgs mutableCopy];
@ -233,22 +233,22 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
completionQueue:(GRPCCompletionQueue *)queue completionQueue:(GRPCCompletionQueue *)queue
callOptions:(GRPCCallOptions *)callOptions callOptions:(GRPCCallOptions *)callOptions
disconnected:(BOOL *)disconnected { disconnected:(BOOL *)disconnected {
GRPCAssert(path.length, NSInvalidArgumentException, @"path must not be empty."); NSAssert(path.length, @"path must not be empty.");
GRPCAssert(queue, NSInvalidArgumentException, @"completionQueue must not be empty."); NSAssert(queue, @"completionQueue must not be empty.");
GRPCAssert(callOptions, NSInvalidArgumentException, @"callOptions must not be empty."); NSAssert(callOptions, @"callOptions must not be empty.");
__block BOOL isDisconnected = NO; __block BOOL isDisconnected = NO;
__block grpc_call *call = NULL; __block grpc_call *call = NULL;
dispatch_sync(_dispatchQueue, ^{ dispatch_sync(_dispatchQueue, ^{
if (self->_disconnected) { if (self->_disconnected) {
isDisconnected = YES; isDisconnected = YES;
} else { } else {
GRPCAssert(self->_unmanagedChannel != NULL, NSInternalInconsistencyException, NSAssert(self->_unmanagedChannel != NULL,
@"Channel should have valid unmanaged channel."); @"Channel should have valid unmanaged channel.");
NSString *serverAuthority = NSString *serverAuthority =
callOptions.transportType == GRPCTransportTypeCronet ? nil : callOptions.serverAuthority; callOptions.transportType == GRPCTransportTypeCronet ? nil : callOptions.serverAuthority;
NSTimeInterval timeout = callOptions.timeout; NSTimeInterval timeout = callOptions.timeout;
GRPCAssert(timeout >= 0, NSInvalidArgumentException, @"Invalid timeout"); NSAssert(timeout >= 0, @"Invalid timeout");
grpc_slice host_slice = grpc_empty_slice(); grpc_slice host_slice = grpc_empty_slice();
if (serverAuthority) { if (serverAuthority) {
host_slice = grpc_slice_from_copied_string(serverAuthority.UTF8String); host_slice = grpc_slice_from_copied_string(serverAuthority.UTF8String);
@ -291,7 +291,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (void)unref { - (void)unref {
NSLog(@"unref"); NSLog(@"unref");
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
GRPCAssert(self->_refcount > 0, NSInternalInconsistencyException, @"Illegal reference count."); NSAssert(self->_refcount > 0, @"Illegal reference count.");
self->_refcount--; self->_refcount--;
if (self->_refcount == 0 && !self->_disconnected) { if (self->_refcount == 0 && !self->_disconnected) {
// Start timer. // Start timer.

@ -44,7 +44,7 @@ static dispatch_once_t gInitChannelPool;
+ (nullable instancetype)sharedInstance { + (nullable instancetype)sharedInstance {
dispatch_once(&gInitChannelPool, ^{ dispatch_once(&gInitChannelPool, ^{
gChannelPool = [[GRPCChannelPool alloc] init]; gChannelPool = [[GRPCChannelPool alloc] init];
GRPCAssert(gChannelPool != nil, NSMallocException, @"Cannot initialize global channel pool."); NSAssert(gChannelPool != nil, @"Cannot initialize global channel pool.");
}); });
return gChannelPool; return gChannelPool;
} }
@ -69,8 +69,8 @@ static dispatch_once_t gInitChannelPool;
- (GRPCChannel *)channelWithHost:(NSString *)host - (GRPCChannel *)channelWithHost:(NSString *)host
callOptions:(GRPCCallOptions *)callOptions callOptions:(GRPCCallOptions *)callOptions
destroyDelay:(NSTimeInterval)destroyDelay { destroyDelay:(NSTimeInterval)destroyDelay {
GRPCAssert(host.length > 0, NSInvalidArgumentException, @"Host must not be empty."); NSAssert(host.length > 0, @"Host must not be empty.");
GRPCAssert(callOptions != nil, NSInvalidArgumentException, @"callOptions must not be empty."); NSAssert(callOptions != nil, @"callOptions must not be empty.");
GRPCChannel *channel; GRPCChannel *channel;
GRPCChannelConfiguration *configuration = GRPCChannelConfiguration *configuration =
[[GRPCChannelConfiguration alloc] initWithHost:host callOptions:callOptions]; [[GRPCChannelConfiguration alloc] initWithHost:host callOptions:callOptions];

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

@ -83,7 +83,7 @@ NS_ASSUME_NONNULL_BEGIN
if (errorPtr) { if (errorPtr) {
*errorPtr = defaultRootsError; *errorPtr = defaultRootsError;
} }
GRPCAssertWithArgument( NSAssert(
defaultRootsASCII, NSObjectNotAvailableException, defaultRootsASCII, NSObjectNotAvailableException,
@"Could not read gRPCCertificates.bundle/roots.pem. This file, " @"Could not read gRPCCertificates.bundle/roots.pem. This file, "
"with the root certificates, is needed to establish secure (TLS) connections. " "with the root certificates, is needed to establish secure (TLS) connections. "

@ -249,7 +249,7 @@
- (instancetype)initWithHost:(NSString *)host - (instancetype)initWithHost:(NSString *)host
path:(NSString *)path path:(NSString *)path
callOptions:(GRPCCallOptions *)callOptions { callOptions:(GRPCCallOptions *)callOptions {
GRPCAssert(host.length != 0 && path.length != 0, NSInvalidArgumentException, NSAssert(host.length != 0 && path.length != 0,
@"path and host cannot be nil."); @"path and host cannot be nil.");
if ((self = [super init])) { if ((self = [super init])) {
@ -261,6 +261,7 @@
do { do {
_channel = [[GRPCChannelPool sharedInstance] channelWithHost:host callOptions:callOptions]; _channel = [[GRPCChannelPool sharedInstance] channelWithHost:host callOptions:callOptions];
if (_channel == nil) { if (_channel == nil) {
NSAssert(_channel != nil, @"Failed to get a channel for the host.");
NSLog(@"Failed to get a channel for the host."); NSLog(@"Failed to get a channel for the host.");
return nil; return nil;
} }
@ -272,6 +273,7 @@
// connectivity monitor disconnection). // connectivity monitor disconnection).
} while (_call == NULL && disconnected); } while (_call == NULL && disconnected);
if (_call == nil) { if (_call == nil) {
NSAssert(_channel != nil, @"Failed to get a channel for the host.");
NSLog(@"Failed to create a call."); NSLog(@"Failed to create a call.");
return nil; return nil;
} }

@ -18,19 +18,6 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
/** Raise exception when condition not met. Disregard NS_BLOCK_ASSERTIONS. */ /** Raise exception when condition not met. */
#define GRPCAssert(condition, errorType, errorString) \ #define GRPCAssert(condition, errorString) \
do { \ NSAssert(condition, errorString)
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)

Loading…
Cancel
Save