diff --git a/src/objective-c/GRPCClient/GRPCCall.m b/src/objective-c/GRPCClient/GRPCCall.m index fb4dbdb5d71..40aaea16e56 100644 --- a/src/objective-c/GRPCClient/GRPCCall.m +++ b/src/objective-c/GRPCClient/GRPCCall.m @@ -18,8 +18,8 @@ #import "GRPCCall.h" #import "GRPCCall+OAuth2.h" -#import "GRPCInterceptor.h" #import "GRPCCallOptions.h" +#import "GRPCInterceptor.h" #import #import @@ -28,6 +28,8 @@ #include #include +#import "private/GRPCCall+V2API.h" +#import "private/GRPCCallInternal.h" #import "private/GRPCChannelPool.h" #import "private/GRPCCompletionQueue.h" #import "private/GRPCConnectivityMonitor.h" @@ -37,8 +39,6 @@ #import "private/NSData+GRPC.h" #import "private/NSDictionary+GRPC.h" #import "private/NSError+GRPC.h" -#import "private/GRPCCall+V2API.h" -#import "private/GRPCCallInternal.h" // 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, @@ -142,8 +142,10 @@ const char *kCFStreamVarName = "grpc_cfstream"; [internalCall setResponseHandler:_responseHandler]; } else { for (int i = (int)interceptorFactories.count - 1; i >= 0; i--) { - GRPCInterceptorManager *manager = [[GRPCInterceptorManager alloc] initWithNextInerceptor:nextInterceptor]; - GRPCInterceptor *interceptor = [interceptorFactories[i] createInterceptorWithManager:manager]; + GRPCInterceptorManager *manager = + [[GRPCInterceptorManager alloc] initWithNextInerceptor:nextInterceptor]; + GRPCInterceptor *interceptor = + [interceptorFactories[i] createInterceptorWithManager:manager]; NSAssert(interceptor != nil, @"Failed to create interceptor"); if (interceptor == nil) { return nil; @@ -180,8 +182,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; GRPCCallOptions *callOptions = [_actualCallOptions copy]; if ([copiedFirstInterceptor respondsToSelector:@selector(startWithRequestOptions:callOptions:)]) { dispatch_async(copiedFirstInterceptor.requestDispatchQueue, ^{ - [copiedFirstInterceptor startWithRequestOptions:requestOptions - callOptions:callOptions]; + [copiedFirstInterceptor startWithRequestOptions:requestOptions callOptions:callOptions]; }); } } @@ -200,7 +201,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (void)writeData:(id)data { id copiedFirstInterceptor; - @synchronized (self) { + @synchronized(self) { copiedFirstInterceptor = _firstInterceptor; } if ([copiedFirstInterceptor respondsToSelector:@selector(writeData:)]) { @@ -212,7 +213,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (void)finish { id copiedFirstInterceptor; - @synchronized (self) { + @synchronized(self) { copiedFirstInterceptor = _firstInterceptor; } if ([copiedFirstInterceptor respondsToSelector:@selector(finish)]) { @@ -224,7 +225,7 @@ const char *kCFStreamVarName = "grpc_cfstream"; - (void)receiveNextMessages:(NSUInteger)numberOfMessages { id copiedFirstInterceptor; - @synchronized (self) { + @synchronized(self) { copiedFirstInterceptor = _firstInterceptor; } if ([copiedFirstInterceptor respondsToSelector:@selector(receiveNextMessages:)]) { diff --git a/src/objective-c/GRPCClient/GRPCCallOptions.h b/src/objective-c/GRPCClient/GRPCCallOptions.h index 6f985c8d952..98511e3f5cb 100644 --- a/src/objective-c/GRPCClient/GRPCCallOptions.h +++ b/src/objective-c/GRPCClient/GRPCCallOptions.h @@ -106,7 +106,6 @@ typedef NS_ENUM(NSUInteger, GRPCTransportType) { */ @property(copy, readonly) NSArray *interceptorFactories; - // OAuth2 parameters. Users of gRPC may specify one of the following two parameters. /** @@ -270,7 +269,6 @@ typedef NS_ENUM(NSUInteger, GRPCTransportType) { */ @property(copy, readwrite) NSArray *interceptorFactories; - // OAuth2 parameters. Users of gRPC may specify one of the following two parameters. /** diff --git a/src/objective-c/GRPCClient/GRPCCallOptions.m b/src/objective-c/GRPCClient/GRPCCallOptions.m index 5a270407620..392e42a9d47 100644 --- a/src/objective-c/GRPCClient/GRPCCallOptions.m +++ b/src/objective-c/GRPCClient/GRPCCallOptions.m @@ -115,7 +115,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) { - (instancetype)init { return [self initWithServerAuthority:kDefaultServerAuthority timeout:kDefaultTimeout - flowControlEnabled:kDefaultFlowControlEnabled + flowControlEnabled:kDefaultFlowControlEnabled interceptorFactories:kDefaultInterceptorFactories oauth2AccessToken:kDefaultOauth2AccessToken authTokenProvider:kDefaultAuthTokenProvider @@ -142,7 +142,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) { - (instancetype)initWithServerAuthority:(NSString *)serverAuthority timeout:(NSTimeInterval)timeout - flowControlEnabled:(BOOL)flowControlEnabled + flowControlEnabled:(BOOL)flowControlEnabled interceptorFactories:(NSArray *)interceptorFactories oauth2AccessToken:(NSString *)oauth2AccessToken authTokenProvider:(id)authTokenProvider @@ -205,7 +205,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) { GRPCCallOptions *newOptions = [[GRPCCallOptions allocWithZone:zone] initWithServerAuthority:_serverAuthority timeout:_timeout - flowControlEnabled:_flowControlEnabled + flowControlEnabled:_flowControlEnabled interceptorFactories:_interceptorFactories oauth2AccessToken:_oauth2AccessToken authTokenProvider:_authTokenProvider @@ -235,7 +235,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) { GRPCMutableCallOptions *newOptions = [[GRPCMutableCallOptions allocWithZone:zone] initWithServerAuthority:[_serverAuthority copy] timeout:_timeout - flowControlEnabled:_flowControlEnabled + flowControlEnabled:_flowControlEnabled interceptorFactories:_interceptorFactories oauth2AccessToken:[_oauth2AccessToken copy] authTokenProvider:_authTokenProvider @@ -344,7 +344,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) { - (instancetype)init { return [self initWithServerAuthority:kDefaultServerAuthority timeout:kDefaultTimeout - flowControlEnabled:kDefaultFlowControlEnabled + flowControlEnabled:kDefaultFlowControlEnabled interceptorFactories:kDefaultInterceptorFactories oauth2AccessToken:kDefaultOauth2AccessToken authTokenProvider:kDefaultAuthTokenProvider @@ -373,7 +373,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) { GRPCCallOptions *newOptions = [[GRPCCallOptions allocWithZone:zone] initWithServerAuthority:_serverAuthority timeout:_timeout - flowControlEnabled:_flowControlEnabled + flowControlEnabled:_flowControlEnabled interceptorFactories:_interceptorFactories oauth2AccessToken:_oauth2AccessToken authTokenProvider:_authTokenProvider @@ -403,7 +403,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) { GRPCMutableCallOptions *newOptions = [[GRPCMutableCallOptions allocWithZone:zone] initWithServerAuthority:_serverAuthority timeout:_timeout - flowControlEnabled:_flowControlEnabled + flowControlEnabled:_flowControlEnabled interceptorFactories:_interceptorFactories oauth2AccessToken:_oauth2AccessToken authTokenProvider:_authTokenProvider diff --git a/src/objective-c/GRPCClient/GRPCInterceptor.h b/src/objective-c/GRPCClient/GRPCInterceptor.h index 8e23ffc5361..931f6e4aa5a 100644 --- a/src/objective-c/GRPCClient/GRPCInterceptor.h +++ b/src/objective-c/GRPCClient/GRPCInterceptor.h @@ -175,9 +175,10 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; ++ (instancetype) new NS_UNAVAILABLE; -- (nullable instancetype)initWithNextInerceptor:(id)nextInterceptor NS_DESIGNATED_INITIALIZER; +- (nullable instancetype)initWithNextInerceptor:(id)nextInterceptor + NS_DESIGNATED_INITIALIZER; /** Set the previous interceptor in the chain. Can only be set once. */ - (void)setPreviousInterceptor:(id)previousInterceptor; @@ -213,7 +214,7 @@ NS_ASSUME_NONNULL_BEGIN /** Forward call close and trailing metadata to the previous interceptor in the chain */ - (void)forwardPreviousInterceptorCloseWithTrailingMetadata: -(nullable NSDictionary *)trailingMetadata + (nullable NSDictionary *)trailingMetadata error:(nullable NSError *)error; /** Forward write completion to the previous interceptor in the chain */ @@ -235,7 +236,7 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; ++ (instancetype) new NS_UNAVAILABLE; /** * Initialize the interceptor with the next interceptor in the chain, and provide the dispatch queue @@ -243,7 +244,8 @@ NS_ASSUME_NONNULL_BEGIN */ - (nullable instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager requestDispatchQueue:(dispatch_queue_t)requestDispatchQueue - responseDispatchQueue:(dispatch_queue_t)responseDispatchQueue NS_DESIGNATED_INITIALIZER; + responseDispatchQueue:(dispatch_queue_t)responseDispatchQueue + NS_DESIGNATED_INITIALIZER; // Default implementation of GRPCInterceptorInterface diff --git a/src/objective-c/GRPCClient/GRPCInterceptor.m b/src/objective-c/GRPCClient/GRPCInterceptor.m index ac0b69f14eb..1c1f0b53114 100644 --- a/src/objective-c/GRPCClient/GRPCInterceptor.m +++ b/src/objective-c/GRPCClient/GRPCInterceptor.m @@ -47,8 +47,7 @@ if ([_nextInterceptor respondsToSelector:@selector(startWithRequestOptions:callOptions:)]) { id copiedNextInterceptor = _nextInterceptor; dispatch_async(copiedNextInterceptor.requestDispatchQueue, ^{ - [copiedNextInterceptor startWithRequestOptions:requestOptions - callOptions:callOptions]; + [copiedNextInterceptor startWithRequestOptions:requestOptions callOptions:callOptions]; }); } } @@ -114,13 +113,12 @@ /** Forward call close and trailing metadata to the previous interceptor in the chain */ - (void)forwardPreviousInterceptorCloseWithTrailingMetadata: -(nullable NSDictionary *)trailingMetadata + (nullable NSDictionary *)trailingMetadata error:(nullable NSError *)error { if ([_previousInterceptor respondsToSelector:@selector(didCloseWithTrailingMetadata:error:)]) { id copiedPreviousInterceptor = _previousInterceptor; dispatch_async(copiedPreviousInterceptor.dispatchQueue, ^{ - [copiedPreviousInterceptor didCloseWithTrailingMetadata:trailingMetadata - error:error]; + [copiedPreviousInterceptor didCloseWithTrailingMetadata:trailingMetadata error:error]; }); } } @@ -165,8 +163,7 @@ - (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions callOptions:(GRPCCallOptions *)callOptions { - [_manager startNextInterceptorWithRequest:requestOptions - callOptions:callOptions]; + [_manager startNextInterceptorWithRequest:requestOptions callOptions:callOptions]; } - (void)writeData:(id)data { @@ -179,13 +176,15 @@ - (void)cancel { [_manager cancelNextInterceptor]; - [_manager forwardPreviousInterceptorCloseWithTrailingMetadata:nil - error:[NSError errorWithDomain:kGRPCErrorDomain - code:GRPCErrorCodeCancelled - userInfo:@{ - NSLocalizedDescriptionKey : - @"Canceled" - }]]; + [_manager + forwardPreviousInterceptorCloseWithTrailingMetadata:nil + error:[NSError + errorWithDomain:kGRPCErrorDomain + code:GRPCErrorCodeCancelled + userInfo:@{ + NSLocalizedDescriptionKey : + @"Canceled" + }]]; [_manager shutDown]; } @@ -193,13 +192,13 @@ [_manager receiveNextInterceptorMessages:numberOfMessages]; } - - (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata { [_manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; } - (void)didReceiveRawMessage:(id)message { - NSAssert(NO, @"The method didReceiveRawMessage is deprecated and cannot be used with interceptor"); + NSAssert(NO, + @"The method didReceiveRawMessage is deprecated and cannot be used with interceptor"); NSLog(@"The method didReceiveRawMessage is deprecated and cannot be used with interceptor"); abort(); } @@ -208,10 +207,8 @@ [_manager forwardPreviousIntercetporWithData:data]; } -- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata - error:(NSError *)error { - [_manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata - error:error]; +- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error { + [_manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata error:error]; [_manager shutDown]; } diff --git a/src/objective-c/GRPCClient/private/GRPCCallInternal.m b/src/objective-c/GRPCClient/private/GRPCCallInternal.m index 06912cc7fa9..e4edcedf36c 100644 --- a/src/objective-c/GRPCClient/private/GRPCCallInternal.m +++ b/src/objective-c/GRPCClient/private/GRPCCallInternal.m @@ -35,12 +35,12 @@ - (instancetype)init { if ((self = [super init])) { - // Set queue QoS only when iOS version is 8.0 or above and Xcode version is 9.0 or above + // Set queue QoS only when iOS version is 8.0 or above and Xcode version is 9.0 or above #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 { @@ -53,7 +53,7 @@ } - (void)setResponseHandler:(id)responseHandler { - @synchronized (self) { + @synchronized(self) { NSAssert(!_started, @"Call already started."); if (_started) { return; @@ -84,7 +84,7 @@ return; } - @synchronized (self) { + @synchronized(self) { NSAssert(_handler != nil, @"Response handler required."); if (_handler == nil) { NSLog(@"Invalid response handler."); @@ -172,7 +172,7 @@ } }; id responseWriteable = - [[GRXWriteable alloc] initWithValueHandler:valueHandler completionHandler:completionHandler]; + [[GRXWriteable alloc] initWithValueHandler:valueHandler completionHandler:completionHandler]; [copiedCall startWithWriteable:responseWriteable]; } @@ -197,9 +197,9 @@ error:[NSError errorWithDomain:kGRPCErrorDomain code:GRPCErrorCodeCancelled userInfo:@{ - NSLocalizedDescriptionKey : - @"Canceled by app" - }]]; + NSLocalizedDescriptionKey : + @"Canceled by app" + }]]; }); } else { _handler = nil; diff --git a/src/objective-c/examples/InterceptorSample/InterceptorSample/AppDelegate.h b/src/objective-c/examples/InterceptorSample/InterceptorSample/AppDelegate.h index c4e433787e6..183abcf4ec8 100644 --- a/src/objective-c/examples/InterceptorSample/InterceptorSample/AppDelegate.h +++ b/src/objective-c/examples/InterceptorSample/InterceptorSample/AppDelegate.h @@ -18,8 +18,8 @@ #import -@interface AppDelegate : UIResponder +@interface AppDelegate : UIResponder -@property (strong, nonatomic) UIWindow *window; +@property(strong, nonatomic) UIWindow* window; @end diff --git a/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.h b/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.h index a2c9d668c14..a1de74cdb1f 100644 --- a/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.h +++ b/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.h @@ -68,10 +68,12 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; ++ (instancetype) new NS_UNAVAILABLE; -- (nullable instancetype)initWithInterceptorManager:(GRPCInterceptorManager * _Nonnull)intercepterManager - cacheContext:(CacheContext * _Nonnull)cacheContext NS_DESIGNATED_INITIALIZER; +- (nullable instancetype)initWithInterceptorManager: + (GRPCInterceptorManager *_Nonnull)intercepterManager + cacheContext:(CacheContext *_Nonnull)cacheContext + NS_DESIGNATED_INITIALIZER; // implementation of GRPCInterceptorInterface - (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions diff --git a/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.m b/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.m index fea770af486..082bd303dec 100644 --- a/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.m +++ b/src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.m @@ -19,7 +19,7 @@ #import "CacheInterceptor.h" @implementation RequestCacheEntry { - @protected + @protected NSString *_path; id _message; } @@ -67,7 +67,7 @@ @end @implementation ResponseCacheEntry { - @protected + @protected NSDate *_deadline; NSDictionary *_headers; id _message; @@ -138,21 +138,19 @@ } - (GRPCInterceptor *)createInterceptorWithManager:(GRPCInterceptorManager *)interceptorManager { - return [[CacheInterceptor alloc] initWithInterceptorManager:interceptorManager - cacheContext:self]; + return [[CacheInterceptor alloc] initWithInterceptorManager:interceptorManager cacheContext:self]; } - (ResponseCacheEntry *)getCachedResponseForRequest:(RequestCacheEntry *)request { ResponseCacheEntry *response = nil; - @synchronized (self) { + @synchronized(self) { response = [_cache objectForKey:request]; } return response; } -- (void)setCachedResponse:(ResponseCacheEntry *)response - forRequest:(RequestCacheEntry *)request { - @synchronized (self) { +- (void)setCachedResponse:(ResponseCacheEntry *)response forRequest:(RequestCacheEntry *)request { + @synchronized(self) { [_cache setObject:response forKey:request]; } } @@ -181,8 +179,8 @@ return _dispatchQueue; } -- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager * _Nonnull)intercepterManager - cacheContext:(CacheContext * _Nonnull)cacheContext { +- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *_Nonnull)intercepterManager + cacheContext:(CacheContext *_Nonnull)cacheContext { if ((self = [super initWithInterceptorManager:intercepterManager requestDispatchQueue:dispatch_get_main_queue() responseDispatchQueue:dispatch_get_main_queue()])) { @@ -198,7 +196,8 @@ return self; } -- (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions callOptions:(GRPCCallOptions *)callOptions { +- (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions + callOptions:(GRPCCallOptions *)callOptions { NSLog(@"start"); if (requestOptions.safety != GRPCCallSafetyCacheableRequest) { NSLog(@"no cache"); @@ -235,8 +234,7 @@ _response = [[_context getCachedResponseForRequest:_request] copy]; NSLog(@"Read cache for %@", _request); if (!_response) { - [_manager startNextInterceptorWithRequest:_requestOptions - callOptions:_callOptions]; + [_manager startNextInterceptorWithRequest:_requestOptions callOptions:_callOptions]; [_manager writeNextInterceptorWithData:_requestMessage]; [_manager finishNextInterceptor]; } else { @@ -256,7 +254,9 @@ if ([key.lowercaseString isEqualToString:@"cache-control"]) { NSArray *cacheControls = [initialMetadata[key] componentsSeparatedByString:@","]; for (NSString *option in cacheControls) { - NSString *trimmedOption = [option stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]]; + NSString *trimmedOption = + [option stringByTrimmingCharactersInSet:[NSCharacterSet + characterSetWithCharactersInString:@" "]]; if ([trimmedOption.lowercaseString isEqualToString:@"no-cache"] || [trimmedOption.lowercaseString isEqualToString:@"no-store"] || [trimmedOption.lowercaseString isEqualToString:@"no-transform"]) { diff --git a/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.h b/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.h index 79de577af81..0aa0b2a73a7 100644 --- a/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.h +++ b/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.h @@ -20,6 +20,4 @@ @interface ViewController : UIViewController - @end - diff --git a/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.m b/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.m index 79e53313e55..9da6dedbf9e 100644 --- a/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.m +++ b/src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.m @@ -19,8 +19,8 @@ #import "ViewController.h" #import -#import #import +#import #import "CacheInterceptor.h" @@ -64,7 +64,6 @@ static NSString *const kService = @"TestService"; [call start]; [call writeData:[request data]]; [call finish]; - } - (dispatch_queue_t)dispatchQueue { diff --git a/src/objective-c/examples/InterceptorSample/InterceptorSample/main.m b/src/objective-c/examples/InterceptorSample/InterceptorSample/main.m index 62a9f8e6efb..2797c6f17f2 100644 --- a/src/objective-c/examples/InterceptorSample/InterceptorSample/main.m +++ b/src/objective-c/examples/InterceptorSample/InterceptorSample/main.m @@ -19,8 +19,8 @@ #import #import "AppDelegate.h" -int main(int argc, char * argv[]) { +int main(int argc, char* argv[]) { @autoreleasepool { - return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } diff --git a/src/objective-c/tests/InteropTests.m b/src/objective-c/tests/InteropTests.m index 42365d2be2c..93316c0ff5a 100644 --- a/src/objective-c/tests/InteropTests.m +++ b/src/objective-c/tests/InteropTests.m @@ -171,15 +171,20 @@ BOOL isRemoteInteropTest(NSString *host) { @interface HookInterceptorFactory : NSObject -- (instancetype)initWithDispatchQueue:(dispatch_queue_t)dispatchQueue - startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook - writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook - finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook - receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook - responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook - responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook - responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook - didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; +- (instancetype) + initWithDispatchQueue:(dispatch_queue_t)dispatchQueue + startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager))startHook + writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook + finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook +receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, + GRPCInterceptorManager *manager))receiveNextMessagesHook + responseHeaderHook:(void (^)(NSDictionary *initialMetadata, + GRPCInterceptorManager *manager))responseHeaderHook + responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook + responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager))responseCloseHook + didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; - (GRPCInterceptor *)createInterceptorWithManager:(GRPCInterceptorManager *)interceptorManager; @@ -187,40 +192,53 @@ BOOL isRemoteInteropTest(NSString *host) { @interface HookIntercetpor : GRPCInterceptor -- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager - dispatchQueue:(dispatch_queue_t)dispatchQueue - startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook - writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook - finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook - receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook - responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook - responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook - responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook - didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; +- (instancetype) +initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager + dispatchQueue:(dispatch_queue_t)dispatchQueue + startHook:(void (^)(GRPCRequestOptions *requestOptions, + GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager))startHook + writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook + finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook + receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, + GRPCInterceptorManager *manager))receiveNextMessagesHook + responseHeaderHook:(void (^)(NSDictionary *initialMetadata, + GRPCInterceptorManager *manager))responseHeaderHook + responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook + responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager))responseCloseHook + didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; @end @implementation HookInterceptorFactory { - void (^_startHook)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager); + void (^_startHook)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager); void (^_writeDataHook)(NSData *data, GRPCInterceptorManager *manager); void (^_finishHook)(GRPCInterceptorManager *manager); void (^_receiveNextMessagesHook)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager); void (^_responseHeaderHook)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager); void (^_responseDataHook)(NSData *data, GRPCInterceptorManager *manager); - void (^_responseCloseHook)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager); + void (^_responseCloseHook)(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager); void (^_didWriteDataHook)(GRPCInterceptorManager *manager); dispatch_queue_t _dispatchQueue; } -- (instancetype)initWithDispatchQueue:(dispatch_queue_t)dispatchQueue - startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook - writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook - finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook - receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook - responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook - responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook - responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook - didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { +- (instancetype) + initWithDispatchQueue:(dispatch_queue_t)dispatchQueue + startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager))startHook + writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook + finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook +receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, + GRPCInterceptorManager *manager))receiveNextMessagesHook + responseHeaderHook:(void (^)(NSDictionary *initialMetadata, + GRPCInterceptorManager *manager))responseHeaderHook + responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook + responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager))responseCloseHook + didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { if ((self = [super init])) { _dispatchQueue = dispatchQueue; _startHook = startHook; @@ -252,13 +270,15 @@ BOOL isRemoteInteropTest(NSString *host) { @end @implementation HookIntercetpor { - void (^_startHook)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager); + void (^_startHook)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager); void (^_writeDataHook)(NSData *data, GRPCInterceptorManager *manager); void (^_finishHook)(GRPCInterceptorManager *manager); void (^_receiveNextMessagesHook)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager); void (^_responseHeaderHook)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager); void (^_responseDataHook)(NSData *data, GRPCInterceptorManager *manager); - void (^_responseCloseHook)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager); + void (^_responseCloseHook)(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager); void (^_didWriteDataHook)(GRPCInterceptorManager *manager); GRPCInterceptorManager *_manager; dispatch_queue_t _dispatchQueue; @@ -272,17 +292,25 @@ BOOL isRemoteInteropTest(NSString *host) { return _dispatchQueue; } -- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager - dispatchQueue:(dispatch_queue_t)dispatchQueue - startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook - writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook - finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook - receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook - responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook - responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook - responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook - didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { - if ((self = [super initWithInterceptorManager:interceptorManager requestDispatchQueue:dispatchQueue responseDispatchQueue:dispatchQueue])) { +- (instancetype) +initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager + dispatchQueue:(dispatch_queue_t)dispatchQueue + startHook:(void (^)(GRPCRequestOptions *requestOptions, + GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager))startHook + writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook + finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook + receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, + GRPCInterceptorManager *manager))receiveNextMessagesHook + responseHeaderHook:(void (^)(NSDictionary *initialMetadata, + GRPCInterceptorManager *manager))responseHeaderHook + responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook + responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager))responseCloseHook + didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { + if ((self = [super initWithInterceptorManager:interceptorManager + requestDispatchQueue:dispatchQueue + responseDispatchQueue:dispatchQueue])) { _startHook = startHook; _writeDataHook = writeDataHook; _finishHook = finishHook; @@ -1271,39 +1299,39 @@ BOOL isRemoteInteropTest(NSString *host) { options.interceptorFactories = @[ [[DefaultInterceptorFactory alloc] init] ]; __block GRPCStreamingProtoCall *call = [_service - fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] - initWithInitialMetadataCallback:nil - messageCallback:^(id message) { - XCTAssertLessThan(index, 4, - @"More than 4 responses received."); - id expected = [RMTStreamingOutputCallResponse - messageWithPayloadSize:responses[index]]; - XCTAssertEqualObjects(message, expected); - index += 1; - if (index < 4) { - id request = [RMTStreamingOutputCallRequest - messageWithPayloadSize:requests[index] - requestedResponseSize:responses[index]]; - [call writeMessage:request]; - } else { - [call finish]; - } - // DEBUG - NSLog(@"Received message"); - } - closeCallback:^(NSDictionary *trailingMetadata, - NSError *error) { - XCTAssertNil(error, - @"Finished with unexpected error: %@", - error); - XCTAssertEqual(index, 4, - @"Received %i responses instead of 4.", - index); - [expectation fulfill]; - // DEBUG - NSLog(@"Received close"); - }] - callOptions:options]; + fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] + initWithInitialMetadataCallback:nil + messageCallback:^(id message) { + XCTAssertLessThan(index, 4, + @"More than 4 responses received."); + id expected = [RMTStreamingOutputCallResponse + messageWithPayloadSize:responses[index]]; + XCTAssertEqualObjects(message, expected); + index += 1; + if (index < 4) { + id request = [RMTStreamingOutputCallRequest + messageWithPayloadSize:requests[index] + requestedResponseSize:responses[index]]; + [call writeMessage:request]; + } else { + [call finish]; + } + // DEBUG + NSLog(@"Received message"); + } + closeCallback:^(NSDictionary *trailingMetadata, + NSError *error) { + XCTAssertNil(error, + @"Finished with unexpected error: %@", + error); + XCTAssertEqual(index, 4, + @"Received %i responses instead of 4.", + index); + [expectation fulfill]; + // DEBUG + NSLog(@"Received close"); + }] + callOptions:options]; [call start]; [call writeMessage:request]; @@ -1322,54 +1350,56 @@ BOOL isRemoteInteropTest(NSString *host) { __block NSUInteger responseDataCount = 0; __block NSUInteger responseCloseCount = 0; __block NSUInteger didWriteDataCount = 0; - id factory = - [[HookInterceptorFactory alloc] initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL) - startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager) { - startCount++; - NSLog(@"Interceptor - started call, %@, %@", requestOptions, callOptions); - XCTAssertEqualObjects(requestOptions.host, [[self class] host]); - XCTAssertEqualObjects(requestOptions.path, @"/grpc.testing.TestService/FullDuplexCall"); - XCTAssertEqual(requestOptions.safety, GRPCCallSafetyDefault); - [manager startNextInterceptorWithRequest:[requestOptions copy] callOptions:[callOptions copy]]; - } - writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { - writeDataCount++; - NSLog(@"Interceptor - send data, %@", data); - XCTAssertNotEqual(data.length, 0); - [manager writeNextInterceptorWithData:data]; - } - finishHook:^(GRPCInterceptorManager *manager) { - finishCount++; - NSLog(@"Interceptor - finish call"); - [manager finishNextInterceptor]; - } - receiveNextMessagesHook:^(NSUInteger numberOfMessages, GRPCInterceptorManager *manager) { - receiveNextMessageCount++; - NSLog(@"Interceptor - receive next messages, %lu", (unsigned long)numberOfMessages); - [manager receiveNextInterceptorMessages:numberOfMessages]; - } - responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) { - responseHeaderCount++; - NSLog(@"Interceptor - received initial metadata, %@", initialMetadata); - [manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; - } - responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) { - responseDataCount++; - NSLog(@"Interceptor - received data, %@", data); - XCTAssertNotEqual(data.length, 0); - [manager forwardPreviousIntercetporWithData:data]; - } - responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager) { - responseCloseCount++; - NSLog(@"Interceptor - received close, %@, %@", trailingMetadata, error); - [manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata - error:error]; - } - didWriteDataHook:^(GRPCInterceptorManager *manager) { - didWriteDataCount++; - NSLog(@"Interceptor - received did-write-data"); - [manager forwardPreviousInterceptorDidWriteData]; - }]; + id factory = [[HookInterceptorFactory alloc] + initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL) + startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager) { + startCount++; + NSLog(@"Interceptor - started call, %@, %@", requestOptions, callOptions); + XCTAssertEqualObjects(requestOptions.host, [[self class] host]); + XCTAssertEqualObjects(requestOptions.path, @"/grpc.testing.TestService/FullDuplexCall"); + XCTAssertEqual(requestOptions.safety, GRPCCallSafetyDefault); + [manager startNextInterceptorWithRequest:[requestOptions copy] + callOptions:[callOptions copy]]; + } + writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { + writeDataCount++; + NSLog(@"Interceptor - send data, %@", data); + XCTAssertNotEqual(data.length, 0); + [manager writeNextInterceptorWithData:data]; + } + finishHook:^(GRPCInterceptorManager *manager) { + finishCount++; + NSLog(@"Interceptor - finish call"); + [manager finishNextInterceptor]; + } + receiveNextMessagesHook:^(NSUInteger numberOfMessages, GRPCInterceptorManager *manager) { + receiveNextMessageCount++; + NSLog(@"Interceptor - receive next messages, %lu", (unsigned long)numberOfMessages); + [manager receiveNextInterceptorMessages:numberOfMessages]; + } + responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) { + responseHeaderCount++; + NSLog(@"Interceptor - received initial metadata, %@", initialMetadata); + [manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; + } + responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) { + responseDataCount++; + NSLog(@"Interceptor - received data, %@", data); + XCTAssertNotEqual(data.length, 0); + [manager forwardPreviousIntercetporWithData:data]; + } + responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager) { + responseCloseCount++; + NSLog(@"Interceptor - received close, %@, %@", trailingMetadata, error); + [manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata error:error]; + } + didWriteDataHook:^(GRPCInterceptorManager *manager) { + didWriteDataCount++; + NSLog(@"Interceptor - received did-write-data"); + [manager forwardPreviousInterceptorDidWriteData]; + }]; NSArray *requests = @[ @1, @2, @3, @4 ]; NSArray *responses = @[ @1, @2, @3, @4 ]; @@ -1387,41 +1417,41 @@ BOOL isRemoteInteropTest(NSString *host) { __block BOOL canWriteData = NO; __block GRPCStreamingProtoCall *call = [_service - fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] - initWithInitialMetadataCallback:nil - messageCallback:^(id message) { - XCTAssertLessThan(index, 4, - @"More than 4 responses received."); - id expected = [RMTStreamingOutputCallResponse - messageWithPayloadSize:responses[index]]; - XCTAssertEqualObjects(message, expected); - index += 1; - if (index < 4) { - id request = [RMTStreamingOutputCallRequest - messageWithPayloadSize:requests[index] - requestedResponseSize:responses[index]]; - XCTAssertTrue(canWriteData); - canWriteData = NO; - [call writeMessage:request]; - [call receiveNextMessage]; - } else { - [call finish]; - } - } - closeCallback:^(NSDictionary *trailingMetadata, - NSError *error) { - XCTAssertNil(error, - @"Finished with unexpected error: %@", - error); - XCTAssertEqual(index, 4, - @"Received %i responses instead of 4.", - index); - [expectation fulfill]; - } - writeMessageCallback:^{ - canWriteData = YES; - }] - callOptions:options]; + fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] + initWithInitialMetadataCallback:nil + messageCallback:^(id message) { + XCTAssertLessThan(index, 4, + @"More than 4 responses received."); + id expected = [RMTStreamingOutputCallResponse + messageWithPayloadSize:responses[index]]; + XCTAssertEqualObjects(message, expected); + index += 1; + if (index < 4) { + id request = [RMTStreamingOutputCallRequest + messageWithPayloadSize:requests[index] + requestedResponseSize:responses[index]]; + XCTAssertTrue(canWriteData); + canWriteData = NO; + [call writeMessage:request]; + [call receiveNextMessage]; + } else { + [call finish]; + } + } + closeCallback:^(NSDictionary *trailingMetadata, + NSError *error) { + XCTAssertNil(error, + @"Finished with unexpected error: %@", + error); + XCTAssertEqual(index, 4, + @"Received %i responses instead of 4.", + index); + [expectation fulfill]; + } + writeMessageCallback:^{ + canWriteData = YES; + }] + callOptions:options]; [call start]; [call receiveNextMessage]; [call writeMessage:request]; @@ -1453,45 +1483,53 @@ BOOL isRemoteInteropTest(NSString *host) { __block NSUInteger responseHeaderCount = 0; __block NSUInteger responseDataCount = 0; __block NSUInteger responseCloseCount = 0; - id factory = - [[HookInterceptorFactory alloc] initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL) - startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager) { - startCount++; - [manager startNextInterceptorWithRequest:[requestOptions copy] callOptions:[callOptions copy]]; - } - writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { - writeDataCount++; - if (index < kCancelAfterWrites) { - [manager writeNextInterceptorWithData:data]; - } else if (index == kCancelAfterWrites) { - [manager cancelNextInterceptor]; - [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]] data]]; - } else { // (index > kCancelAfterWrites) - [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]] data]]; - } - } - finishHook:^(GRPCInterceptorManager *manager) { - finishCount++; - // finish must happen after the hijacking, so directly reply with a close - [manager forwardPreviousInterceptorCloseWithTrailingMetadata:@{ @"grpc-status" : @"0" } error:nil]; - } - receiveNextMessagesHook:nil - responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) { - responseHeaderCount++; - [manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; - } - responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) { - responseDataCount++; - [manager forwardPreviousIntercetporWithData:data]; - } - responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager) { - responseCloseCount++; - // since we canceled the call, it should return cancel error - XCTAssertNil(trailingMetadata); - XCTAssertNotNil(error); - XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED); - } - didWriteDataHook:nil]; + id factory = [[HookInterceptorFactory alloc] + initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL) + startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, + GRPCInterceptorManager *manager) { + startCount++; + [manager startNextInterceptorWithRequest:[requestOptions copy] + callOptions:[callOptions copy]]; + } + writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { + writeDataCount++; + if (index < kCancelAfterWrites) { + [manager writeNextInterceptorWithData:data]; + } else if (index == kCancelAfterWrites) { + [manager cancelNextInterceptor]; + [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse + messageWithPayloadSize:responses[index]] + data]]; + } else { // (index > kCancelAfterWrites) + [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse + messageWithPayloadSize:responses[index]] + data]]; + } + } + finishHook:^(GRPCInterceptorManager *manager) { + finishCount++; + // finish must happen after the hijacking, so directly reply with a close + [manager forwardPreviousInterceptorCloseWithTrailingMetadata:@{@"grpc-status" : @"0"} + error:nil]; + } + receiveNextMessagesHook:nil + responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) { + responseHeaderCount++; + [manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; + } + responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) { + responseDataCount++; + [manager forwardPreviousIntercetporWithData:data]; + } + responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, + GRPCInterceptorManager *manager) { + responseCloseCount++; + // since we canceled the call, it should return cancel error + XCTAssertNil(trailingMetadata); + XCTAssertNotNil(error); + XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED); + } + didWriteDataHook:nil]; NSArray *requests = @[ @1, @2, @3, @4 ]; @@ -1504,36 +1542,36 @@ BOOL isRemoteInteropTest(NSString *host) { options.interceptorFactories = @[ [[DefaultInterceptorFactory alloc] init], factory ]; __block GRPCStreamingProtoCall *call = [_service - fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] - initWithInitialMetadataCallback:nil - messageCallback:^(id message) { - XCTAssertLessThan(index, 4, - @"More than 4 responses received."); - id expected = [RMTStreamingOutputCallResponse - messageWithPayloadSize:responses[index]]; - XCTAssertEqualObjects(message, expected); - index += 1; - if (index < 4) { - id request = [RMTStreamingOutputCallRequest - messageWithPayloadSize:requests[index] - requestedResponseSize:responses[index]]; - [call writeMessage:request]; - [call receiveNextMessage]; - } else { - [call finish]; - } - } - closeCallback:^(NSDictionary *trailingMetadata, - NSError *error) { - XCTAssertNil(error, - @"Finished with unexpected error: %@", - error); - XCTAssertEqual(index, 4, - @"Received %i responses instead of 4.", - index); - [expectation fulfill]; - }] - callOptions:options]; + fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] + initWithInitialMetadataCallback:nil + messageCallback:^(id message) { + XCTAssertLessThan(index, 4, + @"More than 4 responses received."); + id expected = [RMTStreamingOutputCallResponse + messageWithPayloadSize:responses[index]]; + XCTAssertEqualObjects(message, expected); + index += 1; + if (index < 4) { + id request = [RMTStreamingOutputCallRequest + messageWithPayloadSize:requests[index] + requestedResponseSize:responses[index]]; + [call writeMessage:request]; + [call receiveNextMessage]; + } else { + [call finish]; + } + } + closeCallback:^(NSDictionary *trailingMetadata, + NSError *error) { + XCTAssertNil(error, + @"Finished with unexpected error: %@", + error); + XCTAssertEqual(index, 4, + @"Received %i responses instead of 4.", + index); + [expectation fulfill]; + }] + callOptions:options]; [call start]; [call receiveNextMessage]; [call writeMessage:request];