Introduce GRPCAssert

pull/16190/head
Muxi Yan 6 years ago
parent 861e7ce452
commit 33022c9172
  1. 47
      src/objective-c/GRPCClient/GRPCCall.m
  2. 21
      src/objective-c/GRPCClient/private/GRPCChannel.m
  3. 9
      src/objective-c/GRPCClient/private/GRPCChannelPool.m
  4. 3
      src/objective-c/GRPCClient/private/GRPCHost.m
  5. 4
      src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m
  6. 35
      src/objective-c/GRPCClient/private/utilities.h

@ -35,6 +35,7 @@
#import "private/NSData+GRPC.h" #import "private/NSData+GRPC.h"
#import "private/NSDictionary+GRPC.h" #import "private/NSDictionary+GRPC.h"
#import "private/NSError+GRPC.h" #import "private/NSError+GRPC.h"
#import "private/utilities.h"
// At most 6 ops can be in an op batch for a client: SEND_INITIAL_METADATA, // At most 6 ops can be in an op batch for a client: SEND_INITIAL_METADATA,
// SEND_MESSAGE, SEND_CLOSE_FROM_CLIENT, RECV_INITIAL_METADATA, RECV_MESSAGE, // SEND_MESSAGE, SEND_CLOSE_FROM_CLIENT, RECV_INITIAL_METADATA, RECV_MESSAGE,
@ -67,7 +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 {
NSAssert(host.length != 0 && path.length != 0, @"Host and Path cannot be empty"); GRPCAssert(host.length != 0 && path.length != 0, NSInvalidArgumentException, @"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];
@ -114,15 +115,9 @@ 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 {
if (requestOptions.host.length == 0 || requestOptions.path.length == 0) { GRPCAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0, NSInvalidArgumentException, @"Neither host nor path can be nil.");
[NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."]; GRPCAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, @"Invalid call safety value.");
} GRPCAssert(responseHandler != nil, NSInvalidArgumentException, @"Response handler required.");
if (requestOptions.safety > GRPCCallSafetyCacheableRequest) {
[NSException raise:NSInvalidArgumentException format:@"Invalid call safety value."];
}
if (responseHandler == nil) {
[NSException raise:NSInvalidArgumentException format:@"Response handler required."];
}
if ((self = [super init])) { if ((self = [super init])) {
_requestOptions = [requestOptions copy]; _requestOptions = [requestOptions copy];
@ -159,8 +154,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)start { - (void)start {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
NSAssert(!self->_started, @"Call already started."); GRPCAssert(!self->_started, NSInternalInconsistencyException, @"Call already started.");
NSAssert(!self->_canceled, @"Call already canceled."); GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"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];
@ -216,7 +211,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)cancel { - (void)cancel {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
NSAssert(!self->_canceled, @"Call already canceled."); GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call already canceled.");
if (self->_call) { if (self->_call) {
[self->_call cancel]; [self->_call cancel];
self->_call = nil; self->_call = nil;
@ -244,8 +239,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)writeData:(NSData *)data { - (void)writeData:(NSData *)data {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
NSAssert(!self->_canceled, @"Call arleady canceled."); GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call arleady canceled.");
NSAssert(!self->_finished, @"Call is half-closed before sending data."); GRPCAssert(!self->_finished, NSInternalInconsistencyException, @"Call is half-closed before sending data.");
if (self->_call) { if (self->_call) {
[self->_pipe writeValue:data]; [self->_pipe writeValue:data];
} }
@ -254,9 +249,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)finish { - (void)finish {
dispatch_async(_dispatchQueue, ^{ dispatch_async(_dispatchQueue, ^{
NSAssert(self->_started, @"Call not started."); GRPCAssert(self->_started, NSInternalInconsistencyException, @"Call not started.");
NSAssert(!self->_canceled, @"Call arleady canceled."); GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call arleady canceled.");
NSAssert(!self->_finished, @"Call already half-closed."); GRPCAssert(!self->_finished, NSInternalInconsistencyException, @"Call already half-closed.");
if (self->_call) { if (self->_call) {
[self->_pipe writesFinishedWithError:nil]; [self->_pipe writesFinishedWithError:nil];
} }
@ -404,16 +399,9 @@ 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.
if (!host || !path) { GRPCAssert(host && path, NSInvalidArgumentException, @"Neither host nor path can be nil.");
[NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."]; GRPCAssert(safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, @"Invalid call safety value.");
} GRPCAssert(requestWriter.state == GRXWriterStateNotStarted, NSInvalidArgumentException, @"The requests writer can't be already started.");
if (safety > GRPCCallSafetyCacheableRequest) {
[NSException raise:NSInvalidArgumentException format:@"Invalid call safety value."];
}
if (requestWriter.state != GRXWriterStateNotStarted) {
[NSException raise:NSInvalidArgumentException
format:@"The requests writer can't be already started."];
}
if ((self = [super init])) { if ((self = [super init])) {
_host = [host copy]; _host = [host copy];
_path = [path copy]; _path = [path copy];
@ -798,8 +786,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
_callOptions = callOptions; _callOptions = callOptions;
} }
NSAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil, GRPCAssert(_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) {
self.isWaitingForToken = YES; self.isWaitingForToken = YES;

@ -28,6 +28,7 @@
#import "GRPCInsecureChannelFactory.h" #import "GRPCInsecureChannelFactory.h"
#import "GRPCSecureChannelFactory.h" #import "GRPCSecureChannelFactory.h"
#import "version.h" #import "version.h"
#import "utilities.h"
#import "../internal/GRPCCallOptions+Internal.h" #import "../internal/GRPCCallOptions+Internal.h"
#import <GRPCClient/GRPCCall+Cronet.h> #import <GRPCClient/GRPCCall+Cronet.h>
@ -39,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 {
NSAssert(host.length, @"Host must not be empty."); GRPCAssert(host.length, NSInvalidArgumentException, @"Host must not be empty.");
NSAssert(callOptions, @"callOptions must not be empty."); GRPCAssert(callOptions != nil, NSInvalidArgumentException, @"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];
@ -192,8 +193,8 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration - (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration
destroyDelay:(NSTimeInterval)destroyDelay { destroyDelay:(NSTimeInterval)destroyDelay {
NSAssert(channelConfiguration, @"channelConfiguration must not be empty."); GRPCAssert(channelConfiguration != nil, NSInvalidArgumentException, @"channelConfiguration must not be empty.");
NSAssert(destroyDelay > 0, @"destroyDelay must be greater than 0."); GRPCAssert(destroyDelay > 0, NSInvalidArgumentException, @"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, *)) {
@ -206,7 +207,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
// Create gRPC core channel object. // Create gRPC core channel object.
NSString *host = channelConfiguration.host; NSString *host = channelConfiguration.host;
NSAssert(host.length != 0, @"host cannot be nil"); GRPCAssert(host.length != 0, NSInvalidArgumentException, @"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];
@ -231,21 +232,21 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
completionQueue:(GRPCCompletionQueue *)queue completionQueue:(GRPCCompletionQueue *)queue
callOptions:(GRPCCallOptions *)callOptions callOptions:(GRPCCallOptions *)callOptions
disconnected:(BOOL *)disconnected { disconnected:(BOOL *)disconnected {
NSAssert(path.length, @"path must not be empty."); GRPCAssert(path.length, NSInvalidArgumentException, @"path must not be empty.");
NSAssert(queue, @"completionQueue must not be empty."); GRPCAssert(queue, NSInvalidArgumentException, @"completionQueue must not be empty.");
NSAssert(callOptions, @"callOptions must not be empty."); GRPCAssert(callOptions, NSInvalidArgumentException, @"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 {
NSAssert(self->_unmanagedChannel != NULL, @"Invalid channel."); GRPCAssert(self->_unmanagedChannel != NULL, NSInvalidArgumentException, @"Invalid 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;
NSAssert(timeout >= 0, @"Invalid timeout"); GRPCAssert(timeout >= 0, NSInvalidArgumentException, @"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);

@ -27,6 +27,7 @@
#import "GRPCInsecureChannelFactory.h" #import "GRPCInsecureChannelFactory.h"
#import "GRPCSecureChannelFactory.h" #import "GRPCSecureChannelFactory.h"
#import "version.h" #import "version.h"
#import "utilities.h"
#import <GRPCClient/GRPCCall+Cronet.h> #import <GRPCClient/GRPCCall+Cronet.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
@ -43,9 +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];
if (gChannelPool == nil) { GRPCAssert(gChannelPool != nil, NSMallocException, @"Cannot initialize global channel pool.");
[NSException raise:NSMallocException format:@"Cannot initialize global channel pool."];
}
}); });
return gChannelPool; return gChannelPool;
} }
@ -73,8 +72,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 {
NSAssert(host.length > 0, @"Host must not be empty."); GRPCAssert(host.length > 0, NSInvalidArgumentException, @"Host must not be empty.");
NSAssert(callOptions != nil, @"callOptions must not be empty."); GRPCAssert(callOptions != nil, NSInvalidArgumentException, @"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];

@ -33,6 +33,7 @@
#import "GRPCSecureChannelFactory.h" #import "GRPCSecureChannelFactory.h"
#import "NSDictionary+GRPC.h" #import "NSDictionary+GRPC.h"
#import "version.h" #import "version.h"
#import "utilities.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -121,7 +122,7 @@ static NSMutableDictionary *gHostCache;
if (_transportType == GRPCTransportTypeInsecure) { if (_transportType == GRPCTransportTypeInsecure) {
options.transportType = GRPCTransportTypeInsecure; options.transportType = GRPCTransportTypeInsecure;
} else { } else {
NSAssert(_transportType == GRPCTransportTypeDefault, @"Invalid transport type"); GRPCAssert(_transportType == GRPCTransportTypeDefault, NSInvalidArgumentException, @"Invalid transport type");
options.transportType = GRPCTransportTypeCronet; options.transportType = GRPCTransportTypeCronet;
} }
} else } else

@ -22,6 +22,7 @@
#import "ChannelArgsUtil.h" #import "ChannelArgsUtil.h"
#import "GRPCChannel.h" #import "GRPCChannel.h"
#import "utilities.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -82,8 +83,9 @@ NS_ASSUME_NONNULL_BEGIN
if (errorPtr) { if (errorPtr) {
*errorPtr = defaultRootsError; *errorPtr = defaultRootsError;
} }
NSAssert( GRPCAssertWithArgument(
defaultRootsASCII, 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. "
"Because the file is distributed with the gRPC library, this error is usually a sign " "Because the file is distributed with the gRPC library, this error is usually a sign "

@ -0,0 +1,35 @@
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#import <Foundation/Foundation.h>
/** 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)
Loading…
Cancel
Save