clang-format

pull/18852/head
Muxi Yan 6 years ago
parent cb82833038
commit 6a3ea4934b
  1. 21
      src/objective-c/GRPCClient/GRPCCall.m
  2. 2
      src/objective-c/GRPCClient/GRPCCallOptions.h
  3. 12
      src/objective-c/GRPCClient/GRPCInterceptor.h
  4. 27
      src/objective-c/GRPCClient/GRPCInterceptor.m
  5. 4
      src/objective-c/GRPCClient/private/GRPCCallInternal.m
  6. 4
      src/objective-c/examples/InterceptorSample/InterceptorSample/AppDelegate.h
  7. 8
      src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.h
  8. 24
      src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.m
  9. 2
      src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.h
  10. 3
      src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.m
  11. 2
      src/objective-c/examples/InterceptorSample/InterceptorSample/main.m
  12. 118
      src/objective-c/tests/InteropTests.m

@ -18,8 +18,8 @@
#import "GRPCCall.h" #import "GRPCCall.h"
#import "GRPCCall+OAuth2.h" #import "GRPCCall+OAuth2.h"
#import "GRPCInterceptor.h"
#import "GRPCCallOptions.h" #import "GRPCCallOptions.h"
#import "GRPCInterceptor.h"
#import <RxLibrary/GRXBufferedPipe.h> #import <RxLibrary/GRXBufferedPipe.h>
#import <RxLibrary/GRXConcurrentWriteable.h> #import <RxLibrary/GRXConcurrentWriteable.h>
@ -28,6 +28,8 @@
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include <grpc/support/time.h> #include <grpc/support/time.h>
#import "private/GRPCCall+V2API.h"
#import "private/GRPCCallInternal.h"
#import "private/GRPCChannelPool.h" #import "private/GRPCChannelPool.h"
#import "private/GRPCCompletionQueue.h" #import "private/GRPCCompletionQueue.h"
#import "private/GRPCConnectivityMonitor.h" #import "private/GRPCConnectivityMonitor.h"
@ -37,8 +39,6 @@
#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/GRPCCall+V2API.h"
#import "private/GRPCCallInternal.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,
@ -142,8 +142,10 @@ const char *kCFStreamVarName = "grpc_cfstream";
[internalCall setResponseHandler:_responseHandler]; [internalCall setResponseHandler:_responseHandler];
} else { } else {
for (int i = (int)interceptorFactories.count - 1; i >= 0; i--) { for (int i = (int)interceptorFactories.count - 1; i >= 0; i--) {
GRPCInterceptorManager *manager = [[GRPCInterceptorManager alloc] initWithNextInerceptor:nextInterceptor]; GRPCInterceptorManager *manager =
GRPCInterceptor *interceptor = [interceptorFactories[i] createInterceptorWithManager:manager]; [[GRPCInterceptorManager alloc] initWithNextInerceptor:nextInterceptor];
GRPCInterceptor *interceptor =
[interceptorFactories[i] createInterceptorWithManager:manager];
NSAssert(interceptor != nil, @"Failed to create interceptor"); NSAssert(interceptor != nil, @"Failed to create interceptor");
if (interceptor == nil) { if (interceptor == nil) {
return nil; return nil;
@ -180,8 +182,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
GRPCCallOptions *callOptions = [_actualCallOptions copy]; GRPCCallOptions *callOptions = [_actualCallOptions copy];
if ([copiedFirstInterceptor respondsToSelector:@selector(startWithRequestOptions:callOptions:)]) { if ([copiedFirstInterceptor respondsToSelector:@selector(startWithRequestOptions:callOptions:)]) {
dispatch_async(copiedFirstInterceptor.requestDispatchQueue, ^{ dispatch_async(copiedFirstInterceptor.requestDispatchQueue, ^{
[copiedFirstInterceptor startWithRequestOptions:requestOptions [copiedFirstInterceptor startWithRequestOptions:requestOptions callOptions:callOptions];
callOptions:callOptions];
}); });
} }
} }
@ -200,7 +201,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)writeData:(id)data { - (void)writeData:(id)data {
id<GRPCInterceptorInterface> copiedFirstInterceptor; id<GRPCInterceptorInterface> copiedFirstInterceptor;
@synchronized (self) { @synchronized(self) {
copiedFirstInterceptor = _firstInterceptor; copiedFirstInterceptor = _firstInterceptor;
} }
if ([copiedFirstInterceptor respondsToSelector:@selector(writeData:)]) { if ([copiedFirstInterceptor respondsToSelector:@selector(writeData:)]) {
@ -212,7 +213,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)finish { - (void)finish {
id<GRPCInterceptorInterface> copiedFirstInterceptor; id<GRPCInterceptorInterface> copiedFirstInterceptor;
@synchronized (self) { @synchronized(self) {
copiedFirstInterceptor = _firstInterceptor; copiedFirstInterceptor = _firstInterceptor;
} }
if ([copiedFirstInterceptor respondsToSelector:@selector(finish)]) { if ([copiedFirstInterceptor respondsToSelector:@selector(finish)]) {
@ -224,7 +225,7 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)receiveNextMessages:(NSUInteger)numberOfMessages { - (void)receiveNextMessages:(NSUInteger)numberOfMessages {
id<GRPCInterceptorInterface> copiedFirstInterceptor; id<GRPCInterceptorInterface> copiedFirstInterceptor;
@synchronized (self) { @synchronized(self) {
copiedFirstInterceptor = _firstInterceptor; copiedFirstInterceptor = _firstInterceptor;
} }
if ([copiedFirstInterceptor respondsToSelector:@selector(receiveNextMessages:)]) { if ([copiedFirstInterceptor respondsToSelector:@selector(receiveNextMessages:)]) {

@ -106,7 +106,6 @@ typedef NS_ENUM(NSUInteger, GRPCTransportType) {
*/ */
@property(copy, readonly) NSArray *interceptorFactories; @property(copy, readonly) NSArray *interceptorFactories;
// OAuth2 parameters. Users of gRPC may specify one of the following two parameters. // 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; @property(copy, readwrite) NSArray *interceptorFactories;
// OAuth2 parameters. Users of gRPC may specify one of the following two parameters. // OAuth2 parameters. Users of gRPC may specify one of the following two parameters.
/** /**

@ -175,9 +175,10 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype) new NS_UNAVAILABLE;
- (nullable instancetype)initWithNextInerceptor:(id<GRPCInterceptorInterface>)nextInterceptor NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithNextInerceptor:(id<GRPCInterceptorInterface>)nextInterceptor
NS_DESIGNATED_INITIALIZER;
/** Set the previous interceptor in the chain. Can only be set once. */ /** Set the previous interceptor in the chain. Can only be set once. */
- (void)setPreviousInterceptor:(id<GRPCResponseHandler>)previousInterceptor; - (void)setPreviousInterceptor:(id<GRPCResponseHandler>)previousInterceptor;
@ -213,7 +214,7 @@ NS_ASSUME_NONNULL_BEGIN
/** Forward call close and trailing metadata to the previous interceptor in the chain */ /** Forward call close and trailing metadata to the previous interceptor in the chain */
- (void)forwardPreviousInterceptorCloseWithTrailingMetadata: - (void)forwardPreviousInterceptorCloseWithTrailingMetadata:
(nullable NSDictionary *)trailingMetadata (nullable NSDictionary *)trailingMetadata
error:(nullable NSError *)error; error:(nullable NSError *)error;
/** Forward write completion to the previous interceptor in the chain */ /** Forward write completion to the previous interceptor in the chain */
@ -235,7 +236,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init NS_UNAVAILABLE; - (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 * 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 - (nullable instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager
requestDispatchQueue:(dispatch_queue_t)requestDispatchQueue 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 // Default implementation of GRPCInterceptorInterface

@ -47,8 +47,7 @@
if ([_nextInterceptor respondsToSelector:@selector(startWithRequestOptions:callOptions:)]) { if ([_nextInterceptor respondsToSelector:@selector(startWithRequestOptions:callOptions:)]) {
id<GRPCInterceptorInterface> copiedNextInterceptor = _nextInterceptor; id<GRPCInterceptorInterface> copiedNextInterceptor = _nextInterceptor;
dispatch_async(copiedNextInterceptor.requestDispatchQueue, ^{ dispatch_async(copiedNextInterceptor.requestDispatchQueue, ^{
[copiedNextInterceptor startWithRequestOptions:requestOptions [copiedNextInterceptor startWithRequestOptions:requestOptions callOptions:callOptions];
callOptions:callOptions];
}); });
} }
} }
@ -114,13 +113,12 @@
/** Forward call close and trailing metadata to the previous interceptor in the chain */ /** Forward call close and trailing metadata to the previous interceptor in the chain */
- (void)forwardPreviousInterceptorCloseWithTrailingMetadata: - (void)forwardPreviousInterceptorCloseWithTrailingMetadata:
(nullable NSDictionary *)trailingMetadata (nullable NSDictionary *)trailingMetadata
error:(nullable NSError *)error { error:(nullable NSError *)error {
if ([_previousInterceptor respondsToSelector:@selector(didCloseWithTrailingMetadata:error:)]) { if ([_previousInterceptor respondsToSelector:@selector(didCloseWithTrailingMetadata:error:)]) {
id<GRPCResponseHandler> copiedPreviousInterceptor = _previousInterceptor; id<GRPCResponseHandler> copiedPreviousInterceptor = _previousInterceptor;
dispatch_async(copiedPreviousInterceptor.dispatchQueue, ^{ dispatch_async(copiedPreviousInterceptor.dispatchQueue, ^{
[copiedPreviousInterceptor didCloseWithTrailingMetadata:trailingMetadata [copiedPreviousInterceptor didCloseWithTrailingMetadata:trailingMetadata error:error];
error:error];
}); });
} }
} }
@ -165,8 +163,7 @@
- (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions - (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions
callOptions:(GRPCCallOptions *)callOptions { callOptions:(GRPCCallOptions *)callOptions {
[_manager startNextInterceptorWithRequest:requestOptions [_manager startNextInterceptorWithRequest:requestOptions callOptions:callOptions];
callOptions:callOptions];
} }
- (void)writeData:(id)data { - (void)writeData:(id)data {
@ -179,8 +176,10 @@
- (void)cancel { - (void)cancel {
[_manager cancelNextInterceptor]; [_manager cancelNextInterceptor];
[_manager forwardPreviousInterceptorCloseWithTrailingMetadata:nil [_manager
error:[NSError errorWithDomain:kGRPCErrorDomain forwardPreviousInterceptorCloseWithTrailingMetadata:nil
error:[NSError
errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled code:GRPCErrorCodeCancelled
userInfo:@{ userInfo:@{
NSLocalizedDescriptionKey : NSLocalizedDescriptionKey :
@ -193,13 +192,13 @@
[_manager receiveNextInterceptorMessages:numberOfMessages]; [_manager receiveNextInterceptorMessages:numberOfMessages];
} }
- (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata { - (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata {
[_manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; [_manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata];
} }
- (void)didReceiveRawMessage:(id)message { - (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"); NSLog(@"The method didReceiveRawMessage is deprecated and cannot be used with interceptor");
abort(); abort();
} }
@ -208,10 +207,8 @@
[_manager forwardPreviousIntercetporWithData:data]; [_manager forwardPreviousIntercetporWithData:data];
} }
- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
error:(NSError *)error { [_manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata error:error];
[_manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata
error:error];
[_manager shutDown]; [_manager shutDown];
} }

@ -53,7 +53,7 @@
} }
- (void)setResponseHandler:(id<GRPCResponseHandler>)responseHandler { - (void)setResponseHandler:(id<GRPCResponseHandler>)responseHandler {
@synchronized (self) { @synchronized(self) {
NSAssert(!_started, @"Call already started."); NSAssert(!_started, @"Call already started.");
if (_started) { if (_started) {
return; return;
@ -84,7 +84,7 @@
return; return;
} }
@synchronized (self) { @synchronized(self) {
NSAssert(_handler != nil, @"Response handler required."); NSAssert(_handler != nil, @"Response handler required.");
if (_handler == nil) { if (_handler == nil) {
NSLog(@"Invalid response handler."); NSLog(@"Invalid response handler.");

@ -18,8 +18,8 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> @interface AppDelegate : UIResponder<UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window; @property(strong, nonatomic) UIWindow* window;
@end @end

@ -68,10 +68,12 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE; + (instancetype) new NS_UNAVAILABLE;
- (nullable instancetype)initWithInterceptorManager:(GRPCInterceptorManager * _Nonnull)intercepterManager - (nullable instancetype)initWithInterceptorManager:
cacheContext:(CacheContext * _Nonnull)cacheContext NS_DESIGNATED_INITIALIZER; (GRPCInterceptorManager *_Nonnull)intercepterManager
cacheContext:(CacheContext *_Nonnull)cacheContext
NS_DESIGNATED_INITIALIZER;
// implementation of GRPCInterceptorInterface // implementation of GRPCInterceptorInterface
- (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions - (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions

@ -138,21 +138,19 @@
} }
- (GRPCInterceptor *)createInterceptorWithManager:(GRPCInterceptorManager *)interceptorManager { - (GRPCInterceptor *)createInterceptorWithManager:(GRPCInterceptorManager *)interceptorManager {
return [[CacheInterceptor alloc] initWithInterceptorManager:interceptorManager return [[CacheInterceptor alloc] initWithInterceptorManager:interceptorManager cacheContext:self];
cacheContext:self];
} }
- (ResponseCacheEntry *)getCachedResponseForRequest:(RequestCacheEntry *)request { - (ResponseCacheEntry *)getCachedResponseForRequest:(RequestCacheEntry *)request {
ResponseCacheEntry *response = nil; ResponseCacheEntry *response = nil;
@synchronized (self) { @synchronized(self) {
response = [_cache objectForKey:request]; response = [_cache objectForKey:request];
} }
return response; return response;
} }
- (void)setCachedResponse:(ResponseCacheEntry *)response - (void)setCachedResponse:(ResponseCacheEntry *)response forRequest:(RequestCacheEntry *)request {
forRequest:(RequestCacheEntry *)request { @synchronized(self) {
@synchronized (self) {
[_cache setObject:response forKey:request]; [_cache setObject:response forKey:request];
} }
} }
@ -181,8 +179,8 @@
return _dispatchQueue; return _dispatchQueue;
} }
- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager * _Nonnull)intercepterManager - (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *_Nonnull)intercepterManager
cacheContext:(CacheContext * _Nonnull)cacheContext { cacheContext:(CacheContext *_Nonnull)cacheContext {
if ((self = [super initWithInterceptorManager:intercepterManager if ((self = [super initWithInterceptorManager:intercepterManager
requestDispatchQueue:dispatch_get_main_queue() requestDispatchQueue:dispatch_get_main_queue()
responseDispatchQueue:dispatch_get_main_queue()])) { responseDispatchQueue:dispatch_get_main_queue()])) {
@ -198,7 +196,8 @@
return self; return self;
} }
- (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions callOptions:(GRPCCallOptions *)callOptions { - (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions
callOptions:(GRPCCallOptions *)callOptions {
NSLog(@"start"); NSLog(@"start");
if (requestOptions.safety != GRPCCallSafetyCacheableRequest) { if (requestOptions.safety != GRPCCallSafetyCacheableRequest) {
NSLog(@"no cache"); NSLog(@"no cache");
@ -235,8 +234,7 @@
_response = [[_context getCachedResponseForRequest:_request] copy]; _response = [[_context getCachedResponseForRequest:_request] copy];
NSLog(@"Read cache for %@", _request); NSLog(@"Read cache for %@", _request);
if (!_response) { if (!_response) {
[_manager startNextInterceptorWithRequest:_requestOptions [_manager startNextInterceptorWithRequest:_requestOptions callOptions:_callOptions];
callOptions:_callOptions];
[_manager writeNextInterceptorWithData:_requestMessage]; [_manager writeNextInterceptorWithData:_requestMessage];
[_manager finishNextInterceptor]; [_manager finishNextInterceptor];
} else { } else {
@ -256,7 +254,9 @@
if ([key.lowercaseString isEqualToString:@"cache-control"]) { if ([key.lowercaseString isEqualToString:@"cache-control"]) {
NSArray *cacheControls = [initialMetadata[key] componentsSeparatedByString:@","]; NSArray *cacheControls = [initialMetadata[key] componentsSeparatedByString:@","];
for (NSString *option in cacheControls) { for (NSString *option in cacheControls) {
NSString *trimmedOption = [option stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]]; NSString *trimmedOption =
[option stringByTrimmingCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@" "]];
if ([trimmedOption.lowercaseString isEqualToString:@"no-cache"] || if ([trimmedOption.lowercaseString isEqualToString:@"no-cache"] ||
[trimmedOption.lowercaseString isEqualToString:@"no-store"] || [trimmedOption.lowercaseString isEqualToString:@"no-store"] ||
[trimmedOption.lowercaseString isEqualToString:@"no-transform"]) { [trimmedOption.lowercaseString isEqualToString:@"no-transform"]) {

@ -20,6 +20,4 @@
@interface ViewController : UIViewController @interface ViewController : UIViewController
@end @end

@ -19,8 +19,8 @@
#import "ViewController.h" #import "ViewController.h"
#import <GRPCClient/GRPCCall.h> #import <GRPCClient/GRPCCall.h>
#import <RemoteTest/Test.pbrpc.h>
#import <RemoteTest/Messages.pbobjc.h> #import <RemoteTest/Messages.pbobjc.h>
#import <RemoteTest/Test.pbrpc.h>
#import "CacheInterceptor.h" #import "CacheInterceptor.h"
@ -64,7 +64,6 @@ static NSString *const kService = @"TestService";
[call start]; [call start];
[call writeData:[request data]]; [call writeData:[request data]];
[call finish]; [call finish];
} }
- (dispatch_queue_t)dispatchQueue { - (dispatch_queue_t)dispatchQueue {

@ -19,7 +19,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "AppDelegate.h" #import "AppDelegate.h"
int main(int argc, char * argv[]) { int main(int argc, char* argv[]) {
@autoreleasepool { @autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
} }

@ -171,14 +171,19 @@ BOOL isRemoteInteropTest(NSString *host) {
@interface HookInterceptorFactory : NSObject<GRPCInterceptorFactory> @interface HookInterceptorFactory : NSObject<GRPCInterceptorFactory>
- (instancetype)initWithDispatchQueue:(dispatch_queue_t)dispatchQueue - (instancetype)
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook initWithDispatchQueue:(dispatch_queue_t)dispatchQueue
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions,
GRPCInterceptorManager *manager))startHook
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook GRPCInterceptorManager *manager))receiveNextMessagesHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata,
GRPCInterceptorManager *manager))responseHeaderHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error,
GRPCInterceptorManager *manager))responseCloseHook
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook;
- (GRPCInterceptor *)createInterceptorWithManager:(GRPCInterceptorManager *)interceptorManager; - (GRPCInterceptor *)createInterceptorWithManager:(GRPCInterceptorManager *)interceptorManager;
@ -187,39 +192,52 @@ BOOL isRemoteInteropTest(NSString *host) {
@interface HookIntercetpor : GRPCInterceptor @interface HookIntercetpor : GRPCInterceptor
- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager - (instancetype)
initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager
dispatchQueue:(dispatch_queue_t)dispatchQueue dispatchQueue:(dispatch_queue_t)dispatchQueue
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook startHook:(void (^)(GRPCRequestOptions *requestOptions,
GRPCCallOptions *callOptions,
GRPCInterceptorManager *manager))startHook
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook GRPCInterceptorManager *manager))receiveNextMessagesHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata,
GRPCInterceptorManager *manager))responseHeaderHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error,
GRPCInterceptorManager *manager))responseCloseHook
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook;
@end @end
@implementation HookInterceptorFactory { @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 (^_writeDataHook)(NSData *data, GRPCInterceptorManager *manager);
void (^_finishHook)(GRPCInterceptorManager *manager); void (^_finishHook)(GRPCInterceptorManager *manager);
void (^_receiveNextMessagesHook)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager); void (^_receiveNextMessagesHook)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager);
void (^_responseHeaderHook)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager); void (^_responseHeaderHook)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager);
void (^_responseDataHook)(NSData *data, 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); void (^_didWriteDataHook)(GRPCInterceptorManager *manager);
dispatch_queue_t _dispatchQueue; dispatch_queue_t _dispatchQueue;
} }
- (instancetype)initWithDispatchQueue:(dispatch_queue_t)dispatchQueue - (instancetype)
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook initWithDispatchQueue:(dispatch_queue_t)dispatchQueue
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions,
GRPCInterceptorManager *manager))startHook
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook GRPCInterceptorManager *manager))receiveNextMessagesHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata,
GRPCInterceptorManager *manager))responseHeaderHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error,
GRPCInterceptorManager *manager))responseCloseHook
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook {
if ((self = [super init])) { if ((self = [super init])) {
_dispatchQueue = dispatchQueue; _dispatchQueue = dispatchQueue;
@ -252,13 +270,15 @@ BOOL isRemoteInteropTest(NSString *host) {
@end @end
@implementation HookIntercetpor { @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 (^_writeDataHook)(NSData *data, GRPCInterceptorManager *manager);
void (^_finishHook)(GRPCInterceptorManager *manager); void (^_finishHook)(GRPCInterceptorManager *manager);
void (^_receiveNextMessagesHook)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager); void (^_receiveNextMessagesHook)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager);
void (^_responseHeaderHook)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager); void (^_responseHeaderHook)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager);
void (^_responseDataHook)(NSData *data, 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); void (^_didWriteDataHook)(GRPCInterceptorManager *manager);
GRPCInterceptorManager *_manager; GRPCInterceptorManager *_manager;
dispatch_queue_t _dispatchQueue; dispatch_queue_t _dispatchQueue;
@ -272,17 +292,25 @@ BOOL isRemoteInteropTest(NSString *host) {
return _dispatchQueue; return _dispatchQueue;
} }
- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager - (instancetype)
initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager
dispatchQueue:(dispatch_queue_t)dispatchQueue dispatchQueue:(dispatch_queue_t)dispatchQueue
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook startHook:(void (^)(GRPCRequestOptions *requestOptions,
GRPCCallOptions *callOptions,
GRPCInterceptorManager *manager))startHook
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook GRPCInterceptorManager *manager))receiveNextMessagesHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata,
GRPCInterceptorManager *manager))responseHeaderHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error,
GRPCInterceptorManager *manager))responseCloseHook
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook {
if ((self = [super initWithInterceptorManager:interceptorManager requestDispatchQueue:dispatchQueue responseDispatchQueue:dispatchQueue])) { if ((self = [super initWithInterceptorManager:interceptorManager
requestDispatchQueue:dispatchQueue
responseDispatchQueue:dispatchQueue])) {
_startHook = startHook; _startHook = startHook;
_writeDataHook = writeDataHook; _writeDataHook = writeDataHook;
_finishHook = finishHook; _finishHook = finishHook;
@ -1322,15 +1350,17 @@ BOOL isRemoteInteropTest(NSString *host) {
__block NSUInteger responseDataCount = 0; __block NSUInteger responseDataCount = 0;
__block NSUInteger responseCloseCount = 0; __block NSUInteger responseCloseCount = 0;
__block NSUInteger didWriteDataCount = 0; __block NSUInteger didWriteDataCount = 0;
id<GRPCInterceptorFactory> factory = id<GRPCInterceptorFactory> factory = [[HookInterceptorFactory alloc]
[[HookInterceptorFactory alloc] initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL) initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL)
startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager) { startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions,
GRPCInterceptorManager *manager) {
startCount++; startCount++;
NSLog(@"Interceptor - started call, %@, %@", requestOptions, callOptions); NSLog(@"Interceptor - started call, %@, %@", requestOptions, callOptions);
XCTAssertEqualObjects(requestOptions.host, [[self class] host]); XCTAssertEqualObjects(requestOptions.host, [[self class] host]);
XCTAssertEqualObjects(requestOptions.path, @"/grpc.testing.TestService/FullDuplexCall"); XCTAssertEqualObjects(requestOptions.path, @"/grpc.testing.TestService/FullDuplexCall");
XCTAssertEqual(requestOptions.safety, GRPCCallSafetyDefault); XCTAssertEqual(requestOptions.safety, GRPCCallSafetyDefault);
[manager startNextInterceptorWithRequest:[requestOptions copy] callOptions:[callOptions copy]]; [manager startNextInterceptorWithRequest:[requestOptions copy]
callOptions:[callOptions copy]];
} }
writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) {
writeDataCount++; writeDataCount++;
@ -1359,11 +1389,11 @@ BOOL isRemoteInteropTest(NSString *host) {
XCTAssertNotEqual(data.length, 0); XCTAssertNotEqual(data.length, 0);
[manager forwardPreviousIntercetporWithData:data]; [manager forwardPreviousIntercetporWithData:data];
} }
responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager) { responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error,
GRPCInterceptorManager *manager) {
responseCloseCount++; responseCloseCount++;
NSLog(@"Interceptor - received close, %@, %@", trailingMetadata, error); NSLog(@"Interceptor - received close, %@, %@", trailingMetadata, error);
[manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata [manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata error:error];
error:error];
} }
didWriteDataHook:^(GRPCInterceptorManager *manager) { didWriteDataHook:^(GRPCInterceptorManager *manager) {
didWriteDataCount++; didWriteDataCount++;
@ -1453,11 +1483,13 @@ BOOL isRemoteInteropTest(NSString *host) {
__block NSUInteger responseHeaderCount = 0; __block NSUInteger responseHeaderCount = 0;
__block NSUInteger responseDataCount = 0; __block NSUInteger responseDataCount = 0;
__block NSUInteger responseCloseCount = 0; __block NSUInteger responseCloseCount = 0;
id<GRPCInterceptorFactory> factory = id<GRPCInterceptorFactory> factory = [[HookInterceptorFactory alloc]
[[HookInterceptorFactory alloc] initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL) initWithDispatchQueue:dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL)
startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager) { startHook:^(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions,
GRPCInterceptorManager *manager) {
startCount++; startCount++;
[manager startNextInterceptorWithRequest:[requestOptions copy] callOptions:[callOptions copy]]; [manager startNextInterceptorWithRequest:[requestOptions copy]
callOptions:[callOptions copy]];
} }
writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) {
writeDataCount++; writeDataCount++;
@ -1465,15 +1497,20 @@ BOOL isRemoteInteropTest(NSString *host) {
[manager writeNextInterceptorWithData:data]; [manager writeNextInterceptorWithData:data];
} else if (index == kCancelAfterWrites) { } else if (index == kCancelAfterWrites) {
[manager cancelNextInterceptor]; [manager cancelNextInterceptor];
[manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]] data]]; [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse
messageWithPayloadSize:responses[index]]
data]];
} else { // (index > kCancelAfterWrites) } else { // (index > kCancelAfterWrites)
[manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]] data]]; [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse
messageWithPayloadSize:responses[index]]
data]];
} }
} }
finishHook:^(GRPCInterceptorManager *manager) { finishHook:^(GRPCInterceptorManager *manager) {
finishCount++; finishCount++;
// finish must happen after the hijacking, so directly reply with a close // finish must happen after the hijacking, so directly reply with a close
[manager forwardPreviousInterceptorCloseWithTrailingMetadata:@{ @"grpc-status" : @"0" } error:nil]; [manager forwardPreviousInterceptorCloseWithTrailingMetadata:@{@"grpc-status" : @"0"}
error:nil];
} }
receiveNextMessagesHook:nil receiveNextMessagesHook:nil
responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) { responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) {
@ -1484,7 +1521,8 @@ BOOL isRemoteInteropTest(NSString *host) {
responseDataCount++; responseDataCount++;
[manager forwardPreviousIntercetporWithData:data]; [manager forwardPreviousIntercetporWithData:data];
} }
responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager) { responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error,
GRPCInterceptorManager *manager) {
responseCloseCount++; responseCloseCount++;
// since we canceled the call, it should return cancel error // since we canceled the call, it should return cancel error
XCTAssertNil(trailingMetadata); XCTAssertNil(trailingMetadata);

Loading…
Cancel
Save