clang-format objective-c files

pull/15036/head
Muxi Yan 7 years ago
parent e433b56def
commit c92d90aba2
  1. 10
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.h
  2. 3
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.m
  3. 4
      src/objective-c/GRPCClient/GRPCCall+ChannelCredentials.h
  4. 17
      src/objective-c/GRPCClient/GRPCCall+ChannelCredentials.m
  5. 6
      src/objective-c/GRPCClient/GRPCCall+Cronet.h
  6. 4
      src/objective-c/GRPCClient/GRPCCall+GID.h
  7. 6
      src/objective-c/GRPCClient/GRPCCall+OAuth2.m
  8. 7
      src/objective-c/GRPCClient/GRPCCall+Tests.m
  9. 9
      src/objective-c/GRPCClient/GRPCCall.h
  10. 140
      src/objective-c/GRPCClient/GRPCCall.m
  11. 6
      src/objective-c/GRPCClient/private/GRPCChannel.h
  12. 50
      src/objective-c/GRPCClient/private/GRPCChannel.m
  13. 2
      src/objective-c/GRPCClient/private/GRPCCompletionQueue.h
  14. 5
      src/objective-c/GRPCClient/private/GRPCCompletionQueue.m
  15. 5
      src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.h
  16. 11
      src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.m
  17. 1
      src/objective-c/GRPCClient/private/GRPCHost.h
  18. 56
      src/objective-c/GRPCClient/private/GRPCHost.m
  19. 1
      src/objective-c/GRPCClient/private/GRPCOpBatchLog.h
  20. 6
      src/objective-c/GRPCClient/private/GRPCOpBatchLog.m
  21. 6
      src/objective-c/GRPCClient/private/GRPCReachabilityFlagNames.xmacro.h
  22. 20
      src/objective-c/GRPCClient/private/GRPCRequestHeaders.m
  23. 17
      src/objective-c/GRPCClient/private/GRPCWrappedCall.h
  24. 77
      src/objective-c/GRPCClient/private/GRPCWrappedCall.m
  25. 10
      src/objective-c/GRPCClient/private/NSData+GRPC.m
  26. 5
      src/objective-c/GRPCClient/private/NSDictionary+GRPC.m
  27. 4
      src/objective-c/GRPCClient/private/NSError+GRPC.m
  28. 1
      src/objective-c/GRPCClient/private/version.h
  29. 9
      src/objective-c/ProtoRPC/ProtoMethod.h
  30. 26
      src/objective-c/ProtoRPC/ProtoRPC.h
  31. 32
      src/objective-c/ProtoRPC/ProtoRPC.m
  32. 21
      src/objective-c/ProtoRPC/ProtoService.h
  33. 5
      src/objective-c/ProtoRPC/ProtoService.m
  34. 5
      src/objective-c/RxLibrary/GRXBufferedPipe.m
  35. 2
      src/objective-c/RxLibrary/GRXConcurrentWriteable.h
  36. 9
      src/objective-c/RxLibrary/GRXConcurrentWriteable.m
  37. 2
      src/objective-c/RxLibrary/GRXForwardingWriter.m
  38. 7
      src/objective-c/RxLibrary/GRXImmediateWriter.m
  39. 2
      src/objective-c/RxLibrary/GRXWriteable.h
  40. 18
      src/objective-c/RxLibrary/GRXWriteable.m
  41. 2
      src/objective-c/RxLibrary/GRXWriter+Immediate.m
  42. 7
      src/objective-c/RxLibrary/transformations/GRXMappingWriter.h
  43. 2
      src/objective-c/RxLibrary/transformations/GRXMappingWriter.m
  44. 4
      src/objective-c/examples/Sample/Sample/AppDelegate.h
  45. 42
      src/objective-c/examples/Sample/Sample/ViewController.m
  46. 4
      src/objective-c/examples/Sample/Sample/main.m
  47. 58
      src/objective-c/tests/Connectivity/ConnectivityTestingApp/ViewController.m
  48. 8
      src/objective-c/tests/Connectivity/ConnectivityTestingApp/main.m
  49. 42
      src/objective-c/tests/CoreCronetEnd2EndTests/CoreCronetEnd2EndTests.mm
  50. 169
      src/objective-c/tests/CronetUnitTests/CronetUnitTests.m
  51. 411
      src/objective-c/tests/GRPCClientTests.m
  52. 395
      src/objective-c/tests/InteropTests.m
  53. 4
      src/objective-c/tests/InteropTestsLocalCleartext.m
  54. 10
      src/objective-c/tests/InteropTestsLocalSSL.m
  55. 4
      src/objective-c/tests/InteropTestsRemote.m
  56. 5
      src/objective-c/tests/InteropTestsRemoteWithCronet/InteropTestsRemoteWithCronet.m
  57. 41
      src/objective-c/tests/RxLibraryUnitTests.m
  58. 1
      src/objective-c/tests/version.h
  59. 61
      test/cpp/cocoapods/generic/generic.mm
  60. 8
      test/cpp/cocoapods/test/server_context_test_spouse_test.mm

@ -39,12 +39,12 @@ typedef NS_ENUM(NSInteger, GRPCCompressAlgorithm) {
/** The default response size limit is 4MB. Set this to override that default. */
+ (void)setResponseSizeLimit:(NSUInteger)limit forHost:(nonnull NSString *)host;
+ (void)closeOpenConnections DEPRECATED_MSG_ATTRIBUTE("The API for this feature is experimental, "
"and might be removed or modified at any "
"time.");
+ (void)closeOpenConnections DEPRECATED_MSG_ATTRIBUTE(
"The API for this feature is experimental, "
"and might be removed or modified at any "
"time.");
+ (void)setDefaultCompressMethod:(GRPCCompressAlgorithm)algorithm
forhost:(nonnull NSString *)host;
+ (void)setDefaultCompressMethod:(GRPCCompressAlgorithm)algorithm forhost:(nonnull NSString *)host;
/** Enable keepalive and configure keepalive parameters. A user should call this function once to
* enable keepalive for a particular host. gRPC client sends a ping after every \a interval ms to

@ -38,8 +38,7 @@
[GRPCHost flushChannelCache];
}
+ (void)setDefaultCompressMethod:(GRPCCompressAlgorithm)algorithm
forhost:(nonnull NSString *)host {
+ (void)setDefaultCompressMethod:(GRPCCompressAlgorithm)algorithm forhost:(nonnull NSString *)host {
GRPCHost *hostConfig = [GRPCHost hostWithAddress:host];
switch (algorithm) {
case GRPCCompressNone:

@ -26,7 +26,7 @@
*/
+ (BOOL)setTLSPEMRootCerts:(nullable NSString *)pemRootCert
forHost:(nonnull NSString *)host
error:(NSError * _Nullable * _Nullable)errorPtr;
error:(NSError *_Nullable *_Nullable)errorPtr;
/**
* Configures @c host with TLS/SSL Client Credentials and optionally trusted root Certificate
* Authorities. If @c pemRootCerts is nil, the default CA Certificates bundled with gRPC will be
@ -36,6 +36,6 @@
withPrivateKey:(nullable NSString *)pemPrivateKey
withCertChain:(nullable NSString *)pemCertChain
forHost:(nonnull NSString *)host
error:(NSError * _Nullable * _Nullable)errorPtr;
error:(NSError *_Nullable *_Nullable)errorPtr;
@end

@ -28,24 +28,23 @@
forHost:(nonnull NSString *)host
error:(NSError **)errorPtr {
if (!host) {
[NSException raise:NSInvalidArgumentException
format:@"host must be provided."];
[NSException raise:NSInvalidArgumentException format:@"host must be provided."];
}
GRPCHost *hostConfig = [GRPCHost hostWithAddress:host];
return [hostConfig setTLSPEMRootCerts:pemRootCerts
withPrivateKey:pemPrivateKey
withCertChain:pemCertChain
error:errorPtr];
withPrivateKey:pemPrivateKey
withCertChain:pemCertChain
error:errorPtr];
}
+ (BOOL)setTLSPEMRootCerts:(nullable NSString *)pemRootCerts
forHost:(nonnull NSString *)host
error:(NSError **)errorPtr {
return [GRPCCall setTLSPEMRootCerts:pemRootCerts
withPrivateKey:nil
withCertChain:nil
forHost:host
error:errorPtr];
withPrivateKey:nil
withCertChain:nil
forHost:host
error:errorPtr];
}
@end

@ -32,11 +32,11 @@
* all subsequent RPCs will use Cronet transport. The method is not thread
* safe.
*/
+(void)useCronetWithEngine:(stream_engine *)engine;
+ (void)useCronetWithEngine:(stream_engine*)engine;
+(stream_engine *)cronetEngine;
+ (stream_engine*)cronetEngine;
+(BOOL)isUsingCronet;
+ (BOOL)isUsingCronet;
@end
#endif

@ -16,14 +16,14 @@
*
*/
#import "GRPCCall.h"
#import "GRPCCall+OAuth2.h"
#import "GRPCCall.h"
#import <Google/SignIn.h>
/**
* Extend GIDSignIn class to comply GRPCAuthorizationProtocol
*/
@interface GIDSignIn (GRPC) <GRPCAuthorizationProtocol>
@interface GIDSignIn (GRPC)<GRPCAuthorizationProtocol>
- (void)getTokenWithHandler:(void (^)(NSString *token))hander;
@end

@ -20,9 +20,9 @@
#import "GRPCCall+OAuth2.h"
static NSString * const kAuthorizationHeader = @"authorization";
static NSString * const kBearerPrefix = @"Bearer ";
static NSString * const kChallengeHeader = @"www-authenticate";
static NSString *const kAuthorizationHeader = @"authorization";
static NSString *const kBearerPrefix = @"Bearer ";
static NSString *const kChallengeHeader = @"www-authenticate";
@implementation GRPCCall (OAuth2)
@dynamic tokenProvider;

@ -29,11 +29,10 @@
[NSException raise:NSInvalidArgumentException format:@"host, path and name must be provided."];
}
NSError *error = nil;
NSString *certs = [NSString stringWithContentsOfFile:certsPath
encoding:NSUTF8StringEncoding
error:&error];
NSString *certs =
[NSString stringWithContentsOfFile:certsPath encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
[NSException raise:[error localizedDescription] format:@"failed to load certs"];
[NSException raise:[error localizedDescription] format:@"failed to load certs"];
}
GRPCHost *hostConfig = [GRPCHost hostWithAddress:host];

@ -147,7 +147,8 @@ typedef NS_ENUM(NSUInteger, GRPCCallSafety) {
GRPCCallSafetyDefault = 0,
/** Signal that the call is idempotent. gRPC is free to use PUT verb. */
GRPCCallSafetyIdempotentRequest = 1,
/** Signal that the call is cacheable and will not affect server state. gRPC is free to use GET verb. */
/** Signal that the call is cacheable and will not affect server state. gRPC is free to use GET
verb. */
GRPCCallSafetyCacheableRequest = 2,
};
@ -167,7 +168,7 @@ extern id const kGRPCTrailersKey;
* The authority for the RPC. If nil, the default authority will be used. This property must be nil
* when Cronet transport is enabled.
*/
@property (atomic, copy, readwrite) NSString *serverName;
@property(atomic, copy, readwrite) NSString *serverName;
/**
* The timeout for the RPC call in seconds. If set to 0, the call will not timeout. If set to
@ -265,7 +266,7 @@ extern id const kGRPCTrailersKey;
/** This protocol is kept for backwards compatibility with existing code. */
DEPRECATED_MSG_ATTRIBUTE("Use NSDictionary or NSMutableDictionary instead.")
@protocol GRPCRequestHeaders <NSObject>
@protocol GRPCRequestHeaders<NSObject>
@property(nonatomic, readonly) NSUInteger count;
- (id)objectForKeyedSubscript:(id)key;
@ -278,6 +279,6 @@ DEPRECATED_MSG_ATTRIBUTE("Use NSDictionary or NSMutableDictionary instead.")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
/** This is only needed for backwards-compatibility. */
@interface NSMutableDictionary (GRPCRequestHeaders) <GRPCRequestHeaders>
@interface NSMutableDictionary (GRPCRequestHeaders)<GRPCRequestHeaders>
@end
#pragma clang diagnostic pop

