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. 14
      src/objective-c/GRPCClient/GRPCCallOptions.m
  4. 12
      src/objective-c/GRPCClient/GRPCInterceptor.h
  5. 37
      src/objective-c/GRPCClient/GRPCInterceptor.m
  6. 18
      src/objective-c/GRPCClient/private/GRPCCallInternal.m
  7. 4
      src/objective-c/examples/InterceptorSample/InterceptorSample/AppDelegate.h
  8. 8
      src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.h
  9. 28
      src/objective-c/examples/InterceptorSample/InterceptorSample/CacheInterceptor.m
  10. 2
      src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.h
  11. 3
      src/objective-c/examples/InterceptorSample/InterceptorSample/ViewController.m
  12. 4
      src/objective-c/examples/InterceptorSample/InterceptorSample/main.m
  13. 494
      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.
/** /**

@ -115,7 +115,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) {
- (instancetype)init { - (instancetype)init {
return [self initWithServerAuthority:kDefaultServerAuthority return [self initWithServerAuthority:kDefaultServerAuthority
timeout:kDefaultTimeout timeout:kDefaultTimeout
flowControlEnabled:kDefaultFlowControlEnabled flowControlEnabled:kDefaultFlowControlEnabled
interceptorFactories:kDefaultInterceptorFactories interceptorFactories:kDefaultInterceptorFactories
oauth2AccessToken:kDefaultOauth2AccessToken oauth2AccessToken:kDefaultOauth2AccessToken
authTokenProvider:kDefaultAuthTokenProvider authTokenProvider:kDefaultAuthTokenProvider
@ -142,7 +142,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) {
- (instancetype)initWithServerAuthority:(NSString *)serverAuthority - (instancetype)initWithServerAuthority:(NSString *)serverAuthority
timeout:(NSTimeInterval)timeout timeout:(NSTimeInterval)timeout
flowControlEnabled:(BOOL)flowControlEnabled flowControlEnabled:(BOOL)flowControlEnabled
interceptorFactories:(NSArray *)interceptorFactories interceptorFactories:(NSArray *)interceptorFactories
oauth2AccessToken:(NSString *)oauth2AccessToken oauth2AccessToken:(NSString *)oauth2AccessToken
authTokenProvider:(id<GRPCAuthorizationProtocol>)authTokenProvider authTokenProvider:(id<GRPCAuthorizationProtocol>)authTokenProvider
@ -205,7 +205,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) {
GRPCCallOptions *newOptions = GRPCCallOptions *newOptions =
[[GRPCCallOptions allocWithZone:zone] initWithServerAuthority:_serverAuthority [[GRPCCallOptions allocWithZone:zone] initWithServerAuthority:_serverAuthority
timeout:_timeout timeout:_timeout
flowControlEnabled:_flowControlEnabled flowControlEnabled:_flowControlEnabled
interceptorFactories:_interceptorFactories interceptorFactories:_interceptorFactories
oauth2AccessToken:_oauth2AccessToken oauth2AccessToken:_oauth2AccessToken
authTokenProvider:_authTokenProvider authTokenProvider:_authTokenProvider
@ -235,7 +235,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) {
GRPCMutableCallOptions *newOptions = [[GRPCMutableCallOptions allocWithZone:zone] GRPCMutableCallOptions *newOptions = [[GRPCMutableCallOptions allocWithZone:zone]
initWithServerAuthority:[_serverAuthority copy] initWithServerAuthority:[_serverAuthority copy]
timeout:_timeout timeout:_timeout
flowControlEnabled:_flowControlEnabled flowControlEnabled:_flowControlEnabled
interceptorFactories:_interceptorFactories interceptorFactories:_interceptorFactories
oauth2AccessToken:[_oauth2AccessToken copy] oauth2AccessToken:[_oauth2AccessToken copy]
authTokenProvider:_authTokenProvider authTokenProvider:_authTokenProvider
@ -344,7 +344,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) {
- (instancetype)init { - (instancetype)init {
return [self initWithServerAuthority:kDefaultServerAuthority return [self initWithServerAuthority:kDefaultServerAuthority
timeout:kDefaultTimeout timeout:kDefaultTimeout
flowControlEnabled:kDefaultFlowControlEnabled flowControlEnabled:kDefaultFlowControlEnabled
interceptorFactories:kDefaultInterceptorFactories interceptorFactories:kDefaultInterceptorFactories
oauth2AccessToken:kDefaultOauth2AccessToken oauth2AccessToken:kDefaultOauth2AccessToken
authTokenProvider:kDefaultAuthTokenProvider authTokenProvider:kDefaultAuthTokenProvider
@ -373,7 +373,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) {
GRPCCallOptions *newOptions = GRPCCallOptions *newOptions =
[[GRPCCallOptions allocWithZone:zone] initWithServerAuthority:_serverAuthority [[GRPCCallOptions allocWithZone:zone] initWithServerAuthority:_serverAuthority
timeout:_timeout timeout:_timeout
flowControlEnabled:_flowControlEnabled flowControlEnabled:_flowControlEnabled
interceptorFactories:_interceptorFactories interceptorFactories:_interceptorFactories
oauth2AccessToken:_oauth2AccessToken oauth2AccessToken:_oauth2AccessToken
authTokenProvider:_authTokenProvider authTokenProvider:_authTokenProvider
@ -403,7 +403,7 @@ static BOOL areObjectsEqual(id obj1, id obj2) {
GRPCMutableCallOptions *newOptions = [[GRPCMutableCallOptions allocWithZone:zone] GRPCMutableCallOptions *newOptions = [[GRPCMutableCallOptions allocWithZone:zone]
initWithServerAuthority:_serverAuthority initWithServerAuthority:_serverAuthority
timeout:_timeout timeout:_timeout
flowControlEnabled:_flowControlEnabled flowControlEnabled:_flowControlEnabled
interceptorFactories:_interceptorFactories interceptorFactories:_interceptorFactories
oauth2AccessToken:_oauth2AccessToken oauth2AccessToken:_oauth2AccessToken
authTokenProvider:_authTokenProvider authTokenProvider:_authTokenProvider

@ -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,13 +176,15 @@
- (void)cancel { - (void)cancel {
[_manager cancelNextInterceptor]; [_manager cancelNextInterceptor];
[_manager forwardPreviousInterceptorCloseWithTrailingMetadata:nil [_manager
error:[NSError errorWithDomain:kGRPCErrorDomain forwardPreviousInterceptorCloseWithTrailingMetadata:nil
code:GRPCErrorCodeCancelled error:[NSError
userInfo:@{ errorWithDomain:kGRPCErrorDomain
NSLocalizedDescriptionKey : code:GRPCErrorCodeCancelled
@"Canceled" userInfo:@{
}]]; NSLocalizedDescriptionKey :
@"Canceled"
}]];
[_manager shutDown]; [_manager shutDown];
} }
@ -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];
} }

@ -35,12 +35,12 @@
- (instancetype)init { - (instancetype)init {
if ((self = [super 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 __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
if (@available(iOS 8.0, macOS 10.10, *)) { if (@available(iOS 8.0, macOS 10.10, *)) {
_dispatchQueue = dispatch_queue_create( _dispatchQueue = dispatch_queue_create(
NULL, NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0)); dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0));
} else { } else {
#else #else
{ {
@ -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.");
@ -172,7 +172,7 @@
} }
}; };
id<GRXWriteable> responseWriteable = id<GRXWriteable> responseWriteable =
[[GRXWriteable alloc] initWithValueHandler:valueHandler completionHandler:completionHandler]; [[GRXWriteable alloc] initWithValueHandler:valueHandler completionHandler:completionHandler];
[copiedCall startWithWriteable:responseWriteable]; [copiedCall startWithWriteable:responseWriteable];
} }
@ -197,9 +197,9 @@
error:[NSError errorWithDomain:kGRPCErrorDomain error:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled code:GRPCErrorCodeCancelled
userInfo:@{ userInfo:@{
NSLocalizedDescriptionKey : NSLocalizedDescriptionKey :
@"Canceled by app" @"Canceled by app"
}]]; }]];
}); });
} else { } else {
_handler = nil; _handler = nil;

@ -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

@ -19,7 +19,7 @@
#import "CacheInterceptor.h" #import "CacheInterceptor.h"
@implementation RequestCacheEntry { @implementation RequestCacheEntry {
@protected @protected
NSString *_path; NSString *_path;
id<NSObject> _message; id<NSObject> _message;
} }
@ -67,7 +67,7 @@
@end @end
@implementation ResponseCacheEntry { @implementation ResponseCacheEntry {
@protected @protected
NSDate *_deadline; NSDate *_deadline;
NSDictionary *_headers; NSDictionary *_headers;
id _message; id _message;
@ -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,8 +19,8 @@
#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,15 +171,20 @@ 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
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions,
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook GRPCInterceptorManager *manager))startHook
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook GRPCInterceptorManager *manager))receiveNextMessagesHook
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; 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; - (GRPCInterceptor *)createInterceptorWithManager:(GRPCInterceptorManager *)interceptorManager;
@ -187,40 +192,53 @@ BOOL isRemoteInteropTest(NSString *host) {
@interface HookIntercetpor : GRPCInterceptor @interface HookIntercetpor : GRPCInterceptor
- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager - (instancetype)
dispatchQueue:(dispatch_queue_t)dispatchQueue initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook dispatchQueue:(dispatch_queue_t)dispatchQueue
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook startHook:(void (^)(GRPCRequestOptions *requestOptions,
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook GRPCCallOptions *callOptions,
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook GRPCInterceptorManager *manager))startHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook; 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 @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
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions,
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook GRPCInterceptorManager *manager))startHook
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook GRPCInterceptorManager *manager))receiveNextMessagesHook
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { 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])) { if ((self = [super init])) {
_dispatchQueue = dispatchQueue; _dispatchQueue = dispatchQueue;
_startHook = startHook; _startHook = startHook;
@ -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)
dispatchQueue:(dispatch_queue_t)dispatchQueue initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager
startHook:(void (^)(GRPCRequestOptions *requestOptions, GRPCCallOptions *callOptions, GRPCInterceptorManager *manager))startHook dispatchQueue:(dispatch_queue_t)dispatchQueue
writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook startHook:(void (^)(GRPCRequestOptions *requestOptions,
finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook GRPCCallOptions *callOptions,
receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages, GRPCInterceptorManager *manager))receiveNextMessagesHook GRPCInterceptorManager *manager))startHook
responseHeaderHook:(void (^)(NSDictionary *initialMetadata, GRPCInterceptorManager *manager))responseHeaderHook writeDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))writeDataHook
responseDataHook:(void (^)(NSData *data, GRPCInterceptorManager *manager))responseDataHook finishHook:(void (^)(GRPCInterceptorManager *manager))finishHook
responseCloseHook:(void (^)(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager))responseCloseHook receiveNextMessagesHook:(void (^)(NSUInteger numberOfMessages,
didWriteDataHook:(void (^)(GRPCInterceptorManager *manager))didWriteDataHook { GRPCInterceptorManager *manager))receiveNextMessagesHook
if ((self = [super initWithInterceptorManager:interceptorManager requestDispatchQueue:dispatchQueue responseDispatchQueue:dispatchQueue])) { 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; _startHook = startHook;
_writeDataHook = writeDataHook; _writeDataHook = writeDataHook;
_finishHook = finishHook; _finishHook = finishHook;
@ -1271,39 +1299,39 @@ BOOL isRemoteInteropTest(NSString *host) {
options.interceptorFactories = @[ [[DefaultInterceptorFactory alloc] init] ]; options.interceptorFactories = @[ [[DefaultInterceptorFactory alloc] init] ];
__block GRPCStreamingProtoCall *call = [_service __block GRPCStreamingProtoCall *call = [_service
fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc]
initWithInitialMetadataCallback:nil initWithInitialMetadataCallback:nil
messageCallback:^(id message) { messageCallback:^(id message) {
XCTAssertLessThan(index, 4, XCTAssertLessThan(index, 4,
@"More than 4 responses received."); @"More than 4 responses received.");
id expected = [RMTStreamingOutputCallResponse id expected = [RMTStreamingOutputCallResponse
messageWithPayloadSize:responses[index]]; messageWithPayloadSize:responses[index]];
XCTAssertEqualObjects(message, expected); XCTAssertEqualObjects(message, expected);
index += 1; index += 1;
if (index < 4) { if (index < 4) {
id request = [RMTStreamingOutputCallRequest id request = [RMTStreamingOutputCallRequest
messageWithPayloadSize:requests[index] messageWithPayloadSize:requests[index]
requestedResponseSize:responses[index]]; requestedResponseSize:responses[index]];
[call writeMessage:request]; [call writeMessage:request];
} else { } else {
[call finish]; [call finish];
} }
// DEBUG // DEBUG
NSLog(@"Received message"); NSLog(@"Received message");
} }
closeCallback:^(NSDictionary *trailingMetadata, closeCallback:^(NSDictionary *trailingMetadata,
NSError *error) { NSError *error) {
XCTAssertNil(error, XCTAssertNil(error,
@"Finished with unexpected error: %@", @"Finished with unexpected error: %@",
error); error);
XCTAssertEqual(index, 4, XCTAssertEqual(index, 4,
@"Received %i responses instead of 4.", @"Received %i responses instead of 4.",
index); index);
[expectation fulfill]; [expectation fulfill];
// DEBUG // DEBUG
NSLog(@"Received close"); NSLog(@"Received close");
}] }]
callOptions:options]; callOptions:options];
[call start]; [call start];
[call writeMessage:request]; [call writeMessage:request];
@ -1322,54 +1350,56 @@ 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,
startCount++; GRPCInterceptorManager *manager) {
NSLog(@"Interceptor - started call, %@, %@", requestOptions, callOptions); startCount++;
XCTAssertEqualObjects(requestOptions.host, [[self class] host]); NSLog(@"Interceptor - started call, %@, %@", requestOptions, callOptions);
XCTAssertEqualObjects(requestOptions.path, @"/grpc.testing.TestService/FullDuplexCall"); XCTAssertEqualObjects(requestOptions.host, [[self class] host]);
XCTAssertEqual(requestOptions.safety, GRPCCallSafetyDefault); XCTAssertEqualObjects(requestOptions.path, @"/grpc.testing.TestService/FullDuplexCall");
[manager startNextInterceptorWithRequest:[requestOptions copy] callOptions:[callOptions copy]]; XCTAssertEqual(requestOptions.safety, GRPCCallSafetyDefault);
} [manager startNextInterceptorWithRequest:[requestOptions copy]
writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { callOptions:[callOptions copy]];
writeDataCount++; }
NSLog(@"Interceptor - send data, %@", data); writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) {
XCTAssertNotEqual(data.length, 0); writeDataCount++;
[manager writeNextInterceptorWithData:data]; NSLog(@"Interceptor - send data, %@", data);
} XCTAssertNotEqual(data.length, 0);
finishHook:^(GRPCInterceptorManager *manager) { [manager writeNextInterceptorWithData:data];
finishCount++; }
NSLog(@"Interceptor - finish call"); finishHook:^(GRPCInterceptorManager *manager) {
[manager finishNextInterceptor]; finishCount++;
} NSLog(@"Interceptor - finish call");
receiveNextMessagesHook:^(NSUInteger numberOfMessages, GRPCInterceptorManager *manager) { [manager finishNextInterceptor];
receiveNextMessageCount++; }
NSLog(@"Interceptor - receive next messages, %lu", (unsigned long)numberOfMessages); receiveNextMessagesHook:^(NSUInteger numberOfMessages, GRPCInterceptorManager *manager) {
[manager receiveNextInterceptorMessages:numberOfMessages]; receiveNextMessageCount++;
} NSLog(@"Interceptor - receive next messages, %lu", (unsigned long)numberOfMessages);
responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) { [manager receiveNextInterceptorMessages:numberOfMessages];
responseHeaderCount++; }
NSLog(@"Interceptor - received initial metadata, %@", initialMetadata); responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) {
[manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; responseHeaderCount++;
} NSLog(@"Interceptor - received initial metadata, %@", initialMetadata);
responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) { [manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata];
responseDataCount++; }
NSLog(@"Interceptor - received data, %@", data); responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) {
XCTAssertNotEqual(data.length, 0); responseDataCount++;
[manager forwardPreviousIntercetporWithData:data]; NSLog(@"Interceptor - received data, %@", data);
} XCTAssertNotEqual(data.length, 0);
responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager) { [manager forwardPreviousIntercetporWithData:data];
responseCloseCount++; }
NSLog(@"Interceptor - received close, %@, %@", trailingMetadata, error); responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error,
[manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata GRPCInterceptorManager *manager) {
error:error]; responseCloseCount++;
} NSLog(@"Interceptor - received close, %@, %@", trailingMetadata, error);
didWriteDataHook:^(GRPCInterceptorManager *manager) { [manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata error:error];
didWriteDataCount++; }
NSLog(@"Interceptor - received did-write-data"); didWriteDataHook:^(GRPCInterceptorManager *manager) {
[manager forwardPreviousInterceptorDidWriteData]; didWriteDataCount++;
}]; NSLog(@"Interceptor - received did-write-data");
[manager forwardPreviousInterceptorDidWriteData];
}];
NSArray *requests = @[ @1, @2, @3, @4 ]; NSArray *requests = @[ @1, @2, @3, @4 ];
NSArray *responses = @[ @1, @2, @3, @4 ]; NSArray *responses = @[ @1, @2, @3, @4 ];
@ -1387,41 +1417,41 @@ BOOL isRemoteInteropTest(NSString *host) {
__block BOOL canWriteData = NO; __block BOOL canWriteData = NO;
__block GRPCStreamingProtoCall *call = [_service __block GRPCStreamingProtoCall *call = [_service
fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc]
initWithInitialMetadataCallback:nil initWithInitialMetadataCallback:nil
messageCallback:^(id message) { messageCallback:^(id message) {
XCTAssertLessThan(index, 4, XCTAssertLessThan(index, 4,
@"More than 4 responses received."); @"More than 4 responses received.");
id expected = [RMTStreamingOutputCallResponse id expected = [RMTStreamingOutputCallResponse
messageWithPayloadSize:responses[index]]; messageWithPayloadSize:responses[index]];
XCTAssertEqualObjects(message, expected); XCTAssertEqualObjects(message, expected);
index += 1; index += 1;
if (index < 4) { if (index < 4) {
id request = [RMTStreamingOutputCallRequest id request = [RMTStreamingOutputCallRequest
messageWithPayloadSize:requests[index] messageWithPayloadSize:requests[index]
requestedResponseSize:responses[index]]; requestedResponseSize:responses[index]];
XCTAssertTrue(canWriteData); XCTAssertTrue(canWriteData);
canWriteData = NO; canWriteData = NO;
[call writeMessage:request]; [call writeMessage:request];
[call receiveNextMessage]; [call receiveNextMessage];
} else { } else {
[call finish]; [call finish];
} }
} }
closeCallback:^(NSDictionary *trailingMetadata, closeCallback:^(NSDictionary *trailingMetadata,
NSError *error) { NSError *error) {
XCTAssertNil(error, XCTAssertNil(error,
@"Finished with unexpected error: %@", @"Finished with unexpected error: %@",
error); error);
XCTAssertEqual(index, 4, XCTAssertEqual(index, 4,
@"Received %i responses instead of 4.", @"Received %i responses instead of 4.",
index); index);
[expectation fulfill]; [expectation fulfill];
} }
writeMessageCallback:^{ writeMessageCallback:^{
canWriteData = YES; canWriteData = YES;
}] }]
callOptions:options]; callOptions:options];
[call start]; [call start];
[call receiveNextMessage]; [call receiveNextMessage];
[call writeMessage:request]; [call writeMessage:request];
@ -1453,45 +1483,53 @@ 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,
startCount++; GRPCInterceptorManager *manager) {
[manager startNextInterceptorWithRequest:[requestOptions copy] callOptions:[callOptions copy]]; startCount++;
} [manager startNextInterceptorWithRequest:[requestOptions copy]
writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) { callOptions:[callOptions copy]];
writeDataCount++; }
if (index < kCancelAfterWrites) { writeDataHook:^(NSData *data, GRPCInterceptorManager *manager) {
[manager writeNextInterceptorWithData:data]; writeDataCount++;
} else if (index == kCancelAfterWrites) { if (index < kCancelAfterWrites) {
[manager cancelNextInterceptor]; [manager writeNextInterceptorWithData:data];
[manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]] data]]; } else if (index == kCancelAfterWrites) {
} else { // (index > kCancelAfterWrites) [manager cancelNextInterceptor];
[manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]] data]]; [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse
} messageWithPayloadSize:responses[index]]
} data]];
finishHook:^(GRPCInterceptorManager *manager) { } else { // (index > kCancelAfterWrites)
finishCount++; [manager forwardPreviousIntercetporWithData:[[RMTStreamingOutputCallResponse
// finish must happen after the hijacking, so directly reply with a close messageWithPayloadSize:responses[index]]
[manager forwardPreviousInterceptorCloseWithTrailingMetadata:@{ @"grpc-status" : @"0" } error:nil]; data]];
} }
receiveNextMessagesHook:nil }
responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) { finishHook:^(GRPCInterceptorManager *manager) {
responseHeaderCount++; finishCount++;
[manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata]; // finish must happen after the hijacking, so directly reply with a close
} [manager forwardPreviousInterceptorCloseWithTrailingMetadata:@{@"grpc-status" : @"0"}
responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) { error:nil];
responseDataCount++; }
[manager forwardPreviousIntercetporWithData:data]; receiveNextMessagesHook:nil
} responseHeaderHook:^(NSDictionary *initialMetadata, GRPCInterceptorManager *manager) {
responseCloseHook:^(NSDictionary *trailingMetadata, NSError *error, GRPCInterceptorManager *manager) { responseHeaderCount++;
responseCloseCount++; [manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata];
// since we canceled the call, it should return cancel error }
XCTAssertNil(trailingMetadata); responseDataHook:^(NSData *data, GRPCInterceptorManager *manager) {
XCTAssertNotNil(error); responseDataCount++;
XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED); [manager forwardPreviousIntercetporWithData:data];
} }
didWriteDataHook:nil]; 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 ]; NSArray *requests = @[ @1, @2, @3, @4 ];
@ -1504,36 +1542,36 @@ BOOL isRemoteInteropTest(NSString *host) {
options.interceptorFactories = @[ [[DefaultInterceptorFactory alloc] init], factory ]; options.interceptorFactories = @[ [[DefaultInterceptorFactory alloc] init], factory ];
__block GRPCStreamingProtoCall *call = [_service __block GRPCStreamingProtoCall *call = [_service
fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc] fullDuplexCallWithResponseHandler:[[InteropTestsBlockCallbacks alloc]
initWithInitialMetadataCallback:nil initWithInitialMetadataCallback:nil
messageCallback:^(id message) { messageCallback:^(id message) {
XCTAssertLessThan(index, 4, XCTAssertLessThan(index, 4,
@"More than 4 responses received."); @"More than 4 responses received.");
id expected = [RMTStreamingOutputCallResponse id expected = [RMTStreamingOutputCallResponse
messageWithPayloadSize:responses[index]]; messageWithPayloadSize:responses[index]];
XCTAssertEqualObjects(message, expected); XCTAssertEqualObjects(message, expected);
index += 1; index += 1;
if (index < 4) { if (index < 4) {
id request = [RMTStreamingOutputCallRequest id request = [RMTStreamingOutputCallRequest
messageWithPayloadSize:requests[index] messageWithPayloadSize:requests[index]
requestedResponseSize:responses[index]]; requestedResponseSize:responses[index]];
[call writeMessage:request]; [call writeMessage:request];
[call receiveNextMessage]; [call receiveNextMessage];
} else { } else {
[call finish]; [call finish];
} }
} }
closeCallback:^(NSDictionary *trailingMetadata, closeCallback:^(NSDictionary *trailingMetadata,
NSError *error) { NSError *error) {
XCTAssertNil(error, XCTAssertNil(error,
@"Finished with unexpected error: %@", @"Finished with unexpected error: %@",
error); error);
XCTAssertEqual(index, 4, XCTAssertEqual(index, 4,
@"Received %i responses instead of 4.", @"Received %i responses instead of 4.",
index); index);
[expectation fulfill]; [expectation fulfill];
}] }]
callOptions:options]; callOptions:options];
[call start]; [call start];
[call receiveNextMessage]; [call receiveNextMessage];
[call writeMessage:request]; [call writeMessage:request];

Loading…
Cancel
Save