diff --git a/src/objective-c/GRPCClient/GRPCCall+Tests.h b/src/objective-c/GRPCClient/GRPCCall+Tests.h index 184ad09c5c8..f1618afdbf4 100644 --- a/src/objective-c/GRPCClient/GRPCCall+Tests.h +++ b/src/objective-c/GRPCClient/GRPCCall+Tests.h @@ -63,4 +63,19 @@ * cache. */ + (void)resetHostSettings; + +/** + * Enables logging of op batches. Memory consumption increases as more ops are logged. + */ ++ (void)enableOpBatchLog:(BOOL)enabled; + +/** + * Add an op batch to log. + */ ++ (void)addOpBatchToLog:(NSArray *)batch; + +/** + * Obtain the logged op batches. Invoking this method will clean the log. + */ ++ (NSArray *)obtainAndCleanOpBatchLog; @end diff --git a/src/objective-c/GRPCClient/GRPCCall+Tests.m b/src/objective-c/GRPCClient/GRPCCall+Tests.m index 656cba8fec6..fe864707e0a 100644 --- a/src/objective-c/GRPCClient/GRPCCall+Tests.m +++ b/src/objective-c/GRPCClient/GRPCCall+Tests.m @@ -64,4 +64,41 @@ + (void)resetHostSettings { [GRPCHost resetAllHostSettings]; } + +static NSMutableArray *opBatchLog = nil; + ++ (void)enableOpBatchLog:(BOOL)enabled { + @synchronized (opBatchLog) { + if (enabled) { + if (!opBatchLog) { + opBatchLog = [NSMutableArray array]; + } + } else { + if (opBatchLog) { + opBatchLog = nil; + } + } + } +} + ++ (void)addOpBatchToLog:(NSArray *)batch { + @synchronized (opBatchLog) { + if (opBatchLog) { + [opBatchLog addObject:batch]; + } + } +} + ++ (NSArray *)obtainAndCleanOpBatchLog { + @synchronized (opBatchLog) { + if (opBatchLog) { + NSArray *out = opBatchLog; + opBatchLog = [NSMutableArray array]; + return out; + } else { + return nil; + } + } +} + @end diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m index 38fcae0299d..fd624a716ff 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m @@ -44,6 +44,8 @@ #import "NSData+GRPC.h" #import "NSError+GRPC.h" +#import "GRPCCall+Tests.h" + @implementation GRPCOperation { @protected // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being @@ -271,6 +273,8 @@ } - (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler { + [GRPCCall addOpBatchToLog:operations]; + size_t nops = operations.count; grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op)); size_t i = 0; diff --git a/src/objective-c/tests/InteropTests.m b/src/objective-c/tests/InteropTests.m index 8adf0a6164c..2bd54e17142 100644 --- a/src/objective-c/tests/InteropTests.m +++ b/src/objective-c/tests/InteropTests.m @@ -145,32 +145,7 @@ [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; } -// TODO (mxyan): Do the same test for chttp2 -#ifdef GRPC_COMPILE_WITH_CRONET -#ifdef GRPC_CRONET_WITH_PACKET_COALESCING - -static bool coalesced_message_and_eos; - -static void log_processor(gpr_log_func_args *args) { - unsigned long file_len = strlen(args->file); - const char suffix[] = "call.c"; - const int suffix_len = sizeof(suffix) - 1; - const char nops[] = "nops=3"; - - if (file_len > suffix_len && - 0 == strcmp(suffix, &args->file[file_len - suffix_len]) && - strstr(args->message, nops)) { - fprintf(stderr, "%s, %s\n", args->file, args->message); - coalesced_message_and_eos = true; - } -} - - (void)testPacketCoalescing { - gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG); - grpc_tracer_set_enabled("all", 1); - gpr_set_log_function(log_processor); - coalesced_message_and_eos = false; - XCTAssertNotNil(self.class.host); __weak XCTestExpectation *expectation = [self expectationWithDescription:@"LargeUnary"]; @@ -179,6 +154,7 @@ static void log_processor(gpr_log_func_args *args) { request.responseSize = 10; request.payload.body = [NSMutableData dataWithLength:10]; + [GRPCCall enableOpBatchLog:YES]; [_service unaryCallWithRequest:request handler:^(RMTSimpleResponse *response, NSError *error) { XCTAssertNil(error, @"Finished with unexpected error: %@", error); @@ -187,17 +163,22 @@ static void log_processor(gpr_log_func_args *args) { expectedResponse.payload.body = [NSMutableData dataWithLength:10]; XCTAssertEqualObjects(response, expectedResponse); - XCTAssert(coalesced_message_and_eos); - - [expectation fulfill]; + NSArray *opBatches = [GRPCCall obtainAndCleanOpBatchLog]; + for (NSObject *o in opBatches) { + if ([o isKindOfClass:[NSArray class]]) { + NSArray *batch = (NSArray *)o; + if ([batch count] == 3) { + [expectation fulfill]; + break; + } + } + } }]; [self waitForExpectationsWithTimeout:16 handler:nil]; + [GRPCCall enableOpBatchLog:NO]; } -#endif -#endif - - (void)test4MBResponsesAreAccepted { XCTAssertNotNil(self.class.host); __weak XCTestExpectation *expectation = [self expectationWithDescription:@"MaxResponseSize"];