@ -20,10 +20,10 @@
#import "GRPCCall+OAuth2.h"
#include <grpc/grpc.h>
#include <grpc/support/time.h>
#import <RxLibrary/GRXConcurrentWriteable.h>
#import <RxLibrary/GRXImmediateSingleWriter.h>
#include <grpc/grpc.h>
#include <grpc/support/time.h>
#import "private/GRPCConnectivityMonitor.h"
#import "private/GRPCHost.h"
@ -38,14 +38,14 @@
// and RECV_STATUS_ON_CLIENT.
NSInteger kMaxClientBatch = 6;
NSString * const kGRPCHeadersKey = @"io.grpc.HeadersKey";
NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey";
NSString *const kGRPCHeadersKey = @"io.grpc.HeadersKey";
NSString *const kGRPCTrailersKey = @"io.grpc.TrailersKey";
static NSMutableDictionary *callFlags;
static NSString * const kAuthorizationHeader = @"authorization";
static NSString * const kBearerPrefix = @"Bearer ";
static NSString *const kAuthorizationHeader = @"authorization";
static NSString *const kBearerPrefix = @"Bearer ";
@interface GRPCCall () <GRXWriteable>
@interface GRPCCall ()<GRXWriteable>
// Make them read-write.
@property(atomic, strong) NSDictionary *responseHeaders;
@property(atomic, strong) NSDictionary *responseTrailers;
@ -219,9 +219,11 @@ static NSString * const kBearerPrefix = @"Bearer ";
}
- (void)cancel {
[self maybeFinishWithError:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled
userInfo:@{NSLocalizedDescriptionKey: @"Canceled by app"}]];
[self
maybeFinishWithError:[NSError
errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeCancelled
userInfo:@{NSLocalizedDescriptionKey : @"Canceled by app"}]];
if (!self.isWaitingForToken) {
[self cancelCall];
@ -254,9 +256,9 @@ static NSString * const kBearerPrefix = @"Bearer ";
// Only called from the call queue.
// The handler will be called from the network queue.
- (void)startReadWithHandler:(void(^)(grpc_byte_buffer *))handler {
- (void)startReadWithHandler:(void (^)(grpc_byte_buffer *))handler {
// TODO(jcanizales): Add error handlers for async failures
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMessage alloc] initWithHandler:handler]]];
[_wrappedCall startBatchWithOperations:@[ [[GRPCOpRecvMessage alloc] initWithHandler:handler] ]];
}
// Called initially from the network queue once response headers are received,
@ -287,15 +289,21 @@ static NSString * const kBearerPrefix = @"Bearer ";
// don't want to throw, because the app shouldn't crash for a behavior
// that's on the hands of any server to have. Instead we finish and ask
// the server to cancel.
[strongSelf maybeFinishWithError:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeResourceExhausted
userInfo:@{NSLocalizedDescriptionKey: @"Client does not have enough memory to hold the server response."}]];
[strongSelf
maybeFinishWithError:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeResourceExhausted
userInfo:@{
NSLocalizedDescriptionKey :
@"Client does not have enough memory to "
@"hold the server response."
}]];
[strongSelf cancelCall];
return;
}
[strongWriteable enqueueValue:data completionHandler:^{
[strongSelf startNextRead];
}];
[strongWriteable enqueueValue:data
completionHandler:^{
[strongSelf startNextRead];
}];
}];
});
}
@ -304,11 +312,12 @@ static NSString * const kBearerPrefix = @"Bearer ";
- (void)sendHeaders:(NSDictionary *)headers {
// TODO(jcanizales): Add error handlers for async failures
GRPCOpSendMetadata *op = [[GRPCOpSendMetadata alloc] initWithMetadata:headers
flags:[GRPCCall callFlagsForHost:_host path:_path]
handler:nil]; // No clean-up needed after SEND_INITIAL_METADATA
GRPCOpSendMetadata *op = [[GRPCOpSendMetadata alloc]
initWithMetadata:headers
flags:[GRPCCall callFlagsForHost:_host path:_path]
handler:nil]; // No clean-up needed after SEND_INITIAL_METADATA
if (!_unaryCall) {
[_wrappedCall startBatchWithOperations:@[op]];
[_wrappedCall startBatchWithOperations:@[ op ]];
} else {
[_unaryOpBatch addObject:op];
}
@ -321,9 +330,8 @@ static NSString * const kBearerPrefix = @"Bearer ";
// If the call is a unary call, parameter \a errorHandler will be ignored and
// the error handler of GRPCOpSendClose will be executed in case of error.
- (void)writeMessage:(NSData *)message withErrorHandler:(void (^)(void))errorHandler {
__weak GRPCCall *weakSelf = self;
void(^resumingHandler)(void) = ^{
void (^resumingHandler)(void) = ^{
// Resume the request writer.
GRPCCall *strongSelf = weakSelf;
if (strongSelf) {
@ -333,11 +341,10 @@ static NSString * const kBearerPrefix = @"Bearer ";
}
};
GRPCOpSendMessage *op = [[GRPCOpSendMessage alloc] initWithMessage:message
handler:resumingHandler];
GRPCOpSendMessage *op =
[[GRPCOpSendMessage alloc] initWithMessage:message handler:resumingHandler];
if (!_unaryCall) {
[_wrappedCall startBatchWithOperations:@[op]
errorHandler:errorHandler];
[_wrappedCall startBatchWithOperations:@[ op ] errorHandler:errorHandler];
} else {
// Ignored errorHandler since it is the same as the one for GRPCOpSendClose.
// TODO (mxyan): unify the error handlers of all Ops into a single closure.
@ -364,12 +371,11 @@ static NSString * const kBearerPrefix = @"Bearer ";
// network queue if the requests stream couldn't be closed successfully.
- (void)finishRequestWithErrorHandler:(void (^)(void))errorHandler {
if (!_unaryCall) {
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendClose alloc] init]]
[_wrappedCall startBatchWithOperations:@[ [[GRPCOpSendClose alloc] init] ]
errorHandler:errorHandler];
} else {
[_unaryOpBatch addObject:[[GRPCOpSendClose alloc] init]];
[_wrappedCall startBatchWithOperations:_unaryOpBatch
errorHandler:errorHandler];
[_wrappedCall startBatchWithOperations:_unaryOpBatch errorHandler:errorHandler];
}
}
@ -390,13 +396,13 @@ static NSString * const kBearerPrefix = @"Bearer ";
// after this.
// The first one (headersHandler), when the response headers are received.
// The second one (completionHandler), whenever the RPC finishes for any reason.
- (void)invokeCallWithHeadersHandler:(void(^)(NSDictionary *))headersHandler
completionHandler:(void(^)(NSError *, NSDictionary *))completionHandler {
- (void)invokeCallWithHeadersHandler:(void (^)(NSDictionary *))headersHandler
completionHandler:(void (^)(NSError *, NSDictionary *))completionHandler {
// TODO(jcanizales): Add error handlers for async failures
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc]
initWithHandler:headersHandler]]];
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvStatus alloc]
initWithHandler:completionHandler]]];
[_wrappedCall
startBatchWithOperations:@[ [[GRPCOpRecvMetadata alloc] initWithHandler:headersHandler] ]];
[_wrappedCall
startBatchWithOperations:@[ [[GRPCOpRecvStatus alloc] initWithHandler:completionHandler] ]];
}
- (void)invokeCall {
@ -408,30 +414,31 @@ static NSString * const kBearerPrefix = @"Bearer ";
strongSelf.responseHeaders = headers;
[strongSelf startNextRead];
}
} completionHandler:^(NSError *error, NSDictionary *trailers) {
__strong GRPCCall *strongSelf = weakSelf;
if (strongSelf) {
strongSelf.responseTrailers = trailers;
if (error) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
if (error.userInfo) {
[userInfo addEntriesFromDictionary:error.userInfo];
}
userInfo[kGRPCTrailersKey] = strongSelf.responseTrailers;
// TODO(jcanizales): The C gRPC library doesn't guarantee that the headers block will be
// called before this one, so an error might end up with trailers but no headers. We
// shouldn't call finishWithError until ater both blocks are called. It is also when this is
// done that we can provide a merged view of response headers and trailers in a thread-safe
// way.
if (strongSelf.responseHeaders) {
userInfo[kGRPCHeadersKey] = strongSelf.responseHeaders;
}
completionHandler:^(NSError *error, NSDictionary *trailers) {
__strong GRPCCall *strongSelf = weakSelf;
if (strongSelf) {
strongSelf.responseTrailers = trailers;
if (error) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
if (error.userInfo) {
[userInfo addEntriesFromDictionary:error.userInfo];
}
userInfo[kGRPCTrailersKey] = strongSelf.responseTrailers;
// TODO(jcanizales): The C gRPC library doesn't guarantee that the headers block will be
// called before this one, so an error might end up with trailers but no headers. We
// shouldn't call finishWithError until ater both blocks are called. It is also when
// this is done that we can provide a merged view of response headers and trailers in a
// thread-safe way.
if (strongSelf.responseHeaders) {
userInfo[kGRPCHeadersKey] = strongSelf.responseHeaders;
}
error = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo];
}
[strongSelf maybeFinishWithError:error];
}
error = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo];
}
[strongSelf maybeFinishWithError:error];
}
}];
}];
// Now that the RPC has been initiated, request writes can start.
@synchronized(_requestWriter) {
[_requestWriter startWithWriteable:self];
@ -441,8 +448,8 @@ static NSString * const kBearerPrefix = @"Bearer ";
#pragma mark GRXWriter implementation
- (void)startCallWithWriteable:(id<GRXWriteable>)writeable {
_responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable
dispatchQueue:_responseQueue];
_responseWriteable =
[[GRXConcurrentWriteable alloc] initWithWriteable:writeable dispatchQueue:_responseQueue];
_wrappedCall = [[GRPCWrappedCall alloc] initWithHost:_host
serverName:_serverName
@ -453,8 +460,7 @@ static NSString * const kBearerPrefix = @"Bearer ";
[self sendHeaders:_requestHeaders];
[self invokeCall];
[GRPCConnectivityMonitor registerObserver:self
selector:@selector(connectivityChanged:)];
[GRPCConnectivityMonitor registerObserver:self selector:@selector(connectivityChanged:)];
}
- (void)startWithWriteable:(id<GRXWriteable>)writeable {
@ -472,7 +478,7 @@ static NSString * const kBearerPrefix = @"Bearer ";
if (self.tokenProvider != nil) {
self.isWaitingForToken = YES;
__weak typeof(self) weakSelf = self;
[self.tokenProvider getTokenWithHandler:^(NSString *token){
[self.tokenProvider getTokenWithHandler:^(NSString *token) {
typeof(self) strongSelf = weakSelf;
if (strongSelf && strongSelf.isWaitingForToken) {
if (token) {
@ -521,7 +527,9 @@ static NSString * const kBearerPrefix = @"Bearer ";
- (void)connectivityChanged:(NSNotification *)note {
[self maybeFinishWithError:[NSError errorWithDomain:kGRPCErrorDomain
code:GRPCErrorCodeUnavailable
userInfo:@{ NSLocalizedDescriptionKey : @"Connectivity lost." }]];
userInfo:@{
NSLocalizedDescriptionKey : @"Connectivity lost."
}]];
// Cancel underlying call upon this notification
[self cancelCall];
}

@ -23,7 +23,6 @@
@class GRPCCompletionQueue;
struct grpc_channel_credentials;
/**
* Each separate instance of this class represents at least one TCP connection to the provided host.
*/
@ -52,8 +51,9 @@ struct grpc_channel_credentials;
* @c channelArgs. Only in tests should @c GRPC_SSL_TARGET_NAME_OVERRIDE_ARG channel arg be set.
*/
+ (nonnull GRPCChannel *)secureChannelWithHost:(nonnull NSString *)host
credentials:(nonnull struct grpc_channel_credentials *)credentials
channelArgs:(nullable NSDictionary *)channelArgs;
credentials:
(nonnull struct grpc_channel_credentials *)credentials
channelArgs:(nullable NSDictionary *)channelArgs;
/**
* Creates an insecure channel to the specified @c host using the specified @c channelArgs.

@ -32,7 +32,7 @@
#endif
#import "GRPCCompletionQueue.h"
static void* copy_pointer_arg(void *p) {
static void *copy_pointer_arg(void *p) {
// Add ref count to the object when making copy
id obj = (__bridge id)p;
return (__bridge_retained void *)obj;
@ -43,12 +43,10 @@ static void destroy_pointer_arg(void *p) {
CFRelease((CFTreeRef)p);
}
static int cmp_pointer_arg(void *p, void *q) {
return p == q;
}
static int cmp_pointer_arg(void *p, void *q) { return p == q; }
static const grpc_arg_pointer_vtable objc_arg_vtable = {
copy_pointer_arg, destroy_pointer_arg, cmp_pointer_arg};
static const grpc_arg_pointer_vtable objc_arg_vtable = {copy_pointer_arg, destroy_pointer_arg,
cmp_pointer_arg};
static void FreeChannelArgs(grpc_channel_args *channel_args) {
for (size_t i = 0; i < channel_args->num_args; ++i) {
@ -124,10 +122,8 @@ static grpc_channel_args *BuildChannelArgs(NSDictionary *dictionary) {
if (self = [super init]) {
_channelArgs = BuildChannelArgs(channelArgs);
_host = [host copy];
_unmanagedChannel = grpc_cronet_secure_channel_create(cronetEngine,
_host.UTF8String,
_channelArgs,
NULL);
_unmanagedChannel =
grpc_cronet_secure_channel_create(cronetEngine, _host.UTF8String, _channelArgs, NULL);
}
return self;
@ -150,8 +146,8 @@ static grpc_channel_args *BuildChannelArgs(NSDictionary *dictionary) {
_channelArgs = BuildChannelArgs(channelArgs);
_host = [host copy];
if (secure) {
_unmanagedChannel = grpc_secure_channel_create(credentials, _host.UTF8String, _channelArgs,
NULL);
_unmanagedChannel =
grpc_secure_channel_create(credentials, _host.UTF8String, _channelArgs, NULL);
} else {
_unmanagedChannel = grpc_insecure_channel_create(_host.UTF8String, _channelArgs, NULL);
}
@ -172,8 +168,7 @@ static grpc_channel_args *BuildChannelArgs(NSDictionary *dictionary) {
channelArgs:(NSDictionary *)channelArgs {
stream_engine *engine = [GRPCCall cronetEngine];
if (!engine) {
[NSException raise:NSInvalidArgumentException
format:@"cronet_engine is NULL. Set it first."];
[NSException raise:NSInvalidArgumentException format:@"cronet_engine is NULL. Set it first."];
return nil;
}
return [[GRPCChannel alloc] initWithHost:host cronetEngine:engine channelArgs:channelArgs];
@ -191,15 +186,10 @@ static grpc_channel_args *BuildChannelArgs(NSDictionary *dictionary) {
secure:YES
credentials:credentials
channelArgs:channelArgs];
}
+ (GRPCChannel *)insecureChannelWithHost:(NSString *)host
channelArgs:(NSDictionary *)channelArgs {
return [[GRPCChannel alloc] initWithHost:host
secure:NO
credentials:NULL
channelArgs:channelArgs];
+ (GRPCChannel *)insecureChannelWithHost:(NSString *)host channelArgs:(NSDictionary *)channelArgs {
return [[GRPCChannel alloc] initWithHost:host secure:NO credentials:NULL channelArgs:channelArgs];
}
- (grpc_call *)unmanagedCallWithPath:(NSString *)path
@ -215,17 +205,13 @@ static grpc_channel_args *BuildChannelArgs(NSDictionary *dictionary) {
host_slice = grpc_slice_from_copied_string(serverName.UTF8String);
}
grpc_slice path_slice = grpc_slice_from_copied_string(path.UTF8String);
gpr_timespec deadline_ms = timeout == 0 ?
gpr_inf_future(GPR_CLOCK_REALTIME) :
gpr_time_add(
gpr_now(GPR_CLOCK_MONOTONIC),
gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN));
grpc_call *call = grpc_channel_create_call(_unmanagedChannel,
NULL, GRPC_PROPAGATE_DEFAULTS,
queue.unmanagedQueue,
path_slice,
serverName ? &host_slice : NULL,
deadline_ms, NULL);
gpr_timespec deadline_ms =
timeout == 0 ? gpr_inf_future(GPR_CLOCK_REALTIME)
: gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN));
grpc_call *call = grpc_channel_create_call(_unmanagedChannel, NULL, GRPC_PROPAGATE_DEFAULTS,
queue.unmanagedQueue, path_slice,
serverName ? &host_slice : NULL, deadline_ms, NULL);
if (serverName) {
grpc_slice_unref(host_slice);
}

@ -19,7 +19,7 @@
#import <Foundation/Foundation.h>
#include <grpc/grpc.h>
typedef void(^GRPCQueueCompletionHandler)(bool success);
typedef void (^GRPCQueueCompletionHandler)(bool success);
/**
* This class lets one more easily use |grpc_completion_queue|. To use it, pass the value of the

@ -53,9 +53,8 @@
dispatch_async(gDefaultConcurrentQueue, ^{
while (YES) {
// The following call blocks until an event is available.
grpc_event event = grpc_completion_queue_next(unmanagedQueue,
gpr_inf_future(GPR_CLOCK_REALTIME),
NULL);
grpc_event event =
grpc_completion_queue_next(unmanagedQueue, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
GRPCQueueCompletionHandler handler;
switch (event.type) {
case GRPC_OP_COMPLETE:

@ -26,7 +26,7 @@ typedef NS_ENUM(NSInteger, GRPCConnectivityStatus) {
GRPCConnectivityWiFi = 3,
};
extern NSString * _Nonnull kGRPCConnectivityNotification;
extern NSString* _Nonnull kGRPCConnectivityNotification;
// This interface monitors OS reachability interface for any network status
// change. Parties interested in these events should register themselves as
@ -39,8 +39,7 @@ extern NSString * _Nonnull kGRPCConnectivityNotification;
// must have a notification method with one parameter of type
// (NSNotification *) and should pass it to parameter \a selector. The
// parameter of this notification method is not used for now.
+ (void)registerObserver:(_Nonnull id)observer
selector:(_Nonnull SEL)selector;
+ (void)registerObserver:(_Nonnull id)observer selector:(_Nonnull SEL)selector;
// Ungegister an object from observers of network status change.
+ (void)unregisterObserver:(_Nonnull id)observer;

@ -41,8 +41,8 @@ GRPCConnectivityStatus CalculateConnectivityStatus(SCNetworkReachabilityFlags fl
return result;
}
static void ReachabilityCallback(
SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) {
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags,
void *info) {
GRPCConnectivityStatus newStatus = CalculateConnectivityStatus(flags);
if (newStatus != currentStatus) {
@ -69,15 +69,14 @@ static void ReachabilityCallback(
SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
if (!SCNetworkReachabilitySetCallback(reachability, ReachabilityCallback, &context) ||
!SCNetworkReachabilityScheduleWithRunLoop(
reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes)) {
!SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(),
kCFRunLoopCommonModes)) {
NSLog(@"gRPC connectivity monitor fail to set");
}
}
}
+ (void)registerObserver:(_Nonnull id)observer
selector:(SEL)selector {
+ (void)registerObserver:(_Nonnull id)observer selector:(SEL)selector {
[[NSNotificationCenter defaultCenter] addObserver:observer
selector:selector
name:kGRPCConnectivityNotification

@ -47,7 +47,6 @@ struct grpc_channel_credentials;
/** The default response size limit is 4MB. Set this to override that default. */
@property(nonatomic, strong, nullable) NSNumber *responseSizeLimitOverride;
- (nullable instancetype)init NS_UNAVAILABLE;
/** Host objects initialized with the same address are the same. */
+ (nullable instancetype)hostWithAddress:(NSString *)address;

@ -18,10 +18,10 @@
#import "GRPCHost.h"
#import <GRPCClient/GRPCCall+MobileLog.h>
#import <GRPCClient/GRPCCall.h>
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#import <GRPCClient/GRPCCall.h>
#import <GRPCClient/GRPCCall+MobileLog.h>
#ifdef GRPC_COMPILE_WITH_CRONET
#import <GRPCClient/GRPCCall+ChannelArg.h>
#import <GRPCClient/GRPCCall+Cronet.h>
@ -91,16 +91,15 @@ static NSMutableDictionary *kHostCache;
+ (void)flushChannelCache {
@synchronized(kHostCache) {
[kHostCache enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key,
GRPCHost * _Nonnull host,
BOOL * _Nonnull stop) {
[kHostCache enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, GRPCHost *_Nonnull host,
BOOL *_Nonnull stop) {
[host disconnect];
}];
}
}
+ (void)resetAllHostSettings {
@synchronized (kHostCache) {
@synchronized(kHostCache) {
kHostCache = [NSMutableDictionary dictionary];
}
}
@ -131,38 +130,38 @@ static NSMutableDictionary *kHostCache;
static NSError *kDefaultRootsError;
static dispatch_once_t loading;
dispatch_once(&loading, ^{
NSString *defaultPath = @"gRPCCertificates.bundle/roots"; // .pem
NSString *defaultPath = @"gRPCCertificates.bundle/roots"; // .pem
// Do not use NSBundle.mainBundle, as it's nil for tests of library projects.
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSString *path = [bundle pathForResource:defaultPath ofType:@"pem"];
NSError *error;
// Files in PEM format can have non-ASCII characters in their comments (e.g. for the name of the
// issuer). Load them as UTF8 and produce an ASCII equivalent.
NSString *contentInUTF8 = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
NSString *contentInUTF8 =
[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (contentInUTF8 == nil) {
kDefaultRootsError = error;
return;
}
kDefaultRootsASCII = [contentInUTF8 dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
kDefaultRootsASCII =
[contentInUTF8 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
});
NSData *rootsASCII;
if (pemRootCerts != nil) {
rootsASCII = [pemRootCerts dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
rootsASCII = [pemRootCerts dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
} else {
if (kDefaultRootsASCII == nil) {
if (errorPtr) {
*errorPtr = kDefaultRootsError;
}
NSAssert(kDefaultRootsASCII, @"Could not read gRPCCertificates.bundle/roots.pem. This file, "
"with the root certificates, is needed to establish secure (TLS) connections. "
"Because the file is distributed with the gRPC library, this error is usually a sign "
"that the library wasn't configured correctly for your project. Error: %@",
kDefaultRootsError);
NSAssert(
kDefaultRootsASCII,
@"Could not read gRPCCertificates.bundle/roots.pem. This file, "
"with the root certificates, is needed to establish secure (TLS) connections. "
"Because the file is distributed with the gRPC library, this error is usually a sign "
"that the library wasn't configured correctly for your project. Error: %@",
kDefaultRootsError);
return NO;
}
rootsASCII = kDefaultRootsASCII;
@ -173,10 +172,10 @@ static NSMutableDictionary *kHostCache;
creds = grpc_ssl_credentials_create(rootsASCII.bytes, NULL, NULL);
} else {
grpc_ssl_pem_key_cert_pair key_cert_pair;
NSData *privateKeyASCII = [pemPrivateKey dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSData *certChainASCII = [pemCertChain dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSData *privateKeyASCII =
[pemPrivateKey dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSData *certChainASCII =
[pemCertChain dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
key_cert_pair.private_key = privateKeyASCII.bytes;
key_cert_pair.cert_chain = certChainASCII.bytes;
creds = grpc_ssl_credentials_create(rootsASCII.bytes, &key_cert_pair, NULL);
@ -212,8 +211,7 @@ static NSMutableDictionary *kHostCache;
}
if (_compressAlgorithm != GRPC_COMPRESS_NONE) {
args[@GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM] =
[NSNumber numberWithInt:_compressAlgorithm];
args[@GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM] = [NSNumber numberWithInt:_compressAlgorithm];
}
if (_keepaliveInterval != 0) {
@ -247,14 +245,12 @@ static NSMutableDictionary *kHostCache;
}
#ifdef GRPC_COMPILE_WITH_CRONET
if (useCronet) {
channel = [GRPCChannel secureCronetChannelWithHost:_address
channelArgs:args];
channel = [GRPCChannel secureCronetChannelWithHost:_address channelArgs:args];
} else
#endif
{
channel = [GRPCChannel secureChannelWithHost:_address
credentials:_channelCreds
channelArgs:args];
channel =
[GRPCChannel secureChannelWithHost:_address credentials:_channelCreds channelArgs:args];
}
}
return channel;

@ -16,7 +16,6 @@
*
*/
#ifdef GRPC_TEST_OBJC
/**

@ -25,7 +25,7 @@ static NSMutableArray *opBatchLog = nil;
@implementation GRPCOpBatchLog
+ (void)enableOpBatchLog:(BOOL)enabled {
@synchronized (opBatchLog) {
@synchronized(opBatchLog) {
if (enabled) {
if (!opBatchLog) {
opBatchLog = [NSMutableArray array];
@ -39,13 +39,13 @@ static NSMutableArray *opBatchLog = nil;
}
+ (void)addOpBatchToLog:(NSArray *)batch {
@synchronized (opBatchLog) {
@synchronized(opBatchLog) {
[opBatchLog addObject:batch];
}
}
+ (NSArray *)obtainAndCleanOpBatchLog {
@synchronized (opBatchLog) {
@synchronized(opBatchLog) {
NSArray *out = opBatchLog;
opBatchLog = [NSMutableArray array];
return out;

@ -17,7 +17,8 @@
*/
/**
* "X-macro" file that lists the flags names of Apple's Network Reachability API, along with a nice
* "X-macro" file that lists the flags names of Apple's Network Reachability
API, along with a nice
* Objective-C method name used to query each of them.
*
* Example usage: To generate a dictionary from flag value to name, one can do:
@ -29,7 +30,8 @@
#undef GRPC_XMACRO_ITEM
};
XCTAssertEqualObjects(flagNames[@(kSCNetworkReachabilityFlagsIsWWAN)], @"isCell");
XCTAssertEqualObjects(flagNames[@(kSCNetworkReachabilityFlagsIsWWAN)],
@"isCell");
*/

@ -23,7 +23,7 @@
#import "NSDictionary+GRPC.h"
// Used by the setter.
static void CheckIsNonNilASCII(NSString *name, NSString* value) {
static void CheckIsNonNilASCII(NSString *name, NSString *value) {
if (!value) {
[NSException raise:NSInvalidArgumentException format:@"%@ cannot be nil", name];
}
@ -38,14 +38,18 @@ static void CheckKeyValuePairIsValid(NSString *key, id value) {
if ([key hasSuffix:@"-bin"]) {
if (![value isKindOfClass:NSData.class]) {
[NSException raise:NSInvalidArgumentException
format:@"Expected NSData value for header %@ ending in \"-bin\", "
@"instead got %@", key, value];
format:
@"Expected NSData value for header %@ ending in \"-bin\", "
@"instead got %@",
key, value];
}
} else {
if (![value isKindOfClass:NSString.class]) {
[NSException raise:NSInvalidArgumentException
format:@"Expected NSString value for header %@ not ending in \"-bin\", "
@"instead got %@", key, value];
format:
@"Expected NSString value for header %@ not ending in \"-bin\", "
@"instead got %@",
key, value];
}
CheckIsNonNilASCII(@"Text header value", (NSString *)value);
}
@ -85,8 +89,8 @@ static void CheckKeyValuePairIsValid(NSString *key, id value) {
return self;
}
- (instancetype)initWithObjects:(const id _Nonnull __unsafe_unretained *)objects
forKeys:(const id<NSCopying> _Nonnull __unsafe_unretained *)keys
- (instancetype)initWithObjects:(const id _Nonnull __unsafe_unretained *)objects
forKeys:(const id<NSCopying> _Nonnull __unsafe_unretained *)keys
count:(NSUInteger)cnt {
return [self init];
}
@ -118,7 +122,7 @@ static void CheckKeyValuePairIsValid(NSString *key, id value) {
return _delegate.count;
}
- (NSEnumerator * _Nonnull)keyEnumerator {
- (NSEnumerator *_Nonnull)keyEnumerator {
return [_delegate keyEnumerator];
}

@ -29,43 +29,42 @@
@interface GRPCOpSendMetadata : GRPCOperation
- (instancetype)initWithMetadata:(NSDictionary *)metadata
handler:(void(^)(void))handler;
- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler;
- (instancetype)initWithMetadata:(NSDictionary *)metadata
flags:(uint32_t)flags
handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER;
handler:(void (^)(void))handler NS_DESIGNATED_INITIALIZER;
@end
@interface GRPCOpSendMessage : GRPCOperation
- (instancetype)initWithMessage:(NSData *)message
handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER;
handler:(void (^)(void))handler NS_DESIGNATED_INITIALIZER;
@end
@interface GRPCOpSendClose : GRPCOperation
- (instancetype)initWithHandler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithHandler:(void (^)(void))handler NS_DESIGNATED_INITIALIZER;
@end
@interface GRPCOpRecvMetadata : GRPCOperation
- (instancetype)initWithHandler:(void(^)(NSDictionary *))handler NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithHandler:(void (^)(NSDictionary *))handler NS_DESIGNATED_INITIALIZER;
@end
@interface GRPCOpRecvMessage : GRPCOperation
- (instancetype)initWithHandler:(void(^)(grpc_byte_buffer *))handler NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler NS_DESIGNATED_INITIALIZER;
@end
@interface GRPCOpRecvStatus : GRPCOperation
- (instancetype)initWithHandler:(void(^)(NSError *, NSDictionary *))handler
- (instancetype)initWithHandler:(void (^)(NSError *, NSDictionary *))handler
NS_DESIGNATED_INITIALIZER;
@end
@ -79,7 +78,7 @@
path:(NSString *)path
timeout:(NSTimeInterval)timeout NS_DESIGNATED_INITIALIZER;
- (void)startBatchWithOperations:(NSArray *)ops errorHandler:(void(^)(void))errorHandler;
- (void)startBatchWithOperations:(NSArray *)ops errorHandler:(void (^)(void))errorHandler;
- (void)startBatchWithOperations:(NSArray *)ops;

@ -19,29 +19,29 @@
#import "GRPCWrappedCall.h"
#import <Foundation/Foundation.h>
#include <grpc/grpc.h>
#include <grpc/byte_buffer.h>
#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#import "GRPCCompletionQueue.h"
#import "GRPCHost.h"
#import "NSDictionary+GRPC.h"
#import "NSData+GRPC.h"
#import "NSDictionary+GRPC.h"
#import "NSError+GRPC.h"
#import "GRPCOpBatchLog.h"
@implementation GRPCOperation {
@protected
@protected
// Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being
// initialized to zero.
grpc_op _op;
void(^_handler)(void);
void (^_handler)(void);
}
- (void)finish {
if (_handler) {
void(^handler)(void) = _handler;
void (^handler)(void) = _handler;
_handler = nil;
handler();
}
@ -54,8 +54,7 @@
return [self initWithMetadata:nil flags:0 handler:nil];
}
- (instancetype)initWithMetadata:(NSDictionary *)metadata
handler:(void (^)(void))handler {
- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler {
return [self initWithMetadata:metadata flags:0 handler:handler];
}
@ -128,11 +127,11 @@
grpc_metadata_array _headers;
}
- (instancetype) init {
- (instancetype)init {
return [self initWithHandler:nil];
}
- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
- (instancetype)initWithHandler:(void (^)(NSDictionary *))handler {
if (self = [super init]) {
_op.op = GRPC_OP_RECV_INITIAL_METADATA;
grpc_metadata_array_init(&_headers);
@ -142,8 +141,8 @@
__weak typeof(self) weakSelf = self;
_handler = ^{
__strong typeof(self) strongSelf = weakSelf;
NSDictionary *metadata = [NSDictionary
grpc_dictionaryFromMetadataArray:strongSelf->_headers];
NSDictionary *metadata =
[NSDictionary grpc_dictionaryFromMetadataArray:strongSelf->_headers];
handler(metadata);
};
}
@ -157,7 +156,7 @@
@end
@implementation GRPCOpRecvMessage{
@implementation GRPCOpRecvMessage {
grpc_byte_buffer *_receivedMessage;
}
@ -183,18 +182,18 @@
@end
@implementation GRPCOpRecvStatus{
@implementation GRPCOpRecvStatus {
grpc_status_code _statusCode;
grpc_slice _details;
size_t _detailsCapacity;
grpc_metadata_array _trailers;
}
- (instancetype) init {
- (instancetype)init {
return [self initWithHandler:nil];
}
- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
- (instancetype)initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
if (self = [super init]) {
_op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
_op.data.recv_status_on_client.status = &_statusCode;
@ -208,10 +207,10 @@
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) {
char *details = grpc_slice_to_c_string(strongSelf->_details);
NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
details:details];
NSDictionary *trailers = [NSDictionary
grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
NSError *error =
[NSError grpc_errorFromStatusCode:strongSelf->_statusCode details:details];
NSDictionary *trailers =
[NSDictionary grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
handler(error, trailers);
gpr_free(details);
}
@ -244,8 +243,7 @@
path:(NSString *)path
timeout:(NSTimeInterval)timeout {
if (!path || !host) {
[NSException raise:NSInvalidArgumentException
format:@"path and host cannot be nil."];
[NSException raise:NSInvalidArgumentException format:@"path and host cannot be nil."];
}
if (self = [super init]) {
@ -270,8 +268,8 @@
}
- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)(void))errorHandler {
// Keep logs of op batches when we are running tests. Disabled when in production for improved
// performance.
// Keep logs of op batches when we are running tests. Disabled when in production for improved
// performance.
#ifdef GRPC_TEST_OBJC
[GRPCOpBatchLog addOpBatchToLog:operations];
#endif
@ -282,25 +280,26 @@
for (GRPCOperation *operation in operations) {
ops_array[i++] = operation.op;
}
grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
(__bridge_retained void *)(^(bool success){
if (!success) {
if (errorHandler) {
errorHandler();
} else {
return;
}
}
for (GRPCOperation *operation in operations) {
[operation finish];
}
}), NULL);
grpc_call_error error =
grpc_call_start_batch(_call, ops_array, nops, (__bridge_retained void *)(^(bool success) {
if (!success) {
if (errorHandler) {
errorHandler();
} else {
return;
}
}
for (GRPCOperation *operation in operations) {
[operation finish];
}
}),
NULL);
gpr_free(ops_array);
if (error != GRPC_CALL_OK) {
[NSException raise:NSInternalInconsistencyException
format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i",
error];
[NSException
raise:NSInternalInconsistencyException
format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i", error];
}
}

@ -24,8 +24,8 @@
// TODO(jcanizales): Move these two incantations to the C library.
static void MallocAndCopyByteBufferToCharArray(grpc_byte_buffer *buffer,
size_t *length, char **array) {
static void MallocAndCopyByteBufferToCharArray(grpc_byte_buffer *buffer, size_t *length,
char **array) {
grpc_byte_buffer_reader reader;
if (!grpc_byte_buffer_reader_init(&reader, buffer)) {
// grpc_byte_buffer_reader_init can fail if the data sent by the server
@ -51,8 +51,7 @@ static void MallocAndCopyByteBufferToCharArray(grpc_byte_buffer *buffer,
grpc_byte_buffer_reader_destroy(&reader);
}
static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array,
size_t length) {
static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array, size_t length) {
grpc_slice slice = grpc_slice_from_copied_buffer(array, length);
grpc_byte_buffer *buffer = grpc_raw_byte_buffer_create(&slice, 1);
grpc_slice_unref(slice);
@ -89,7 +88,6 @@ static grpc_byte_buffer *CopyCharArrayToNewByteBuffer(const char *array,
// to create an array of grpc_slice objects to pass to
// grpc_raw_byte_buffer_create.
// That would make it do exactly one copy, always.
return CopyCharArrayToNewByteBuffer((const char *)self.bytes,
(size_t)self.length);
return CopyCharArrayToNewByteBuffer((const char *)self.bytes, (size_t)self.length);
}
@end

@ -74,8 +74,7 @@
NSMutableDictionary *metadata = [NSMutableDictionary dictionaryWithCapacity:count];
for (grpc_metadata *entry = entries; entry < entries + count; entry++) {
char *key = grpc_slice_to_c_string(entry->key);
NSString *name = [NSString stringWithCString:key
encoding:NSASCIIStringEncoding];
NSString *name = [NSString stringWithCString:key encoding:NSASCIIStringEncoding];
gpr_free(key);
if (!name || metadata[name]) {
// Log if name is nil?
@ -97,7 +96,7 @@
- (grpc_metadata *)grpc_metadataArray {
grpc_metadata *metadata = gpr_malloc([self count] * sizeof(grpc_metadata));
grpc_metadata *current = metadata;
for (NSString* key in self) {
for (NSString *key in self) {
id value = self[key];
current->key = grpc_slice_from_copied_string(key.UTF8String);
if ([value respondsToSelector:@selector(grpc_initMetadata:)]) {

@ -20,7 +20,7 @@
#include <grpc/grpc.h>
NSString * const kGRPCErrorDomain = @"io.grpc";
NSString *const kGRPCErrorDomain = @"io.grpc";
@implementation NSError (GRPC)
+ (instancetype)grpc_errorFromStatusCode:(grpc_status_code)statusCode details:(char *)details {
@ -30,6 +30,6 @@ NSString * const kGRPCErrorDomain = @"io.grpc";
NSString *message = [NSString stringWithCString:details encoding:NSASCIIStringEncoding];
return [NSError errorWithDomain:kGRPCErrorDomain
code:statusCode
userInfo:@{NSLocalizedDescriptionKey: message}];
userInfo:@{NSLocalizedDescriptionKey : message}];
}
@end

@ -22,5 +22,4 @@
// instead. This file can be regenerated from the template by running
// `tools/buildgen/generate_projects.sh`.
#define GRPC_OBJC_VERSION_STRING @"1.12.0-dev"

@ -22,9 +22,8 @@
* A fully-qualified proto service method name. Full qualification is needed because a gRPC endpoint
* can implement multiple services.
*/
__attribute__((deprecated("Please use GRPCProtoMethod.")))
@interface ProtoMethod : NSObject
@property(nonatomic, readonly) NSString *package;
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod
: NSObject @property(nonatomic, readonly) NSString *package;
@property(nonatomic, readonly) NSString *service;
@property(nonatomic, readonly) NSString *method;
@ -41,7 +40,7 @@ __attribute__((deprecated("Please use GRPCProtoMethod.")))
*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@interface GRPCProtoMethod : ProtoMethod
@interface GRPCProtoMethod : ProtoMethod
#pragma clang diagnostic pop
@end
@end

@ -21,18 +21,18 @@
#import "ProtoMethod.h"
__attribute__((deprecated("Please use GRPCProtoCall.")))
@interface ProtoRPC : GRPCCall
__attribute__((deprecated("Please use GRPCProtoCall."))) @interface ProtoRPC
: GRPCCall
/**
* host parameter should not contain the scheme (http:// or https://), only the name or IP addr
* and the port number, for example @"localhost:5050".
*/
- (instancetype)initWithHost:(NSString *)host
method:(GRPCProtoMethod *)method
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable NS_DESIGNATED_INITIALIZER;
/**
* host parameter should not contain the scheme (http:// or https://), only the name or IP
* addr and the port number, for example @"localhost:5050".
*/
-
(instancetype)initWithHost : (NSString *)host method
: (GRPCProtoMethod *)method requestsWriter : (GRXWriter *)requestsWriter responseClass
: (Class)responseClass responsesWriteable
: (id<GRXWriteable>)responsesWriteable NS_DESIGNATED_INITIALIZER;
- (void)start;
@end
@ -43,7 +43,7 @@ __attribute__((deprecated("Please use GRPCProtoCall.")))
*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@interface GRPCProtoCall : ProtoRPC
@interface GRPCProtoCall : ProtoRPC
#pragma clang diagnostic pop
@end
@end

@ -19,27 +19,26 @@
#import "ProtoRPC.h"
#if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS
#import <Protobuf/GPBProtocolBuffers.h>
#import <Protobuf/GPBProtocolBuffers.h>
#else
#import <GPBProtocolBuffers.h>
#import <GPBProtocolBuffers.h>
#endif
#import <RxLibrary/GRXWriteable.h>
#import <RxLibrary/GRXWriter+Transformations.h>
static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsingError) {
NSDictionary *info = @{
NSLocalizedDescriptionKey: @"Unable to parse response from the server",
NSLocalizedRecoverySuggestionErrorKey: @"If this RPC is idempotent, retry "
@"with exponential backoff. Otherwise, query the server status before "
@"retrying.",
NSUnderlyingErrorKey: parsingError,
@"Expected class": expectedClass,
@"Received value": proto,
};
NSLocalizedDescriptionKey : @"Unable to parse response from the server",
NSLocalizedRecoverySuggestionErrorKey :
@"If this RPC is idempotent, retry "
@"with exponential backoff. Otherwise, query the server status before "
@"retrying.",
NSUnderlyingErrorKey : parsingError,
@"Expected class" : expectedClass,
@"Received value" : proto,
};
// TODO(jcanizales): Use kGRPCErrorDomain and GRPCErrorCodeInternal when they're public.
return [NSError errorWithDomain:@"io.grpc"
code:13
userInfo:info];
return [NSError errorWithDomain:@"io.grpc" code:13 userInfo:info];
}
#pragma clang diagnostic push
@ -92,9 +91,10 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
} else {
[weakSelf finishWithError:ErrorForBadProto(value, responseClass, error)];
}
} completionHandler:^(NSError *errorOrNil) {
[responsesWriteable writesFinishedWithError:errorOrNil];
}];
}
completionHandler:^(NSError *errorOrNil) {
[responsesWriteable writesFinishedWithError:errorOrNil];
}];
}
return self;
}

@ -22,27 +22,24 @@
@protocol GRXWriteable;
@class GRXWriter;
__attribute__((deprecated("Please use GRPCProtoService.")))
@interface ProtoService : NSObject
- (instancetype)initWithHost:(NSString *)host
packageName:(NSString *)packageName
serviceName:(NSString *)serviceName NS_DESIGNATED_INITIALIZER;
__attribute__((deprecated("Please use GRPCProtoService."))) @interface ProtoService
: NSObject -
(instancetype)initWithHost : (NSString *)host packageName
: (NSString *)packageName serviceName : (NSString *)serviceName NS_DESIGNATED_INITIALIZER;
- (GRPCProtoCall *)RPCToMethod:(NSString *)method
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable;
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable;
@end
/**
* This subclass is empty now. Eventually we'll remove ProtoService class
* to avoid potential naming conflict
*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@interface GRPCProtoService : ProtoService
@interface GRPCProtoService : ProtoService
#pragma clang diagnostic pop
@end
@end

@ -57,9 +57,8 @@
requestsWriter:(GRXWriter *)requestsWriter
responseClass:(Class)responseClass
responsesWriteable:(id<GRXWriteable>)responsesWriteable {
GRPCProtoMethod *methodName = [[GRPCProtoMethod alloc] initWithPackage:_packageName
service:_serviceName
method:method];
GRPCProtoMethod *methodName =
[[GRPCProtoMethod alloc] initWithPackage:_packageName service:_serviceName method:method];
return [[GRPCProtoCall alloc] initWithHost:_host
method:methodName
requestsWriter:requestsWriter

@ -67,7 +67,7 @@
#pragma mark GRXWriter implementation
- (void)setState:(GRXWriterState)newState {
@synchronized (self) {
@synchronized(self) {
// Manual transitions are only allowed from the started or paused states.
if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) {
return;
@ -112,8 +112,7 @@
- (void)dealloc {
GRXWriterState state = self.state;
if (state == GRXWriterStateNotStarted ||
state == GRXWriterStatePaused) {
if (state == GRXWriterStateNotStarted || state == GRXWriterStatePaused) {
dispatch_resume(_writeQueue);
}
}

@ -18,8 +18,8 @@
#import <Foundation/Foundation.h>
#import "GRXWriter.h"
#import "GRXWriteable.h"
#import "GRXWriter.h"
/**
* This is a thread-safe wrapper over a GRXWriteable instance. It lets one enqueue calls to a

@ -46,8 +46,7 @@
}
- (instancetype)initWithWriteable:(id<GRXWriteable>)writeable {
return [self initWithWriteable:writeable
dispatchQueue:dispatch_get_main_queue()];
return [self initWithWriteable:writeable dispatchQueue:dispatch_get_main_queue()];
}
- (void)enqueueValue:(id)value completionHandler:(void (^)(void))handler {
@ -69,7 +68,7 @@
typeof(self) strongSelf = weakSelf;
if (strongSelf) {
BOOL finished = NO;
@synchronized (self) {
@synchronized(self) {
if (!strongSelf->_alreadyFinished) {
strongSelf->_alreadyFinished = YES;
} else {
@ -90,7 +89,7 @@
- (void)cancelWithError:(NSError *)error {
NSAssert(error, @"For a successful completion, use enqueueSuccessfulCompletion.");
BOOL finished = NO;
@synchronized (self) {
@synchronized(self) {
if (!_alreadyFinished) {
_alreadyFinished = YES;
} else {
@ -112,7 +111,7 @@
- (void)cancelSilently {
BOOL finished = NO;
@synchronized (self) {
@synchronized(self) {
if (!_alreadyFinished) {
_alreadyFinished = YES;
} else {

@ -18,7 +18,7 @@
#import "GRXForwardingWriter.h"
@interface GRXForwardingWriter () <GRXWriteable>
@interface GRXForwardingWriter ()<GRXWriteable>
@end
@implementation GRXForwardingWriter {

@ -28,8 +28,8 @@
@synthesize state = _state;
- (instancetype) init {
return [self initWithEnumerator:nil error:nil]; // results in an empty writer.
- (instancetype)init {
return [self initWithEnumerator:nil error:nil]; // results in an empty writer.
}
// Designated initializer
@ -57,7 +57,8 @@
}
+ (GRXWriter *)writerWithContainer:(id<NSFastEnumeration>)container {
return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithContainer:container]];;
return [self writerWithEnumerator:[NSEnumerator grx_enumeratorWithContainer:container]];
;
}
+ (GRXWriter *)writerWithValue:(id)value {

@ -22,7 +22,7 @@
* A GRXWriteable is an object to which a sequence of values can be sent. The
* sequence finishes with an optional error.
*/
@protocol GRXWriteable <NSObject>
@protocol GRXWriteable<NSObject>
/** Push the next value of the sequence to the receiving object. */
- (void)writeValue:(id)value;

@ -42,9 +42,8 @@
} else if (error) {
singleHandler(nil, error);
} else {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: @"The writer finished without producing any value."
};
NSDictionary *userInfo =
@{NSLocalizedDescriptionKey : @"The writer finished without producing any value."};
// Even though RxLibrary is independent of gRPC, the domain and code here are, for the moment,
// set to the values of kGRPCErrorDomain and GRPCErrorCodeInternal. This way, the error formed
// is the one user of gRPC would expect if the server failed to produce a response.
@ -55,9 +54,9 @@
// the two domains.
static NSString *kGRPCErrorDomain = @"io.grpc";
static NSUInteger kGRPCErrorCodeInternal = 13;
singleHandler(nil, [NSError errorWithDomain:kGRPCErrorDomain
code:kGRPCErrorCodeInternal
userInfo:userInfo]);
singleHandler(
nil,
[NSError errorWithDomain:kGRPCErrorDomain code:kGRPCErrorCodeInternal userInfo:userInfo]);
}
};
return [self writeableWithEventHandler:^(BOOL done, id value, NSError *error) {
@ -73,9 +72,10 @@
}
return [[self alloc] initWithValueHandler:^(id value) {
handler(NO, value, nil);
} completionHandler:^(NSError *errorOrNil) {
handler(YES, nil, errorOrNil);
}];
}
completionHandler:^(NSError *errorOrNil) {
handler(YES, nil, errorOrNil);
}];
}
- (instancetype)init {

@ -18,8 +18,8 @@
#import "GRXWriter+Immediate.h"
#import "GRXImmediateWriter.h"
#import "GRXImmediateSingleWriter.h"
#import "GRXImmediateWriter.h"
@implementation GRXWriter (Immediate)

@ -18,8 +18,9 @@
#import "RxLibrary/GRXForwardingWriter.h"
/** A "proxy" writer that transforms all the values of its input writer by using a mapping function. */
/** A "proxy" writer that transforms all the values of its input writer by using a mapping function.
*/
@interface GRXMappingWriter : GRXForwardingWriter
- (instancetype)initWithWriter:(GRXWriter *)writer map:(id (^)(id value))map
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithWriter:(GRXWriter *)writer
map:(id (^)(id value))map NS_DESIGNATED_INITIALIZER;
@end

@ -18,7 +18,7 @@
#import "GRXMappingWriter.h"
@interface GRXForwardingWriter () <GRXWriteable>
@interface GRXForwardingWriter ()<GRXWriteable>
@end
@implementation GRXMappingWriter {

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

@ -22,15 +22,15 @@
#import <ProtoRPC/ProtoMethod.h>
#import <RemoteTest/Messages.pbobjc.h>
#import <RemoteTest/Test.pbrpc.h>
#import <RxLibrary/GRXWriter+Immediate.h>
#import <RxLibrary/GRXWriteable.h>
#import <RxLibrary/GRXWriter+Immediate.h>
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString * const kRemoteHost = @"grpc-test.sandbox.googleapis.com";
NSString *const kRemoteHost = @"grpc-test.sandbox.googleapis.com";
RMTSimpleRequest *request = [[RMTSimpleRequest alloc] init];
request.responseSize = 10;
@ -40,14 +40,14 @@
// Example gRPC call using a generated proto client library:
RMTTestService *service = [[RMTTestService alloc] initWithHost:kRemoteHost];
[service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) {
if (response) {
NSLog(@"Finished successfully with response:\n%@", response);
} else if (error) {
NSLog(@"Finished with error: %@", error);
}
}];
[service unaryCallWithRequest:request
handler:^(RMTSimpleResponse *response, NSError *error) {
if (response) {
NSLog(@"Finished successfully with response:\n%@", response);
} else if (error) {
NSLog(@"Finished with error: %@", error);
}
}];
// Same example call using the generic gRPC client library:
@ -61,16 +61,18 @@
path:method.HTTPPath
requestsWriter:requestsWriter];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value error:NULL];
NSLog(@"Received response:\n%@", response);
} completionHandler:^(NSError *errorOrNil) {
if (errorOrNil) {
NSLog(@"Finished with error: %@", errorOrNil);
} else {
NSLog(@"Finished successfully.");
}
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
RMTSimpleResponse *response = [RMTSimpleResponse parseFromData:value error:NULL];
NSLog(@"Received response:\n%@", response);
}
completionHandler:^(NSError *errorOrNil) {
if (errorOrNil) {
NSLog(@"Finished with error: %@", errorOrNil);
} else {
NSLog(@"Finished successfully.");
}
}];
[call startWithWriteable:responsesWriteable];
}

@ -19,8 +19,8 @@
#import <UIKit/UIKit.h>
#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]));
}
}

@ -20,9 +20,9 @@
#import <GRPCClient/GRPCCall.h>
#import <ProtoRPC/ProtoMethod.h>
#import <RxLibrary/GRXBufferedPipe.h>
#import <RxLibrary/GRXWriter+Immediate.h>
#import <RxLibrary/GRXWriter+Transformations.h>
#import <RxLibrary/GRXBufferedPipe.h>
#import "src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.h"
@ -35,8 +35,7 @@ NSString *host = @"grpc-test.sandbox.googleapis.com";
- (void)viewDidLoad {
[super viewDidLoad];
[GRPCConnectivityMonitor registerObserver:self
selector:@selector(reachabilityChanged:)];
[GRPCConnectivityMonitor registerObserver:self selector:@selector(reachabilityChanged:)];
}
- (void)reachabilityChanged:(NSNotification *)note {
@ -52,23 +51,21 @@ NSString *host = @"grpc-test.sandbox.googleapis.com";
GRPCProtoMethod *method = [[GRPCProtoMethod alloc] initWithPackage:@"grpc.testing"
service:@"TestService"
method:@"UnaryCall"];
GRXWriter *loggingRequestWriter =
[[GRXWriter writerWithValue:[NSData dataWithBytes:bytes length:sizeof(bytes)]]
map:^id(id value) {
NSLog(@"Sending request.");
return value;
}];
GRPCCall *call = [[GRPCCall alloc] initWithHost:host
path:method.HTTPPath
requestsWriter:loggingRequestWriter];
[call startWithWriteable:[GRXWriteable writeableWithEventHandler:^(BOOL done, id value,
NSError *error) {
if (!done) {
return;
}
NSLog(@"Unary call finished with error: %@", error);
}]];
GRXWriter *loggingRequestWriter = [[GRXWriter
writerWithValue:[NSData dataWithBytes:bytes length:sizeof(bytes)]] map:^id(id value) {
NSLog(@"Sending request.");
return value;
}];
GRPCCall *call =
[[GRPCCall alloc] initWithHost:host path:method.HTTPPath requestsWriter:loggingRequestWriter];
[call startWithWriteable:[GRXWriteable
writeableWithEventHandler:^(BOOL done, id value, NSError *error) {
if (!done) {
return;
}
NSLog(@"Unary call finished with error: %@", error);
}]];
}
- (IBAction)tapStreaming:(id)sender {
@ -85,17 +82,16 @@ NSString *host = @"grpc-test.sandbox.googleapis.com";
[requestsBuffer writeValue:[NSData dataWithBytes:bytes length:sizeof(bytes)]];
GRPCCall *call = [[GRPCCall alloc] initWithHost:host
path:method.HTTPPath
requestsWriter:requestsBuffer];
[call startWithWriteable:[GRXWriteable writeableWithEventHandler:^(BOOL done, id value,
NSError *error) {
if (!done) {
return;
}
NSLog(@"Streaming call finished with error: %@", error);
}]];
GRPCCall *call =
[[GRPCCall alloc] initWithHost:host path:method.HTTPPath requestsWriter:requestsBuffer];
[call startWithWriteable:[GRXWriteable
writeableWithEventHandler:^(BOOL done, id value, NSError *error) {
if (!done) {
return;
}
NSLog(@"Streaming call finished with error: %@", error);
}]];
}
@end

@ -18,14 +18,14 @@
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@interface AppDelegate : UIResponder<UIApplicationDelegate>
@property(strong, nonatomic) UIWindow *window;
@end
@implementation AppDelegate
@end
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));
}
}

@ -37,11 +37,11 @@
#include <grpc/support/log.h>
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "src/core/lib/gpr/env.h"
#include "src/core/lib/gpr/host_port.h"
#include "src/core/lib/gpr/string.h"
#include "src/core/lib/gpr/tmpfile.h"
#include "src/core/lib/security/credentials/credentials.h"
#include "test/core/end2end/data/ssl_test_data.h"
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
@ -58,7 +58,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
grpc_end2end_test_fixture f;
int port = grpc_pick_unused_port_or_die();
fullstack_secure_fixture_data *ffd =
(fullstack_secure_fixture_data*)gpr_malloc(sizeof(fullstack_secure_fixture_data));
(fullstack_secure_fixture_data *)gpr_malloc(sizeof(fullstack_secure_fixture_data));
memset(&f, 0, sizeof(f));
gpr_join_host_port(&ffd->localaddr, "127.0.0.1", port);
@ -70,9 +70,8 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack(
return f;
}
static void process_auth_failure(void *state, grpc_auth_context *ctx,
const grpc_metadata *md, size_t md_count,
grpc_process_auth_metadata_done_cb cb,
static void process_auth_failure(void *state, grpc_auth_context *ctx, const grpc_metadata *md,
size_t md_count, grpc_process_auth_metadata_done_cb cb,
void *user_data) {
GPR_ASSERT(state == NULL);
cb(user_data, NULL, 0, NULL, 0, GRPC_STATUS_UNAUTHENTICATED, NULL);
@ -83,27 +82,25 @@ static void cronet_init_client_secure_fullstack(grpc_end2end_test_fixture *f,
stream_engine *cronetEngine) {
fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data;
grpc_arg arg;
arg.key = const_cast<char*>(GRPC_ARG_DISABLE_CLIENT_AUTHORITY_FILTER);
arg.key = const_cast<char *>(GRPC_ARG_DISABLE_CLIENT_AUTHORITY_FILTER);
arg.type = GRPC_ARG_INTEGER;
arg.value.integer = 1;
client_args = grpc_channel_args_copy_and_add(client_args, &arg, 1);
f->client = grpc_cronet_secure_channel_create(cronetEngine, ffd->localaddr,
client_args, NULL);
f->client = grpc_cronet_secure_channel_create(cronetEngine, ffd->localaddr, client_args, NULL);
grpc_channel_args_destroy(client_args);
GPR_ASSERT(f->client != NULL);
}
static void chttp2_init_server_secure_fullstack(
grpc_end2end_test_fixture *f, grpc_channel_args *server_args,
grpc_server_credentials *server_creds) {
static void chttp2_init_server_secure_fullstack(grpc_end2end_test_fixture *f,
grpc_channel_args *server_args,
grpc_server_credentials *server_creds) {
fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data;
if (f->server) {
grpc_server_destroy(f->server);
}
f->server = grpc_server_create(server_args, NULL);
grpc_server_register_completion_queue(f->server, f->cq, NULL);
GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr,
server_creds));
GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr, server_creds));
grpc_server_credentials_release(server_creds);
grpc_server_start(f->server);
}
@ -114,8 +111,8 @@ static void chttp2_tear_down_secure_fullstack(grpc_end2end_test_fixture *f) {
gpr_free(ffd);
}
static void cronet_init_client_simple_ssl_secure_fullstack(
grpc_end2end_test_fixture *f, grpc_channel_args *client_args) {
static void cronet_init_client_simple_ssl_secure_fullstack(grpc_end2end_test_fixture *f,
grpc_channel_args *client_args) {
grpc_core::ExecCtx exec_ctx;
stream_engine *cronetEngine = [Cronet getGlobalEngine];
@ -128,18 +125,16 @@ static int fail_server_auth_check(grpc_channel_args *server_args) {
size_t i;
if (server_args == NULL) return 0;
for (i = 0; i < server_args->num_args; i++) {
if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) ==
0) {
if (strcmp(server_args->args[i].key, FAIL_AUTH_CHECK_SERVER_ARG_NAME) == 0) {
return 1;
}
}
return 0;
}
static void chttp2_init_server_simple_ssl_secure_fullstack(
grpc_end2end_test_fixture *f, grpc_channel_args *server_args) {
grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key,
test_server1_cert};
static void chttp2_init_server_simple_ssl_secure_fullstack(grpc_end2end_test_fixture *f,
grpc_channel_args *server_args) {
grpc_ssl_pem_key_cert_pair pem_cert_key_pair = {test_server1_key, test_server1_cert};
grpc_server_credentials *ssl_creds =
grpc_ssl_server_credentials_create(NULL, &pem_cert_key_pair, 1, 0, NULL);
if (fail_server_auth_check(server_args)) {
@ -189,9 +184,8 @@ static char *roots_filename;
[Cronet setHttp2Enabled:YES];
[Cronet enableTestCertVerifierForTesting];
NSURL *url = [[[NSFileManager defaultManager]
URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSLog(@"Documents directory: %@", url);
[Cronet start];
[Cronet startNetLogToFile:@"cronet_netlog.json" logBytes:YES];

@ -42,8 +42,7 @@
static void drain_cq(grpc_completion_queue *cq) {
grpc_event ev;
do {
ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
NULL);
ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5), NULL);
} while (ev.type != GRPC_QUEUE_SHUTDOWN);
}
@ -64,9 +63,8 @@ static void drain_cq(grpc_completion_queue *cq) {
[Cronet setHttp2Enabled:YES];
[Cronet setSslKeyLogFileName:@"Documents/key"];
[Cronet enableTestCertVerifierForTesting];
NSURL *url = [[[NSFileManager defaultManager]
URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSLog(@"Documents directory: %@", url);
[Cronet start];
[Cronet startNetLogToFile:@"Documents/cronet_netlog.json" logBytes:YES];
@ -88,8 +86,8 @@ void init_ssl(void) {
void cleanup_ssl(void) { EVP_cleanup(); }
int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen,
const unsigned char *in, unsigned int inlen, void *arg) {
int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in,
unsigned int inlen, void *arg) {
// Always select "h2" as the ALPN protocol to be used
*out = (const unsigned char *)"h2";
*outlen = 2;
@ -98,16 +96,14 @@ int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen,
void init_ctx(SSL_CTX *ctx) {
// Install server certificate
BIO *pem = BIO_new_mem_buf((void *)test_server1_cert,
(int)strlen(test_server1_cert));
BIO *pem = BIO_new_mem_buf((void *)test_server1_cert, (int)strlen(test_server1_cert));
X509 *cert = PEM_read_bio_X509_AUX(pem, NULL, NULL, (char *)"");
SSL_CTX_use_certificate(ctx, cert);
X509_free(cert);
BIO_free(pem);
// Install server private key
pem =
BIO_new_mem_buf((void *)test_server1_key, (int)strlen(test_server1_key));
pem = BIO_new_mem_buf((void *)test_server1_key, (int)strlen(test_server1_key));
EVP_PKEY *key = PEM_read_bio_PrivateKey(pem, NULL, NULL, (char *)"");
SSL_CTX_use_PrivateKey(ctx, key);
EVP_PKEY_free(key);
@ -128,7 +124,7 @@ unsigned int parse_h2_length(const char *field) {
grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *args) {
grpc_arg arg;
arg.key = const_cast<char*>(GRPC_ARG_DISABLE_CLIENT_AUTHORITY_FILTER);
arg.key = const_cast<char *>(GRPC_ARG_DISABLE_CLIENT_AUTHORITY_FILTER);
arg.type = GRPC_ARG_INTEGER;
arg.value.integer = 1;
return grpc_channel_args_copy_and_add(args, &arg, 1);
@ -136,10 +132,8 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
- (void)testInternalError {
grpc_call *c;
grpc_slice request_payload_slice =
grpc_slice_from_copied_string("hello world");
grpc_byte_buffer *request_payload =
grpc_raw_byte_buffer_create(&request_payload_slice, 1);
grpc_slice request_payload_slice = grpc_slice_from_copied_string("hello world");
grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"),
grpc_slice_from_static_string("val1"),
@ -156,8 +150,7 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
stream_engine *cronetEngine = [Cronet getGlobalEngine];
grpc_channel_args *client_args = add_disable_client_authority_filter_args(NULL);
grpc_channel *client =
grpc_cronet_secure_channel_create(cronetEngine, addr, client_args, NULL);
grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr, client_args, NULL);
grpc_channel_args_destroy(client_args);
cq_verifier *cqv = cq_verifier_create(cq);
@ -173,8 +166,7 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
grpc_slice details;
c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
grpc_slice_from_static_string("/foo"), NULL,
deadline, NULL);
grpc_slice_from_static_string("/foo"), NULL, deadline, NULL);
GPR_ASSERT(c);
grpc_metadata_array_init(&initial_metadata_recv);
@ -231,16 +223,15 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), (void *)1, NULL);
GPR_ASSERT(GRPC_CALL_OK == error);
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int s = accept(sl, NULL, NULL);
GPR_ASSERT(s >= 0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int s = accept(sl, NULL, NULL);
GPR_ASSERT(s >= 0);
// Close the connection after 1 second to trigger Cronet's on_failed()
sleep(1);
close(s);
close(sl);
});
// Close the connection after 1 second to trigger Cronet's on_failed()
sleep(1);
close(s);
close(sl);
});
CQ_EXPECT_COMPLETION(cqv, (void *)1, 1);
cq_verify(cqv);
@ -275,10 +266,8 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
args = add_disable_client_authority_filter_args(args);
grpc_call *c;
grpc_slice request_payload_slice =
grpc_slice_from_copied_string("hello world");
grpc_byte_buffer *request_payload =
grpc_raw_byte_buffer_create(&request_payload_slice, 1);
grpc_slice request_payload_slice = grpc_slice_from_copied_string("hello world");
grpc_byte_buffer *request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
grpc_metadata meta_c[2] = {{grpc_slice_from_static_string("key1"),
grpc_slice_from_static_string("val1"),
@ -294,8 +283,7 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
gpr_join_host_port(&addr, "127.0.0.1", port);
grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
stream_engine *cronetEngine = [Cronet getGlobalEngine];
grpc_channel *client =
grpc_cronet_secure_channel_create(cronetEngine, addr, args, NULL);
grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr, args, NULL);
cq_verifier *cqv = cq_verifier_create(cq);
grpc_op ops[6];
@ -310,8 +298,7 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
grpc_slice details;
c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
grpc_slice_from_static_string("/foo"), NULL,
deadline, NULL);
grpc_slice_from_static_string("/foo"), NULL, deadline, NULL);
GPR_ASSERT(c);
grpc_metadata_array_init(&initial_metadata_recv);
@ -319,8 +306,7 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
grpc_metadata_array_init(&request_metadata_recv);
grpc_call_details_init(&call_details);
__weak XCTestExpectation *expectation =
[self expectationWithDescription:@"Coalescing"];
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Coalescing"];
int sl = socket(AF_INET, SOCK_STREAM, 0);
GPR_ASSERT(sl >= 0);
@ -332,61 +318,60 @@ grpc_channel_args *add_disable_client_authority_filter_args(grpc_channel_args *a
GPR_ASSERT(0 == bind(sl, (struct sockaddr *)&s_addr, sizeof(s_addr)));
GPR_ASSERT(0 == listen(sl, 5));
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int s = accept(sl, NULL, NULL);
GPR_ASSERT(s >= 0);
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
// Make an TLS endpoint to receive Cronet's transmission
SSL_CTX *ctx = SSL_CTX_new(TLSv1_2_server_method());
init_ctx(ctx);
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, s);
SSL_accept(ssl);
const char magic[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
char buf[4096];
long len;
BOOL coalesced = NO;
while ((len = SSL_read(ssl, buf, sizeof(buf))) > 0) {
gpr_log(GPR_DEBUG, "Read len: %ld", len);
// Analyze the HTTP/2 frames in the same TLS PDU to identify if
// coalescing is successful
unsigned int p = 0;
while (p < len) {
if (len - p >= 24 && 0 == memcmp(&buf[p], magic, 24)) {
p += 24;
continue;
}
if (buf[p + 3] == 0 && // Type is DATA
parse_h2_length(&buf[p]) == 0x10 && // Length is correct
(buf[p + 4] & 1) != 0 && // EOS bit is set
0 == memcmp("hello world", &buf[p + 14],
11)) { // Message is correct
coalesced = YES;
break;
}
p += (parse_h2_length(&buf[p]) + 9);
}
if (coalesced) {
break;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int s = accept(sl, NULL, NULL);
GPR_ASSERT(s >= 0);
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
// Make an TLS endpoint to receive Cronet's transmission
SSL_CTX *ctx = SSL_CTX_new(TLSv1_2_server_method());
init_ctx(ctx);
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, s);
SSL_accept(ssl);
const char magic[] = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
char buf[4096];
long len;
BOOL coalesced = NO;
while ((len = SSL_read(ssl, buf, sizeof(buf))) > 0) {
gpr_log(GPR_DEBUG, "Read len: %ld", len);
// Analyze the HTTP/2 frames in the same TLS PDU to identify if
// coalescing is successful
unsigned int p = 0;
while (p < len) {
if (len - p >= 24 && 0 == memcmp(&buf[p], magic, 24)) {
p += 24;
continue;
}
XCTAssert(coalesced == useCoalescing);
SSL_free(ssl);
SSL_CTX_free(ctx);
close(s);
close(sl);
[expectation fulfill];
});
if (buf[p + 3] == 0 && // Type is DATA
parse_h2_length(&buf[p]) == 0x10 && // Length is correct
(buf[p + 4] & 1) != 0 && // EOS bit is set
0 == memcmp("hello world", &buf[p + 14],
11)) { // Message is correct
coalesced = YES;
break;
}
p += (parse_h2_length(&buf[p]) + 9);
}
if (coalesced) {
break;
}
}
XCTAssert(coalesced == useCoalescing);
SSL_free(ssl);
SSL_CTX_free(ctx);
close(s);
close(sl);
[expectation fulfill];
});
memset(ops, 0, sizeof(ops));
op = ops;

@ -20,16 +20,16 @@
#import <XCTest/XCTest.h>
#import <grpc/grpc.h>
#import <GRPCClient/GRPCCall.h>
#import <GRPCClient/GRPCCall+ChannelArg.h>
#import <GRPCClient/GRPCCall+OAuth2.h>
#import <GRPCClient/GRPCCall+Tests.h>
#import <GRPCClient/GRPCCall.h>
#import <GRPCClient/internal_testing/GRPCCall+InternalTests.h>
#import <ProtoRPC/ProtoMethod.h>
#import <RemoteTest/Messages.pbobjc.h>
#import <RxLibrary/GRXBufferedPipe.h>
#import <RxLibrary/GRXWriteable.h>
#import <RxLibrary/GRXWriter+Immediate.h>
#import <RxLibrary/GRXBufferedPipe.h>
#include <netinet/in.h>
@ -37,10 +37,10 @@
#define TEST_TIMEOUT 16
static NSString * const kHostAddress = @"localhost:5050";
static NSString * const kPackage = @"grpc.testing";
static NSString * const kService = @"TestService";
static NSString * const kRemoteSSLHost = @"grpc-test.sandbox.googleapis.com";
static NSString *const kHostAddress = @"localhost:5050";
static NSString *const kPackage = @"grpc.testing";
static NSString *const kService = @"TestService";
static NSString *const kRemoteSSLHost = @"grpc-test.sandbox.googleapis.com";
static GRPCProtoMethod *kInexistentMethod;
static GRPCProtoMethod *kEmptyCallMethod;
@ -49,15 +49,17 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
/** Observer class for testing that responseMetadata is KVO-compliant */
@interface PassthroughObserver : NSObject
- (instancetype) initWithCallback:(void (^)(NSString*, id, NSDictionary*))callback
- (instancetype)initWithCallback:(void (^)(NSString *, id, NSDictionary *))callback
NS_DESIGNATED_INITIALIZER;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context;
@end
@implementation PassthroughObserver {
void (^_callback)(NSString*, id, NSDictionary*);
void (^_callback)(NSString *, id, NSDictionary *);
}
- (instancetype)init {
@ -84,7 +86,7 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
@end
# pragma mark Tests
#pragma mark Tests
/**
* A few tests similar to InteropTests, but which use the generic gRPC client (GRPCCall) rather than
@ -108,18 +110,14 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
[GRPCCall useInsecureConnectionsForHost:kHostAddress];
// This method isn't implemented by the remote server.
kInexistentMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage
service:kService
method:@"Inexistent"];
kEmptyCallMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage
service:kService
method:@"EmptyCall"];
kUnaryCallMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage
service:kService
method:@"UnaryCall"];
kFullDuplexCallMethod = [[GRPCProtoMethod alloc] initWithPackage:kPackage
service:kService
method:@"FullDuplexCall"];
kInexistentMethod =
[[GRPCProtoMethod alloc] initWithPackage:kPackage service:kService method:@"Inexistent"];
kEmptyCallMethod =
[[GRPCProtoMethod alloc] initWithPackage:kPackage service:kService method:@"EmptyCall"];
kUnaryCallMethod =
[[GRPCProtoMethod alloc] initWithPackage:kPackage service:kService method:@"UnaryCall"];
kFullDuplexCallMethod =
[[GRPCProtoMethod alloc] initWithPackage:kPackage service:kService method:@"FullDuplexCall"];
}
- (void)testConnectionToRemoteServer {
@ -129,13 +127,15 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
path:kInexistentMethod.HTTPPath
requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTFail(@"Received unexpected response: %@", value);
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil, @"Finished without error!");
XCTAssertEqual(errorOrNil.code, 12, @"Finished with unexpected error: %@", errorOrNil);
[expectation fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTFail(@"Received unexpected response: %@", value);
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil, @"Finished without error!");
XCTAssertEqual(errorOrNil.code, 12, @"Finished with unexpected error: %@", errorOrNil);
[expectation fulfill];
}];
[call startWithWriteable:responsesWriteable];
@ -143,21 +143,24 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
}
- (void)testEmptyRPC {
__weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *response =
[self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
path:kEmptyCallMethod.HTTPPath
requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
[response fulfill];
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
[response fulfill];
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
[call startWithWriteable:responsesWriteable];
@ -178,18 +181,20 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
path:kUnaryCallMethod.HTTPPath
requestsWriter:requestsWriter];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertGreaterThan(value.length, 0, @"Empty response received.");
RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL];
// We expect empty strings, not nil:
XCTAssertNotNil(responseProto.username, @"Response's username is nil.");
XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil.");
[response fulfill];
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertGreaterThan(value.length, 0, @"Empty response received.");
RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL];
// We expect empty strings, not nil:
XCTAssertNotNil(responseProto.username, @"Response's username is nil.");
XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil.");
[response fulfill];
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
[call startWithWriteable:responsesWriteable];
@ -210,20 +215,22 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
call.oauth2AccessToken = @"bogusToken";
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTFail(@"Received unexpected response: %@", value);
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil, @"Finished without error!");
XCTAssertEqual(errorOrNil.code, 16, @"Finished with unexpected error: %@", errorOrNil);
XCTAssertEqualObjects(call.responseHeaders, errorOrNil.userInfo[kGRPCHeadersKey],
@"Headers in the NSError object and call object differ.");
XCTAssertEqualObjects(call.responseTrailers, errorOrNil.userInfo[kGRPCTrailersKey],
@"Trailers in the NSError object and call object differ.");
NSString *challengeHeader = call.oauth2ChallengeHeader;
XCTAssertGreaterThan(challengeHeader.length, 0,
@"No challenge in response headers %@", call.responseHeaders);
[expectation fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTFail(@"Received unexpected response: %@", value);
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil, @"Finished without error!");
XCTAssertEqual(errorOrNil.code, 16, @"Finished with unexpected error: %@", errorOrNil);
XCTAssertEqualObjects(call.responseHeaders, errorOrNil.userInfo[kGRPCHeadersKey],
@"Headers in the NSError object and call object differ.");
XCTAssertEqualObjects(call.responseTrailers, errorOrNil.userInfo[kGRPCTrailersKey],
@"Trailers in the NSError object and call object differ.");
NSString *challengeHeader = call.oauth2ChallengeHeader;
XCTAssertGreaterThan(challengeHeader.length, 0, @"No challenge in response headers %@",
call.responseHeaders);
[expectation fulfill];
}];
[call startWithWriteable:responsesWriteable];
@ -231,38 +238,43 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
}
- (void)testResponseMetadataKVO {
__weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *response =
[self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
__weak XCTestExpectation *metadata = [self expectationWithDescription:@"Metadata changed."];
GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
path:kEmptyCallMethod.HTTPPath
requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
PassthroughObserver *observer = [[PassthroughObserver alloc] initWithCallback:^(NSString *keypath, id object, NSDictionary * change) {
if ([keypath isEqual: @"responseHeaders"]) {
[metadata fulfill];
}
}];
PassthroughObserver *observer = [[PassthroughObserver alloc]
initWithCallback:^(NSString *keypath, id object, NSDictionary *change) {
if ([keypath isEqual:@"responseHeaders"]) {
[metadata fulfill];
}
}];
[call addObserver:observer forKeyPath:@"responseHeaders" options:0 context:NULL];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
[response fulfill];
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
[response fulfill];
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
[call startWithWriteable:responsesWriteable];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
- (void)testUserAgentPrefix {
__weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *response =
[self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
@ -272,46 +284,45 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
// user-agent value, which we confirm.
call.requestHeaders[@"x-grpc-test-echo-useragent"] = @"";
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
NSString *userAgent = call.responseHeaders[@"x-grpc-test-echo-useragent"];
NSError *error = nil;
// Test the regex is correct
NSString *expectedUserAgent = @"Foo grpc-objc/";
expectedUserAgent =
[expectedUserAgent stringByAppendingString:GRPC_OBJC_VERSION_STRING];
expectedUserAgent =
[expectedUserAgent stringByAppendingString:@" grpc-c/"];
expectedUserAgent =
[expectedUserAgent stringByAppendingString:GRPC_C_VERSION_STRING];
expectedUserAgent =
[expectedUserAgent stringByAppendingString:@" (ios; chttp2; "];
expectedUserAgent =
[expectedUserAgent stringByAppendingString:[NSString stringWithUTF8String:grpc_g_stands_for()]];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@")"];
XCTAssertEqualObjects(userAgent, expectedUserAgent);
// Change in format of user-agent field in a direction that does not match the regex will likely
// cause problem for certain gRPC users. For details, refer to internal doc https://goo.gl/c2diBc
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@" grpc-[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?/[^ ,]+( \\([^)]*\\))?"
options:0
error:&error];
NSString *customUserAgent =
[regex stringByReplacingMatchesInString:userAgent
options:0
range:NSMakeRange(0, [userAgent length])
withTemplate:@""];
XCTAssertEqualObjects(customUserAgent, @"Foo");
[response fulfill];
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
NSString *userAgent = call.responseHeaders[@"x-grpc-test-echo-useragent"];
NSError *error = nil;
// Test the regex is correct
NSString *expectedUserAgent = @"Foo grpc-objc/";
expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_OBJC_VERSION_STRING];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@" grpc-c/"];
expectedUserAgent = [expectedUserAgent stringByAppendingString:GRPC_C_VERSION_STRING];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@" (ios; chttp2; "];
expectedUserAgent = [expectedUserAgent
stringByAppendingString:[NSString stringWithUTF8String:grpc_g_stands_for()]];
expectedUserAgent = [expectedUserAgent stringByAppendingString:@")"];
XCTAssertEqualObjects(userAgent, expectedUserAgent);
// Change in format of user-agent field in a direction that does not match the regex will
// likely cause problem for certain gRPC users. For details, refer to internal doc
// https://goo.gl/c2diBc
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@" grpc-[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?/[^ ,]+( \\([^)]*\\))?"
options:0
error:&error];
NSString *customUserAgent =
[regex stringByReplacingMatchesInString:userAgent
options:0
range:NSMakeRange(0, [userAgent length])
withTemplate:@""];
XCTAssertEqualObjects(customUserAgent, @"Foo");
[response fulfill];
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
[call startWithWriteable:responsesWriteable];
@ -319,7 +330,8 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
}
- (void)testTrailers {
__weak XCTestExpectation *response = [self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *response =
[self expectationWithDescription:@"Empty response received."];
__weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
@ -327,21 +339,22 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
// Setting this special key in the header will cause the interop server to echo back the
// trailer data.
const unsigned char raw_bytes[] = {1,2,3,4};
const unsigned char raw_bytes[] = {1, 2, 3, 4};
NSData *trailer_data = [NSData dataWithBytes:raw_bytes length:sizeof(raw_bytes)];
call.requestHeaders[@"x-grpc-test-echo-trailing-bin"] = trailer_data;
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
[response fulfill];
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
XCTAssertEqualObjects((NSData *)call.responseTrailers[@"x-grpc-test-echo-trailing-bin"],
trailer_data,
@"Did not receive expected trailer");
[completion fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertEqual([value length], 0, @"Non-empty response received: %@", value);
[response fulfill];
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
XCTAssertEqualObjects((NSData *)call.responseTrailers[@"x-grpc-test-echo-trailing-bin"],
trailer_data, @"Did not receive expected trailer");
[completion fulfill];
}];
[call startWithWriteable:responsesWriteable];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
@ -351,15 +364,12 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
- (void)testExceptions {
// Try to set parameters to nil for GRPCCall. This should cause an exception
@try {
(void)[[GRPCCall alloc] initWithHost:nil
path:nil
requestsWriter:nil];
(void)[[GRPCCall alloc] initWithHost:nil path:nil requestsWriter:nil];
XCTFail(@"Did not receive an exception when parameters are nil");
} @catch(NSException *theException) {
} @catch (NSException *theException) {
NSLog(@"Received exception as expected: %@", theException.name);
}
// Set state to Finished by force
GRXWriter *requestsWriter = [GRXWriter emptyWriter];
[requestsWriter finishWithError:nil];
@ -368,10 +378,9 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
path:kUnaryCallMethod.HTTPPath
requestsWriter:requestsWriter];
XCTFail(@"Did not receive an exception when GRXWriter has incorrect state.");
} @catch(NSException *theException) {
} @catch (NSException *theException) {
NSLog(@"Received exception as expected: %@", theException.name);
}
}
- (void)testIdempotentProtoRPC {
@ -387,20 +396,24 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
GRPCCall *call = [[GRPCCall alloc] initWithHost:kHostAddress
path:kUnaryCallMethod.HTTPPath
requestsWriter:requestsWriter];
[GRPCCall setCallSafety:GRPCCallSafetyIdempotentRequest host:kHostAddress path:kUnaryCallMethod.HTTPPath];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertGreaterThan(value.length, 0, @"Empty response received.");
RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL];
// We expect empty strings, not nil:
XCTAssertNotNil(responseProto.username, @"Response's username is nil.");
XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil.");
[response fulfill];
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
[GRPCCall setCallSafety:GRPCCallSafetyIdempotentRequest
host:kHostAddress
path:kUnaryCallMethod.HTTPPath];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssertNotNil(value, @"nil value received as response.");
XCTAssertGreaterThan(value.length, 0, @"Empty response received.");
RMTSimpleResponse *responseProto = [RMTSimpleResponse parseFromData:value error:NULL];
// We expect empty strings, not nil:
XCTAssertNotNil(responseProto.username, @"Response's username is nil.");
XCTAssertNotNil(responseProto.oauthScope, @"Response's OAuth scope is nil.");
[response fulfill];
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNil(errorOrNil, @"Finished with unexpected error: %@", errorOrNil);
[completion fulfill];
}];
[call startWithWriteable:responsesWriteable];
@ -412,10 +425,12 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
RMTSimpleRequest *request = [RMTSimpleRequest message];
request.responseSize = kPayloadSize;
__weak XCTestExpectation *expectation1 = [self expectationWithDescription:@"AlternateDispatchQueue1"];
__weak XCTestExpectation *expectation1 =
[self expectationWithDescription:@"AlternateDispatchQueue1"];
// Use default (main) dispatch queue
NSString *main_queue_label = [NSString stringWithUTF8String:dispatch_queue_get_label(dispatch_get_main_queue())];
NSString *main_queue_label =
[NSString stringWithUTF8String:dispatch_queue_get_label(dispatch_get_main_queue())];
GRXWriter *requestsWriter1 = [GRXWriter writerWithValue:[request data]];
@ -423,20 +438,24 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
path:kUnaryCallMethod.HTTPPath
requestsWriter:requestsWriter1];
id<GRXWriteable> responsesWriteable1 = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
NSString *label = [NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)];
XCTAssert([label isEqualToString:main_queue_label]);
id<GRXWriteable> responsesWriteable1 =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
NSString *label =
[NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)];
XCTAssert([label isEqualToString:main_queue_label]);
[expectation1 fulfill];
} completionHandler:^(NSError *errorOrNil) {
}];
[expectation1 fulfill];
}
completionHandler:^(NSError *errorOrNil){
}];
[call1 startWithWriteable:responsesWriteable1];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
// Use a custom queue
__weak XCTestExpectation *expectation2 = [self expectationWithDescription:@"AlternateDispatchQueue2"];
__weak XCTestExpectation *expectation2 =
[self expectationWithDescription:@"AlternateDispatchQueue2"];
NSString *queue_label = @"test.queue1";
dispatch_queue_t queue = dispatch_queue_create([queue_label UTF8String], DISPATCH_QUEUE_SERIAL);
@ -449,13 +468,16 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
[call2 setResponseDispatchQueue:queue];
id<GRXWriteable> responsesWriteable2 = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
NSString *label = [NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)];
XCTAssert([label isEqualToString:queue_label]);
id<GRXWriteable> responsesWriteable2 =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
NSString *label =
[NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)];
XCTAssert([label isEqualToString:queue_label]);
[expectation2 fulfill];
} completionHandler:^(NSError *errorOrNil) {
}];
[expectation2 fulfill];
}
completionHandler:^(NSError *errorOrNil){
}];
[call2 startWithWriteable:responsesWriteable2];
@ -470,13 +492,16 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
path:kFullDuplexCallMethod.HTTPPath
requestsWriter:pipe];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssert(0, @"Failure: response received; Expect: no response received.");
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil, @"Failure: no error received; Expect: receive deadline exceeded.");
XCTAssertEqual(errorOrNil.code, GRPCErrorCodeDeadlineExceeded);
[completion fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
XCTAssert(0, @"Failure: response received; Expect: no response received.");
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil,
@"Failure: no error received; Expect: receive deadline exceeded.");
XCTAssertEqual(errorOrNil.code, GRPCErrorCodeDeadlineExceeded);
[completion fulfill];
}];
call.timeout = 0.001;
[call startWithWriteable:responsesWriteable];
@ -490,8 +515,8 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
int fd = socket(AF_INET, SOCK_STREAM, 0);
XCTAssertEqual(bind(fd, (struct sockaddr*)&addr, sizeof(addr)), 0);
XCTAssertEqual(getsockname(fd, (struct sockaddr*)&addr, &addr_len), 0);
XCTAssertEqual(bind(fd, (struct sockaddr *)&addr, sizeof(addr)), 0);
XCTAssertEqual(getsockname(fd, (struct sockaddr *)&addr, &addr_len), 0);
XCTAssertEqual(addr_len, sizeof(addr));
close(fd);
return addr.sin_port;
@ -499,21 +524,23 @@ static GRPCProtoMethod *kFullDuplexCallMethod;
- (void)testErrorCode {
int port = [self findFreePort];
NSString * const kDummyAddress = [NSString stringWithFormat:@"localhost:%d", port];
NSString *const kDummyAddress = [NSString stringWithFormat:@"localhost:%d", port];
__weak XCTestExpectation *completion = [self expectationWithDescription:@"Empty RPC completed."];
GRPCCall *call = [[GRPCCall alloc] initWithHost:kDummyAddress
path:kEmptyCallMethod.HTTPPath
requestsWriter:[GRXWriter writerWithValue:[NSData data]]];
id<GRXWriteable> responsesWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
// Should not reach here
XCTAssert(NO);
} completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil, @"Finished with no error");
XCTAssertEqual(errorOrNil.code, GRPC_STATUS_UNAVAILABLE);
[completion fulfill];
}];
id<GRXWriteable> responsesWriteable =
[[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
// Should not reach here
XCTAssert(NO);
}
completionHandler:^(NSError *errorOrNil) {
XCTAssertNotNil(errorOrNil, @"Finished with no error");
XCTAssertEqual(errorOrNil.code, GRPC_STATUS_UNAVAILABLE);
[completion fulfill];
}];
[call startWithWriteable:responsesWriteable];

@ -22,17 +22,17 @@
#import <Cronet/Cronet.h>
#import <GRPCClient/GRPCCall+ChannelArg.h>
#import <GRPCClient/GRPCCall+Cronet.h>
#import <GRPCClient/GRPCCall+Tests.h>
#import <GRPCClient/internal_testing/GRPCCall+InternalTests.h>
#import <GRPCClient/GRPCCall+Cronet.h>
#import <ProtoRPC/ProtoRPC.h>
#import <RemoteTest/Messages.pbobjc.h>
#import <RemoteTest/Test.pbobjc.h>
#import <RemoteTest/Test.pbrpc.h>
#import <RxLibrary/GRXBufferedPipe.h>
#import <RxLibrary/GRXWriter+Immediate.h>
#import <grpc/support/log.h>
#import <grpc/grpc.h>
#import <grpc/support/log.h>
#define TEST_TIMEOUT 32
@ -61,7 +61,7 @@
@implementation RMTStreamingOutputCallResponse (Constructors)
+ (instancetype)messageWithPayloadSize:(NSNumber *)payloadSize {
RMTStreamingOutputCallResponse * response = [self message];
RMTStreamingOutputCallResponse *response = [self message];
response.payload.type = RMTPayloadType_Compressable;
response.payload.body = [NSMutableData dataWithLength:payloadSize.unsignedIntegerValue];
return response;
@ -113,14 +113,15 @@ BOOL isRemoteInteropTest(NSString *host) {
GPBEmpty *request = [GPBEmpty message];
[_service emptyCallWithRequest:request handler:^(GPBEmpty *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
[_service emptyCallWithRequest:request
handler:^(GPBEmpty *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
id expectedResponse = [GPBEmpty message];
XCTAssertEqualObjects(response, expectedResponse);
id expectedResponse = [GPBEmpty message];
XCTAssertEqualObjects(response, expectedResponse);
[expectation fulfill];
}];
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -134,16 +135,17 @@ BOOL isRemoteInteropTest(NSString *host) {
request.responseSize = 314159;
request.payload.body = [NSMutableData dataWithLength:271828];
[_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
[_service unaryCallWithRequest:request
handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message];
expectedResponse.payload.type = RMTPayloadType_Compressable;
expectedResponse.payload.body = [NSMutableData dataWithLength:314159];
XCTAssertEqualObjects(response, expectedResponse);
RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message];
expectedResponse.payload.type = RMTPayloadType_Compressable;
expectedResponse.payload.body = [NSMutableData dataWithLength:314159];
XCTAssertEqualObjects(response, expectedResponse);
[expectation fulfill];
}];
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -158,29 +160,30 @@ BOOL isRemoteInteropTest(NSString *host) {
request.payload.body = [NSMutableData dataWithLength:10];
[GRPCCall enableOpBatchLog:YES];
[_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message];
expectedResponse.payload.type = RMTPayloadType_Compressable;
expectedResponse.payload.body = [NSMutableData dataWithLength:10];
XCTAssertEqualObjects(response, expectedResponse);
// The test is a success if there is a batch of exactly 3 ops (SEND_INITIAL_METADATA,
// SEND_MESSAGE, SEND_CLOSE_FROM_CLIENT). Without packet coalescing each batch of ops contains
// only one op.
NSArray *opBatches = [GRPCCall obtainAndCleanOpBatchLog];
const NSInteger kExpectedOpBatchSize = 3;
for (NSObject *o in opBatches) {
if ([o isKindOfClass:[NSArray class]]) {
NSArray *batch = (NSArray *)o;
if ([batch count] == kExpectedOpBatchSize) {
[expectation fulfill];
break;
}
}
}
}];
[_service unaryCallWithRequest:request
handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message];
expectedResponse.payload.type = RMTPayloadType_Compressable;
expectedResponse.payload.body = [NSMutableData dataWithLength:10];
XCTAssertEqualObjects(response, expectedResponse);
// The test is a success if there is a batch of exactly 3 ops
// (SEND_INITIAL_METADATA, SEND_MESSAGE, SEND_CLOSE_FROM_CLIENT). Without
// packet coalescing each batch of ops contains only one op.
NSArray *opBatches = [GRPCCall obtainAndCleanOpBatchLog];
const NSInteger kExpectedOpBatchSize = 3;
for (NSObject *o in opBatches) {
if ([o isKindOfClass:[NSArray class]]) {
NSArray *batch = (NSArray *)o;
if ([batch count] == kExpectedOpBatchSize) {
[expectation fulfill];
break;
}
}
}
}];
[self waitForExpectationsWithTimeout:16 handler:nil];
[GRPCCall enableOpBatchLog:NO];
@ -191,14 +194,15 @@ BOOL isRemoteInteropTest(NSString *host) {
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"MaxResponseSize"];
RMTSimpleRequest *request = [RMTSimpleRequest message];
const int32_t kPayloadSize = 4 * 1024 * 1024 - self.encodingOverhead; // 4MB - encoding overhead
const int32_t kPayloadSize = 4 * 1024 * 1024 - self.encodingOverhead; // 4MB - encoding overhead
request.responseSize = kPayloadSize;
[_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertEqual(response.payload.body.length, kPayloadSize);
[expectation fulfill];
}];
[_service unaryCallWithRequest:request
handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertEqual(response.payload.body.length, kPayloadSize);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -208,18 +212,23 @@ BOOL isRemoteInteropTest(NSString *host) {
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"ResponseOverMaxSize"];
RMTSimpleRequest *request = [RMTSimpleRequest message];
const int32_t kPayloadSize = 4 * 1024 * 1024 - self.encodingOverhead + 1; // 1B over max size
const int32_t kPayloadSize = 4 * 1024 * 1024 - self.encodingOverhead + 1; // 1B over max size
request.responseSize = kPayloadSize;
[_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) {
// TODO(jcanizales): Catch the error and rethrow it with an actionable message:
// - Use +[GRPCCall setResponseSizeLimit:forHost:] to set a higher limit.
// - If you're developing the server, consider using response streaming, or let clients filter
// responses by setting a google.protobuf.FieldMask in the request:
// https://github.com/google/protobuf/blob/master/src/google/protobuf/field_mask.proto
XCTAssertEqualObjects(error.localizedDescription, @"Received message larger than max (4194305 vs. 4194304)");
[expectation fulfill];
}];
[_service unaryCallWithRequest:request
handler:^(RMTSimpleResponse *response, NSError *error) {
// TODO(jcanizales): Catch the error and rethrow it with an actionable
// message:
// - Use +[GRPCCall setResponseSizeLimit:forHost:] to set a higher limit.
// - If you're developing the server, consider using response streaming,
// or let clients filter
// responses by setting a google.protobuf.FieldMask in the request:
// https://github.com/google/protobuf/blob/master/src/google/protobuf/field_mask.proto
XCTAssertEqualObjects(
error.localizedDescription,
@"Received message larger than max (4194305 vs. 4194304)");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -230,16 +239,17 @@ BOOL isRemoteInteropTest(NSString *host) {
[self expectationWithDescription:@"HigherResponseSizeLimit"];
RMTSimpleRequest *request = [RMTSimpleRequest message];
const size_t kPayloadSize = 5 * 1024 * 1024; // 5MB
const size_t kPayloadSize = 5 * 1024 * 1024; // 5MB
request.responseSize = kPayloadSize;
[GRPCCall setResponseSizeLimit:6 * 1024 * 1024 forHost:self.class.host];
[_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertEqual(response.payload.body.length, kPayloadSize);
[expectation fulfill];
}];
[_service unaryCallWithRequest:request
handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertEqual(response.payload.body.length, kPayloadSize);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -260,19 +270,21 @@ BOOL isRemoteInteropTest(NSString *host) {
RMTStreamingInputCallRequest *request4 = [RMTStreamingInputCallRequest message];
request4.payload.body = [NSMutableData dataWithLength:45904];
GRXWriter *writer = [GRXWriter writerWithContainer:@[request1, request2, request3, request4]];
GRXWriter *writer = [GRXWriter writerWithContainer:@[ request1, request2, request3, request4 ]];
[_service streamingInputCallWithRequestsWriter:writer
handler:^(RMTStreamingInputCallResponse *response,
NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertNil(
error, @"Finished with unexpected error: %@", error);
RMTStreamingInputCallResponse *expectedResponse = [RMTStreamingInputCallResponse message];
expectedResponse.aggregatedPayloadSize = 74922;
XCTAssertEqualObjects(response, expectedResponse);
RMTStreamingInputCallResponse *expectedResponse =
[RMTStreamingInputCallResponse message];
expectedResponse.aggregatedPayloadSize = 74922;
XCTAssertEqualObjects(response, expectedResponse);
[expectation fulfill];
}];
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -281,7 +293,7 @@ BOOL isRemoteInteropTest(NSString *host) {
XCTAssertNotNil(self.class.host);
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"ServerStreaming"];
NSArray *expectedSizes = @[@31415, @9, @2653, @58979];
NSArray *expectedSizes = @[ @31415, @9, @2653, @58979 ];
RMTStreamingOutputCallRequest *request = [RMTStreamingOutputCallRequest message];
for (NSNumber *size in expectedSizes) {
@ -291,25 +303,27 @@ BOOL isRemoteInteropTest(NSString *host) {
}
__block int index = 0;
[_service streamingOutputCallWithRequest:request
eventHandler:^(BOOL done,
RMTStreamingOutputCallResponse *response,
NSError *error){
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertTrue(done || response, @"Event handler called without an event.");
if (response) {
XCTAssertLessThan(index, 4, @"More than 4 responses received.");
id expected = [RMTStreamingOutputCallResponse messageWithPayloadSize:expectedSizes[index]];
XCTAssertEqualObjects(response, expected);
index += 1;
}
if (done) {
XCTAssertEqual(index, 4, @"Received %i responses instead of 4.", index);
[expectation fulfill];
}
}];
[_service
streamingOutputCallWithRequest:request
eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response,
NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertTrue(done || response,
@"Event handler called without an event.");
if (response) {
XCTAssertLessThan(index, 4, @"More than 4 responses received.");
id expected = [RMTStreamingOutputCallResponse
messageWithPayloadSize:expectedSizes[index]];
XCTAssertEqualObjects(response, expected);
index += 1;
}
if (done) {
XCTAssertEqual(index, 4, @"Received %i responses instead of 4.", index);
[expectation fulfill];
}
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -318,8 +332,8 @@ BOOL isRemoteInteropTest(NSString *host) {
XCTAssertNotNil(self.class.host);
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"PingPong"];
NSArray *requests = @[@27182, @8, @1828, @45904];
NSArray *responses = @[@31415, @9, @2653, @58979];
NSArray *requests = @[ @27182, @8, @1828, @45904 ];
NSArray *responses = @[ @31415, @9, @2653, @58979 ];
GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init];
@ -330,31 +344,34 @@ BOOL isRemoteInteropTest(NSString *host) {
[requestsBuffer writeValue:request];
[_service fullDuplexCallWithRequestsWriter:requestsBuffer
eventHandler:^(BOOL done,
RMTStreamingOutputCallResponse *response,
eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response,
NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertTrue(done || response, @"Event handler called without an event.");
if (response) {
XCTAssertLessThan(index, 4, @"More than 4 responses received.");
id expected = [RMTStreamingOutputCallResponse messageWithPayloadSize:responses[index]];
XCTAssertEqualObjects(response, expected);
index += 1;
if (index < 4) {
id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index]
requestedResponseSize:responses[index]];
[requestsBuffer writeValue:request];
} else {
[requestsBuffer writesFinishedWithError:nil];
}
}
if (done) {
XCTAssertEqual(index, 4, @"Received %i responses instead of 4.", index);
[expectation fulfill];
}
}];
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertTrue(done || response,
@"Event handler called without an event.");
if (response) {
XCTAssertLessThan(index, 4, @"More than 4 responses received.");
id expected = [RMTStreamingOutputCallResponse
messageWithPayloadSize:responses[index]];
XCTAssertEqualObjects(response, expected);
index += 1;
if (index < 4) {
id request = [RMTStreamingOutputCallRequest
messageWithPayloadSize:requests[index]
requestedResponseSize:responses[index]];
[requestsBuffer writeValue:request];
} else {
[requestsBuffer writesFinishedWithError:nil];
}
}
if (done) {
XCTAssertEqual(index, 4, @"Received %i responses instead of 4.",
index);
[expectation fulfill];
}
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -362,13 +379,12 @@ BOOL isRemoteInteropTest(NSString *host) {
XCTAssertNotNil(self.class.host);
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"EmptyStream"];
[_service fullDuplexCallWithRequestsWriter:[GRXWriter emptyWriter]
eventHandler:^(BOOL done,
RMTStreamingOutputCallResponse *response,
eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response,
NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssert(done, @"Unexpected response: %@", response);
[expectation fulfill];
}];
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssert(done, @"Unexpected response: %@", response);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -379,13 +395,13 @@ BOOL isRemoteInteropTest(NSString *host) {
// A buffered pipe to which we never write any value acts as a writer that just hangs.
GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init];
GRPCProtoCall *call =
[_service RPCToStreamingInputCallWithRequestsWriter:requestsBuffer
handler:^(RMTStreamingInputCallResponse *response,
NSError *error) {
XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED);
[expectation fulfill];
}];
GRPCProtoCall *call = [_service
RPCToStreamingInputCallWithRequestsWriter:requestsBuffer
handler:^(RMTStreamingInputCallResponse *response,
NSError *error) {
XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED);
[expectation fulfill];
}];
XCTAssertEqual(call.state, GRXWriterStateNotStarted);
[call start];
@ -399,35 +415,36 @@ BOOL isRemoteInteropTest(NSString *host) {
- (void)testCancelAfterFirstResponseRPC {
XCTAssertNotNil(self.class.host);
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"CancelAfterFirstResponse"];
__weak XCTestExpectation *expectation =
[self expectationWithDescription:@"CancelAfterFirstResponse"];
// A buffered pipe to which we write a single value but never close
GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init];
__block BOOL receivedResponse = NO;
id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:@21782
requestedResponseSize:@31415];
id request =
[RMTStreamingOutputCallRequest messageWithPayloadSize:@21782 requestedResponseSize:@31415];
[requestsBuffer writeValue:request];
__block GRPCProtoCall *call =
[_service RPCToFullDuplexCallWithRequestsWriter:requestsBuffer
eventHandler:^(BOOL done,
RMTStreamingOutputCallResponse *response,
NSError *error) {
if (receivedResponse) {
XCTAssert(done, @"Unexpected extra response %@", response);
XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED);
[expectation fulfill];
} else {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertFalse(done, @"Finished without response");
XCTAssertNotNil(response);
receivedResponse = YES;
[call cancel];
}
}];
__block GRPCProtoCall *call = [_service
RPCToFullDuplexCallWithRequestsWriter:requestsBuffer
eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response,
NSError *error) {
if (receivedResponse) {
XCTAssert(done, @"Unexpected extra response %@", response);
XCTAssertEqual(error.code, GRPC_STATUS_CANCELLED);
[expectation fulfill];
} else {
XCTAssertNil(error, @"Finished with unexpected error: %@",
error);
XCTAssertFalse(done, @"Finished without response");
XCTAssertNotNil(response);
receivedResponse = YES;
[call cancel];
}
}];
[call start];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -439,19 +456,25 @@ BOOL isRemoteInteropTest(NSString *host) {
GPBEmpty *request = [GPBEmpty message];
[_service emptyCallWithRequest:request handler:^(GPBEmpty *response, NSError *error) {
XCTAssertNil(error, @"First RPC finished with unexpected error: %@", error);
[_service
emptyCallWithRequest:request
handler:^(GPBEmpty *response, NSError *error) {
XCTAssertNil(error, @"First RPC finished with unexpected error: %@", error);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[GRPCCall closeOpenConnections];
[GRPCCall closeOpenConnections];
#pragma clang diagnostic pop
[_service emptyCallWithRequest:request handler:^(GPBEmpty *response, NSError *error) {
XCTAssertNil(error, @"Second RPC finished with unexpected error: %@", error);
[expectation fulfill];
}];
}];
[_service
emptyCallWithRequest:request
handler:^(GPBEmpty *response, NSError *error) {
XCTAssertNil(
error, @"Second RPC finished with unexpected error: %@",
error);
[expectation fulfill];
}];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -472,16 +495,17 @@ BOOL isRemoteInteropTest(NSString *host) {
request.expectCompressed.value = YES;
[GRPCCall setDefaultCompressMethod:GRPCCompressGzip forhost:self.class.host];
[_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
[_service unaryCallWithRequest:request
handler:^(RMTSimpleResponse *response, NSError *error) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message];
expectedResponse.payload.type = RMTPayloadType_Compressable;
expectedResponse.payload.body = [NSMutableData dataWithLength:314159];
XCTAssertEqualObjects(response, expectedResponse);
RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message];
expectedResponse.payload.type = RMTPayloadType_Compressable;
expectedResponse.payload.body = [NSMutableData dataWithLength:314159];
XCTAssertEqualObjects(response, expectedResponse);
[expectation fulfill];
}];
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}
@ -493,8 +517,8 @@ BOOL isRemoteInteropTest(NSString *host) {
[GRPCCall setKeepaliveWithInterval:1500 timeout:0 forHost:self.class.host];
NSArray *requests = @[@27182, @8];
NSArray *responses = @[@31415, @9];
NSArray *requests = @[ @27182, @8 ];
NSArray *responses = @[ @31415, @9 ];
GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init];
@ -504,25 +528,26 @@ BOOL isRemoteInteropTest(NSString *host) {
requestedResponseSize:responses[index]];
[requestsBuffer writeValue:request];
[_service fullDuplexCallWithRequestsWriter:requestsBuffer
eventHandler:^(BOOL done,
RMTStreamingOutputCallResponse *response,
NSError *error) {
if (index == 0) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertTrue(response, @"Event handler called without an event.");
XCTAssertFalse(done);
index++;
} else {
// Keepalive should kick after 1s elapsed and fails the call.
XCTAssertNotNil(error);
XCTAssertEqual(error.code, GRPC_STATUS_INTERNAL);
XCTAssertEqualObjects(error.localizedDescription, @"keepalive watchdog timeout",
@"Unexpected failure that is not keepalive watchdog timeout.");
XCTAssertTrue(done);
[expectation fulfill];
}
}];
[_service
fullDuplexCallWithRequestsWriter:requestsBuffer
eventHandler:^(BOOL done, RMTStreamingOutputCallResponse *response,
NSError *error) {
if (index == 0) {
XCTAssertNil(error, @"Finished with unexpected error: %@", error);
XCTAssertTrue(response, @"Event handler called without an event.");
XCTAssertFalse(done);
index++;
} else {
// Keepalive should kick after 1s elapsed and fails the call.
XCTAssertNotNil(error);
XCTAssertEqual(error.code, GRPC_STATUS_INTERNAL);
XCTAssertEqualObjects(
error.localizedDescription, @"keepalive watchdog timeout",
@"Unexpected failure that is not keepalive watchdog timeout.");
XCTAssertTrue(done);
[expectation fulfill];
}
}];
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
}

@ -25,7 +25,7 @@
// in turn derived from environment variable of the same name.
#define NSStringize_helper(x) #x
#define NSStringize(x) @NSStringize_helper(x)
static NSString * const kLocalCleartextHost = NSStringize(HOST_PORT_LOCAL);
static NSString *const kLocalCleartextHost = NSStringize(HOST_PORT_LOCAL);
// The Protocol Buffers encoding overhead of local interop server. Acquired
// by experiment. Adjust this when server's proto file changes.
@ -42,7 +42,7 @@ static int32_t kLocalInteropServerOverhead = 10;
}
- (int32_t)encodingOverhead {
return kLocalInteropServerOverhead; // bytes
return kLocalInteropServerOverhead; // bytes
}
- (void)setUp {

@ -24,7 +24,7 @@
// in turn derived from environment variable of the same name.
#define NSStringize_helper(x) #x
#define NSStringize(x) @NSStringize_helper(x)
static NSString * const kLocalSSLHost = NSStringize(HOST_PORT_LOCALSSL);
static NSString *const kLocalSSLHost = NSStringize(HOST_PORT_LOCALSSL);
// The Protocol Buffers encoding overhead of local interop server. Acquired
// by experiment. Adjust this when server's proto file changes.
@ -41,7 +41,7 @@ static int32_t kLocalInteropServerOverhead = 10;
}
- (int32_t)encodingOverhead {
return kLocalInteropServerOverhead; // bytes
return kLocalInteropServerOverhead; // bytes
}
- (void)setUp {
@ -49,8 +49,8 @@ static int32_t kLocalInteropServerOverhead = 10;
// Register test server certificates and name.
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSString *certsPath = [bundle pathForResource:@"TestCertificates.bundle/test-certificates"
ofType:@"pem"];
NSString *certsPath =
[bundle pathForResource:@"TestCertificates.bundle/test-certificates" ofType:@"pem"];
[GRPCCall useTestCertsPath:certsPath testName:@"foo.test.google.fr" forHost:kLocalSSLHost];
}
@ -60,7 +60,7 @@ static int32_t kLocalInteropServerOverhead = 10;
@try {
[GRPCCall useTestCertsPath:nil testName:nil forHost:nil];
XCTFail(@"Did not receive an exception when parameters are nil");
} @catch(NSException *theException) {
} @catch (NSException *theException) {
NSLog(@"Received exception as expected: %@", theException.name);
}
}

@ -25,7 +25,7 @@
// in turn derived from environment variable of the same name.
#define NSStringize_helper(x) #x
#define NSStringize(x) @NSStringize_helper(x)
static NSString * const kRemoteSSLHost = NSStringize(HOST_PORT_REMOTE);
static NSString *const kRemoteSSLHost = NSStringize(HOST_PORT_REMOTE);
// The Protocol Buffers encoding overhead of remote interop server. Acquired
// by experiment. Adjust this when server's proto file changes.
@ -42,7 +42,7 @@ static int32_t kRemoteInteropServerOverhead = 12;
}
- (int32_t)encodingOverhead {
return kRemoteInteropServerOverhead; // bytes
return kRemoteInteropServerOverhead; // bytes
}
@end

@ -28,8 +28,7 @@
// in turn derived from environment variable of the same name.
#define NSStringize_helper(x) #x
#define NSStringize(x) @NSStringize_helper(x)
static NSString * const kRemoteSSLHost = NSStringize(HOST_PORT_REMOTE);
static NSString *const kRemoteSSLHost = NSStringize(HOST_PORT_REMOTE);
// The Protocol Buffers encoding overhead of remote interop server. Acquired
// by experiment. Adjust this when server's proto file changes.
@ -46,7 +45,7 @@ static int32_t kRemoteInteropServerOverhead = 12;
}
- (int32_t)encodingOverhead {
return kRemoteInteropServerOverhead; // bytes
return kRemoteInteropServerOverhead; // bytes
}
@end

@ -30,10 +30,10 @@
//
// TODO(jcanizales): Move this to a test util library, and add tests for it.
@interface CapturingSingleValueHandler : NSObject
@property (nonatomic, readonly) void (^block)(id value, NSError *errorOrNil);
@property (nonatomic, readonly) NSUInteger timesCalled;
@property (nonatomic, readonly) id value;
@property (nonatomic, readonly) NSError *errorOrNil;
@property(nonatomic, readonly) void (^block)(id value, NSError *errorOrNil);
@property(nonatomic, readonly) NSUInteger timesCalled;
@property(nonatomic, readonly) id value;
@property(nonatomic, readonly) NSError *errorOrNil;
+ (instancetype)handler;
@end
@ -149,10 +149,11 @@
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
// Given:
CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
id<GRXWriteable> writeable =
[GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
id anyValue = @7;
@ -167,17 +168,17 @@
XCTAssertEqual(handler.timesCalled, 1);
XCTAssertEqualObjects(handler.value, anyValue);
XCTAssertEqualObjects(handler.errorOrNil, nil);
}
- (void)testBufferedPipePropagatesError {
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
// Given:
CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
id<GRXWriteable> writeable =
[GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
// If:
@ -196,10 +197,11 @@
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
// Given:
CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
id<GRXWriteable> writeable =
[GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
id anyValue = @7;
// If:
@ -220,8 +222,9 @@
#define WRITE_ROUNDS (1000)
- (void)testBufferedPipeResumeWhenDealloc {
id anyValue = @7;
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
}];
id<GRXWriteable> writeable =
[GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil){
}];
// Release after alloc;
GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];

@ -22,6 +22,5 @@
// instead. This file can be regenerated from the template by running
// `tools/buildgen/generate_projects.sh`.
#define GRPC_OBJC_VERSION_STRING @"1.12.0-dev"
#define GRPC_C_VERSION_STRING @"6.0.0-dev"

@ -20,6 +20,8 @@
#include <sstream>
#include <grpc/grpc.h>
#include <grpc/support/time.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
@ -29,8 +31,6 @@
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>
#include <grpcpp/support/slice.h>
#include <grpc/grpc.h>
#include <grpc/support/time.h>
#include "src/core/lib/gprpp/thd.h"
#include "test/core/util/port.h"
@ -55,8 +55,7 @@ static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) {
cursor = GRPC_SLICE_START_PTR(out);
for (i = 0; i < nslices; i++) {
memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
GRPC_SLICE_LENGTH(slices[i]));
memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]), GRPC_SLICE_LENGTH(slices[i]));
cursor += GRPC_SLICE_LENGTH(slices[i]);
}
@ -74,16 +73,14 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
}
grpc_slice a = merge_slices(c_slices, slices.size());
grpc_slice b = grpc_slice_from_copied_string(str);
res =
(GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b)) &&
(0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
GRPC_SLICE_LENGTH(a)));
res = (GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b)) &&
(0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b), GRPC_SLICE_LENGTH(a)));
grpc_slice_unref(a);
grpc_slice_unref(b);
for (int i = 0; i < slices.size(); i++) {
grpc_slice_unref(c_slices[i]);
}
delete [] c_slices;
delete[] c_slices;
return res;
}
@ -102,9 +99,7 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
std::ostringstream server_address_;
}
- (void)verify_ok:(grpc::CompletionQueue*)cq
i:(int)i
expect_ok:(bool)expect_ok {
- (void)verify_ok:(grpc::CompletionQueue*)cq i:(int)i expect_ok:(bool)expect_ok {
bool ok;
void* got_tag;
XCTAssertTrue(cq->Next(&got_tag, &ok));
@ -112,10 +107,18 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
XCTAssertEqual(tag(i), got_tag);
}
- (void)server_ok:(int)i { [self verify_ok:srv_cq_.get() i:i expect_ok:true]; }
- (void)client_ok:(int)i { [self verify_ok:&cli_cq_ i:i expect_ok:true]; }
- (void)server_fail:(int)i { [self verify_ok:srv_cq_.get() i:i expect_ok:false]; }
- (void)client_fail:(int)i { [self verify_ok:&cli_cq_ i:i expect_ok:false]; }
- (void)server_ok:(int)i {
[self verify_ok:srv_cq_.get() i:i expect_ok:true];
}
- (void)client_ok:(int)i {
[self verify_ok:&cli_cq_ i:i expect_ok:true];
}
- (void)server_fail:(int)i {
[self verify_ok:srv_cq_.get() i:i expect_ok:false];
}
- (void)client_fail:(int)i {
[self verify_ok:&cli_cq_ i:i expect_ok:false];
}
- (void)setUp {
[super setUp];
@ -125,8 +128,7 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
server_address_ << server_host_ << ":" << port;
// Setup server
ServerBuilder builder;
builder.AddListeningPort(server_address_.str(),
InsecureServerCredentials());
builder.AddListeningPort(server_address_.str(), InsecureServerCredentials());
builder.RegisterAsyncGenericService(&generic_service_);
// Include a second call to RegisterAsyncGenericService to make sure that
// we get an error in the log, since it is not allowed to have 2 async
@ -137,14 +139,17 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// Put teardown code here. This method is called after the invocation of each test method in the
// class.
server_->Shutdown();
void* ignored_tag;
bool ignored_ok;
cli_cq_.Shutdown();
srv_cq_->Shutdown();
while (cli_cq_.Next(&ignored_tag, &ignored_ok));
while (srv_cq_->Next(&ignored_tag, &ignored_ok));
while (cli_cq_.Next(&ignored_tag, &ignored_ok))
;
while (srv_cq_->Next(&ignored_tag, &ignored_ok))
;
[super tearDown];
}
@ -156,11 +161,9 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
- (void)SendRpc:(int)num_rpcs {
[self SendRpc:num_rpcs check_deadline:false deadline:gpr_inf_future(GPR_CLOCK_MONOTONIC)];
}
}
- (void)SendRpc:(int)num_rpcs
check_deadline:(bool)check_deadline
deadline:(gpr_timespec)deadline {
- (void)SendRpc:(int)num_rpcs check_deadline:(bool)check_deadline deadline:(gpr_timespec)deadline {
const grpc::string kMethodName("/grpc.cpp.test.util.EchoTestService/Echo");
for (int i = 0; i < num_rpcs; i++) {
Status recv_status;
@ -177,7 +180,7 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
}
std::unique_ptr<GenericClientAsyncReaderWriter> call =
generic_stub_->Call(&cli_ctx, kMethodName, &cli_cq_, tag(1));
generic_stub_->Call(&cli_ctx, kMethodName, &cli_cq_, tag(1));
[self client_ok:1];
Slice send_slice = Slice("hello world", 11);
std::unique_ptr<ByteBuffer> send_buffer =
@ -189,8 +192,7 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
call->WritesDone(tag(3));
[self client_ok:3];
generic_service_.RequestCall(&srv_ctx, &stream, srv_cq_.get(),
srv_cq_.get(), tag(4));
generic_service_.RequestCall(&srv_ctx, &stream, srv_cq_.get(), srv_cq_.get(), tag(4));
[self verify_ok:srv_cq_.get() i:4 expect_ok:true];
XCTAssertEqual(server_host_, srv_ctx.host().substr(0, server_host_.length()));
@ -198,7 +200,7 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
if (check_deadline) {
XCTAssertTrue(gpr_time_similar(deadline, srv_ctx.raw_deadline(),
gpr_time_from_millis(1000, GPR_TIMESPAN)));
gpr_time_from_millis(1000, GPR_TIMESPAN)));
}
ByteBuffer recv_buffer;
@ -241,4 +243,3 @@ int byte_buffer_eq_string(ByteBuffer* bb, const char* str) {
}
@end

@ -18,7 +18,7 @@
// Hack TEST macro of gTest and make they conform XCTest style. We only
// need test name (b), not test case name (a).
#define TEST(a,b) - (void)test ## b
#define TEST(a, b) -(void)test##b
#define ASSERT_TRUE XCTAssert
#define ASSERT_EQ XCTAssertEqual
@ -38,12 +38,10 @@ const char key2[] = "metadata-key2";
const char val1[] = "metadata-val1";
const char val2[] = "metadata-val2";
bool ClientMetadataContains(const grpc::ServerContext& context,
const grpc::string_ref& key,
bool ClientMetadataContains(const grpc::ServerContext& context, const grpc::string_ref& key,
const grpc::string_ref& value) {
const auto& client_metadata = context.client_metadata();
for (auto iter = client_metadata.begin(); iter != client_metadata.end();
++iter) {
for (auto iter = client_metadata.begin(); iter != client_metadata.end(); ++iter) {
if (iter->first == key && iter->second == value) {
return true;
}

Loading…
Cancel
Save