Browse Source

isolate start: function from proto calls

Muxi Yan 6 years ago
parent
commit
70f34521de

+ 10 - 0
src/objective-c/ProtoRPC/ProtoRPC.h

@@ -76,6 +76,11 @@ NS_ASSUME_NONNULL_BEGIN
                            callOptions:(nullable GRPCCallOptions *)callOptions
                            callOptions:(nullable GRPCCallOptions *)callOptions
                          responseClass:(Class)responseClass NS_DESIGNATED_INITIALIZER;
                          responseClass:(Class)responseClass NS_DESIGNATED_INITIALIZER;
 
 
+/**
+ * Start the call. This function must only be called once for each instance.
+ */
+- (void)start;
+
 /**
 /**
  * Cancel the request of this call at best effort. It attempts to notify the server that the RPC
  * Cancel the request of this call at best effort. It attempts to notify the server that the RPC
  * should be cancelled, and issue closedWithTrailingMetadata:error: callback with error code
  * should be cancelled, and issue closedWithTrailingMetadata:error: callback with error code
@@ -101,6 +106,11 @@ NS_ASSUME_NONNULL_BEGIN
                            callOptions:(nullable GRPCCallOptions *)callOptions
                            callOptions:(nullable GRPCCallOptions *)callOptions
                          responseClass:(Class)responseClass NS_DESIGNATED_INITIALIZER;
                          responseClass:(Class)responseClass NS_DESIGNATED_INITIALIZER;
 
 
+/**
+ * Start the call. This function must only be called once for each instance.
+ */
+- (void)start;
+
 /**
 /**
  * Cancel the request of this call at best effort. It attempts to notify the server that the RPC
  * Cancel the request of this call at best effort. It attempts to notify the server that the RPC
  * should be cancelled, and issue closedWithTrailingMetadata:error: callback with error code
  * should be cancelled, and issue closedWithTrailingMetadata:error: callback with error code

+ 13 - 6
src/objective-c/ProtoRPC/ProtoRPC.m

@@ -47,6 +47,7 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
 
 
 @implementation GRPCUnaryProtoCall {
 @implementation GRPCUnaryProtoCall {
   GRPCStreamingProtoCall *_call;
   GRPCStreamingProtoCall *_call;
+  GPBMessage *_message;
 }
 }
 
 
 - (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions
 - (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions
@@ -54,17 +55,24 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
                        responseHandler:(id<GRPCProtoResponseHandler>)handler
                        responseHandler:(id<GRPCProtoResponseHandler>)handler
                            callOptions:(GRPCCallOptions *)callOptions
                            callOptions:(GRPCCallOptions *)callOptions
                          responseClass:(Class)responseClass {
                          responseClass:(Class)responseClass {
+  NSAssert(message != nil, @"message cannot be empty.");
+  NSAssert(responseClass != nil, @"responseClass cannot be empty.");
   if ((self = [super init])) {
   if ((self = [super init])) {
     _call = [[GRPCStreamingProtoCall alloc] initWithRequestOptions:requestOptions
     _call = [[GRPCStreamingProtoCall alloc] initWithRequestOptions:requestOptions
                                                    responseHandler:handler
                                                    responseHandler:handler
                                                        callOptions:callOptions
                                                        callOptions:callOptions
                                                      responseClass:responseClass];
                                                      responseClass:responseClass];
-    [_call writeMessage:message];
-    [_call finish];
+    _message = [message copy];
   }
   }
   return self;
   return self;
 }
 }
 
 
+- (void)start {
+  [_call start];
+  [_call writeMessage:_message];
+  [_call finish];
+}
+
 - (void)cancel {
 - (void)cancel {
   [_call cancel];
   [_call cancel];
   _call = nil;
   _call = nil;
@@ -120,15 +128,14 @@ static NSError *ErrorForBadProto(id proto, Class expectedClass, NSError *parsing
     }
     }
     dispatch_set_target_queue(_dispatchQueue, handler.dispatchQueue);
     dispatch_set_target_queue(_dispatchQueue, handler.dispatchQueue);
 
 
-    [self start];
+    _call = [[GRPCCall2 alloc] initWithRequestOptions:_requestOptions
+                                      responseHandler:self
+                                          callOptions:_callOptions];
   }
   }
   return self;
   return self;
 }
 }
 
 
 - (void)start {
 - (void)start {
-  _call = [[GRPCCall2 alloc] initWithRequestOptions:_requestOptions
-                                    responseHandler:self
-                                        callOptions:_callOptions];
   [_call start];
   [_call start];
 }
 }
 
 

+ 47 - 6
src/objective-c/tests/InteropTests.m

@@ -197,7 +197,7 @@ BOOL isRemoteInteropTest(NSString *host) {
 
 
 - (void)testEmptyUnaryRPCWithV2API {
 - (void)testEmptyUnaryRPCWithV2API {
   XCTAssertNotNil([[self class] host]);
   XCTAssertNotNil([[self class] host]);
-  __weak XCTestExpectation *expectation = [self expectationWithDescription:@"EmptyUnary"];
+  __weak XCTestExpectation *expectation = [self expectationWithDescription:@"EmptyUnaryWithV2API"];
 
 
   GPBEmpty *request = [GPBEmpty message];
   GPBEmpty *request = [GPBEmpty message];
   GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
   GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
@@ -205,7 +205,7 @@ BOOL isRemoteInteropTest(NSString *host) {
   options.PEMRootCertificates = [[self class] PEMRootCertificates];
   options.PEMRootCertificates = [[self class] PEMRootCertificates];
   options.hostNameOverride = [[self class] hostNameOverride];
   options.hostNameOverride = [[self class] hostNameOverride];
 
 
-  [_service
+  GRPCUnaryProtoCall *call = [_service
       emptyCallWithMessage:request
       emptyCallWithMessage:request
            responseHandler:[[InteropTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil
            responseHandler:[[InteropTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil
                                messageCallback:^(id message) {
                                messageCallback:^(id message) {
@@ -219,6 +219,7 @@ BOOL isRemoteInteropTest(NSString *host) {
                                  XCTAssertNil(error, @"Unexpected error: %@", error);
                                  XCTAssertNil(error, @"Unexpected error: %@", error);
                                }]
                                }]
                callOptions:options];
                callOptions:options];
+  [call start];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
 }
 }
 
 
@@ -246,6 +247,44 @@ BOOL isRemoteInteropTest(NSString *host) {
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
 }
 }
 
 
+- (void)testLargeUnaryRPCWithV2API {
+  XCTAssertNotNil([[self class] host]);
+  __weak XCTestExpectation *expectRecvMessage = [self expectationWithDescription:@"LargeUnaryWithV2API received message"];
+  __weak XCTestExpectation *expectRecvComplete = [self expectationWithDescription:@"LargeUnaryWithV2API received complete"];
+
+  RMTSimpleRequest *request = [RMTSimpleRequest message];
+  request.responseType = RMTPayloadType_Compressable;
+  request.responseSize = 314159;
+  request.payload.body = [NSMutableData dataWithLength:271828];
+
+  GRPCMutableCallOptions *options = [[GRPCMutableCallOptions alloc] init];
+  options.transportType = [[self class] transportType];
+  options.PEMRootCertificates = [[self class] PEMRootCertificates];
+  options.hostNameOverride = [[self class] hostNameOverride];
+
+  GRPCUnaryProtoCall *call = [_service
+                              unaryCallWithMessage:request
+                              responseHandler:[[InteropTestsBlockCallbacks alloc] initWithInitialMetadataCallback:nil
+                                                                                                  messageCallback:^(id message) {
+                                                                                                    XCTAssertNotNil(message);
+                                                                                                    if (message) {
+                                                                                                      RMTSimpleResponse *expectedResponse = [RMTSimpleResponse message];
+                                                                                                      expectedResponse.payload.type = RMTPayloadType_Compressable;
+                                                                                                      expectedResponse.payload.body = [NSMutableData dataWithLength:314159];
+                                                                                                      XCTAssertEqualObjects(message, expectedResponse);
+
+                                                                                                      [expectRecvMessage fulfill];
+                                                                                                    }
+                                                                                                  }
+                                                                                                    closeCallback:^(NSDictionary *trailingMetadata, NSError *error) {
+                                                                                                      XCTAssertNil(error, @"Unexpected error: %@", error);
+                                                                                                      [expectRecvComplete fulfill];
+                                                                                                    }]
+                              callOptions:options];
+  [call start];
+  [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
+}
+
 - (void)testPacketCoalescing {
 - (void)testPacketCoalescing {
   XCTAssertNotNil([[self class] host]);
   XCTAssertNotNil([[self class] host]);
   __weak XCTestExpectation *expectation = [self expectationWithDescription:@"LargeUnary"];
   __weak XCTestExpectation *expectation = [self expectationWithDescription:@"LargeUnary"];
@@ -473,7 +512,7 @@ BOOL isRemoteInteropTest(NSString *host) {
 
 
 - (void)testPingPongRPCWithV2API {
 - (void)testPingPongRPCWithV2API {
   XCTAssertNotNil([[self class] host]);
   XCTAssertNotNil([[self class] host]);
-  __weak XCTestExpectation *expectation = [self expectationWithDescription:@"PingPong"];
+  __weak XCTestExpectation *expectation = [self expectationWithDescription:@"PingPongWithV2API"];
 
 
   NSArray *requests = @[ @27182, @8, @1828, @45904 ];
   NSArray *requests = @[ @27182, @8, @1828, @45904 ];
   NSArray *responses = @[ @31415, @9, @2653, @58979 ];
   NSArray *responses = @[ @31415, @9, @2653, @58979 ];
@@ -517,6 +556,7 @@ BOOL isRemoteInteropTest(NSString *host) {
                                               [expectation fulfill];
                                               [expectation fulfill];
                                             }]
                                             }]
                             callOptions:options];
                             callOptions:options];
+  [call start];
   [call writeMessage:request];
   [call writeMessage:request];
 
 
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
@@ -562,7 +602,7 @@ BOOL isRemoteInteropTest(NSString *host) {
 
 
 - (void)testCancelAfterBeginRPCWithV2API {
 - (void)testCancelAfterBeginRPCWithV2API {
   XCTAssertNotNil([[self class] host]);
   XCTAssertNotNil([[self class] host]);
-  __weak XCTestExpectation *expectation = [self expectationWithDescription:@"CancelAfterBegin"];
+  __weak XCTestExpectation *expectation = [self expectationWithDescription:@"CancelAfterBeginWithV2API"];
 
 
   // A buffered pipe to which we never write any value acts as a writer that just hangs.
   // A buffered pipe to which we never write any value acts as a writer that just hangs.
   __block GRPCStreamingProtoCall *call = [_service
   __block GRPCStreamingProtoCall *call = [_service
@@ -577,6 +617,7 @@ BOOL isRemoteInteropTest(NSString *host) {
                                                   [expectation fulfill];
                                                   [expectation fulfill];
                                                 }]
                                                 }]
                                 callOptions:nil];
                                 callOptions:nil];
+  [call start];
   [call cancel];
   [call cancel];
 
 
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
@@ -650,7 +691,7 @@ BOOL isRemoteInteropTest(NSString *host) {
                                               [completionExpectation fulfill];
                                               [completionExpectation fulfill];
                                             }]
                                             }]
                             callOptions:options];
                             callOptions:options];
-
+  [call start];
   [call writeMessage:request];
   [call writeMessage:request];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
 }
 }
@@ -680,7 +721,7 @@ BOOL isRemoteInteropTest(NSString *host) {
                                                                                [completionExpectation fulfill];
                                                                                [completionExpectation fulfill];
                                                                              }]
                                                                              }]
                                           callOptions:options];
                                           callOptions:options];
-
+  [call start];
   [call writeMessage:request];
   [call writeMessage:request];
   [call cancel];
   [call cancel];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
   [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];