From d6c98bf82e46daed0841382d59d112a36979fb17 Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Tue, 30 Apr 2019 18:25:06 -0700 Subject: [PATCH] Fix Cronet multiple initialization problem --- .../CronetTests/CoreCronetEnd2EndTests.mm | 10 ++--- .../tests/CronetTests/CronetUnitTests.mm | 13 ++----- .../InteropTestsRemoteWithCronet.m | 4 ++ src/objective-c/tests/EnableCronet.h | 34 +++++++++++++++++ src/objective-c/tests/EnableCronet.m | 38 +++++++++++++++++++ .../tests/InteropTests/InteropTests.h | 5 +++ .../tests/InteropTests/InteropTests.m | 14 ++++--- .../tests/Tests.xcodeproj/project.pbxproj | 8 ++++ .../xcschemes/CronetTests.xcscheme | 5 +++ 9 files changed, 108 insertions(+), 23 deletions(-) create mode 100644 src/objective-c/tests/EnableCronet.h create mode 100644 src/objective-c/tests/EnableCronet.m diff --git a/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm b/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm index 2fac1be3d0e..3a753d4a4a4 100644 --- a/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm +++ b/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm @@ -49,6 +49,8 @@ #import #include +#import "../EnableCronet.h" + typedef struct fullstack_secure_fixture_data { char *localaddr; } fullstack_secure_fixture_data; @@ -176,13 +178,7 @@ static char *roots_filename; grpc_init(); - [Cronet setHttp2Enabled:YES]; - [Cronet enableTestCertVerifierForTesting]; - NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory - inDomains:NSUserDomainMask] lastObject]; - NSLog(@"Documents directory: %@", url); - [Cronet start]; - [Cronet startNetLogToFile:@"cronet_netlog.json" logBytes:YES]; + enableCronet(); } // The tearDown() function is run after all test cases finish running diff --git a/src/objective-c/tests/CronetTests/CronetUnitTests.mm b/src/objective-c/tests/CronetTests/CronetUnitTests.mm index 2a861032caf..b17bc334479 100644 --- a/src/objective-c/tests/CronetTests/CronetUnitTests.mm +++ b/src/objective-c/tests/CronetTests/CronetUnitTests.mm @@ -20,6 +20,8 @@ #import #import +#import "../EnableCronet.h" + #import #import #import @@ -61,16 +63,7 @@ static void drain_cq(grpc_completion_queue *cq) { grpc_test_init(1, argv); grpc_init(); - - [Cronet setHttp2Enabled:YES]; - [Cronet setSslKeyLogFileName:@"Documents/key"]; - [Cronet enableTestCertVerifierForTesting]; - NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory - inDomains:NSUserDomainMask] lastObject]; - NSLog(@"Documents directory: %@", url); - [Cronet start]; - [Cronet startNetLogToFile:@"Documents/cronet_netlog.json" logBytes:YES]; - + enableCronet(); init_ssl(); } diff --git a/src/objective-c/tests/CronetTests/InteropTestsRemoteWithCronet.m b/src/objective-c/tests/CronetTests/InteropTestsRemoteWithCronet.m index 25041ae5eb1..a2a79c46316 100644 --- a/src/objective-c/tests/CronetTests/InteropTestsRemoteWithCronet.m +++ b/src/objective-c/tests/CronetTests/InteropTestsRemoteWithCronet.m @@ -44,6 +44,10 @@ static int32_t kRemoteInteropServerOverhead = 12; return kRemoteSSLHost; } ++ (BOOL)useCronet { + return YES; +} + - (int32_t)encodingOverhead { return kRemoteInteropServerOverhead; // bytes } diff --git a/src/objective-c/tests/EnableCronet.h b/src/objective-c/tests/EnableCronet.h new file mode 100644 index 00000000000..e544e76345d --- /dev/null +++ b/src/objective-c/tests/EnableCronet.h @@ -0,0 +1,34 @@ +/* + * + * Copyright 2019 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifdef GRPC_COMPILE_WITH_CRONET + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Enable Cronet for once. + */ +void enableCronet(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/objective-c/tests/EnableCronet.m b/src/objective-c/tests/EnableCronet.m new file mode 100644 index 00000000000..ff6e4612d88 --- /dev/null +++ b/src/objective-c/tests/EnableCronet.m @@ -0,0 +1,38 @@ +/* + * + * Copyright 2019 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifdef GRPC_COMPILE_WITH_CRONET + +#import +#import "EnableCronet.h" + +void enableCronet(void) { + static dispatch_once_t enableCronet; + dispatch_once(&enableCronet, ^{ + [Cronet setHttp2Enabled:YES]; + [Cronet setSslKeyLogFileName:@"Documents/key"]; + [Cronet enableTestCertVerifierForTesting]; + NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory + inDomains:NSUserDomainMask] lastObject]; + NSLog(@"Documents directory: %@", url); + [Cronet start]; + [Cronet startNetLogToFile:@"cronet_netlog.json" logBytes:YES]; + }); +} + +#endif diff --git a/src/objective-c/tests/InteropTests/InteropTests.h b/src/objective-c/tests/InteropTests/InteropTests.h index 038f24b62e5..cffa90ac497 100644 --- a/src/objective-c/tests/InteropTests/InteropTests.h +++ b/src/objective-c/tests/InteropTests/InteropTests.h @@ -59,4 +59,9 @@ */ + (NSString *)hostNameOverride; +/** + * Whether to use Cronet for all the v1 API tests in the test suite. + */ ++ (BOOL)useCronet; + @end diff --git a/src/objective-c/tests/InteropTests/InteropTests.m b/src/objective-c/tests/InteropTests/InteropTests.m index 260afb83e6f..3204f689c48 100644 --- a/src/objective-c/tests/InteropTests/InteropTests.m +++ b/src/objective-c/tests/InteropTests/InteropTests.m @@ -37,6 +37,7 @@ #import #import "InteropTestsBlockCallbacks.h" +#import "../enableCronet.h" #define TEST_TIMEOUT 32 @@ -107,16 +108,17 @@ BOOL isRemoteInteropTest(NSString *host) { return nil; } ++ (BOOL)useCronet { + return NO; +} + + (void)setUp { NSLog(@"InteropTest Started, class: %@", [[self class] description]); #ifdef GRPC_COMPILE_WITH_CRONET - static dispatch_once_t *enableCronet; - dispatch_once(enableCronet, ^{ - // Cronet setup - [Cronet setHttp2Enabled:YES]; - [Cronet start]; + enableCronet(); + if ([self useCronet]) { [GRPCCall useCronetWithEngine:[Cronet getGlobalEngine]]; - }); + } #endif #ifdef GRPC_CFSTREAM setenv(kCFStreamVarName, "1", 1); diff --git a/src/objective-c/tests/Tests.xcodeproj/project.pbxproj b/src/objective-c/tests/Tests.xcodeproj/project.pbxproj index 1f05899bd60..c226e29d83b 100644 --- a/src/objective-c/tests/Tests.xcodeproj/project.pbxproj +++ b/src/objective-c/tests/Tests.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 5E3F14842278B461007C6D90 /* InteropTestsBlockCallbacks.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E3F14832278B461007C6D90 /* InteropTestsBlockCallbacks.m */; }; 5E3F14852278BF5D007C6D90 /* InteropTestsBlockCallbacks.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E3F14832278B461007C6D90 /* InteropTestsBlockCallbacks.m */; }; 5E3F14862278BFFF007C6D90 /* InteropTestsBlockCallbacks.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E3F14832278B461007C6D90 /* InteropTestsBlockCallbacks.m */; }; + 5E3F148D22792856007C6D90 /* EnableCronet.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E3F1487227918AA007C6D90 /* EnableCronet.m */; }; + 5E3F148E22792AF5007C6D90 /* EnableCronet.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E3F1487227918AA007C6D90 /* EnableCronet.m */; }; 5E7F486422775B37006656AD /* InteropTestsRemoteWithCronet.m in Sources */ = {isa = PBXBuildFile; fileRef = 5EE84BF31D4717E40050C6CC /* InteropTestsRemoteWithCronet.m */; }; 5E7F486522775B41006656AD /* CronetUnitTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5EAD6D261E27047400002378 /* CronetUnitTests.mm */; }; 5E7F486E22778086006656AD /* CoreCronetEnd2EndTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5E7F486D22778086006656AD /* CoreCronetEnd2EndTests.mm */; }; @@ -92,6 +94,8 @@ 5E0282E8215AA697007AC99D /* NSErrorUnitTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NSErrorUnitTests.m; sourceTree = ""; }; 5E3F14822278B42D007C6D90 /* InteropTestsBlockCallbacks.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InteropTestsBlockCallbacks.h; sourceTree = ""; }; 5E3F14832278B461007C6D90 /* InteropTestsBlockCallbacks.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = InteropTestsBlockCallbacks.m; sourceTree = ""; }; + 5E3F1487227918AA007C6D90 /* EnableCronet.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EnableCronet.m; sourceTree = ""; }; + 5E3F148A227918C4007C6D90 /* EnableCronet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EnableCronet.h; sourceTree = ""; }; 5E7F485922775B15006656AD /* CronetTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CronetTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 5E7F486622776AD8006656AD /* Cronet.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cronet.framework; path = Pods/CronetFramework/Cronet.framework; sourceTree = ""; }; 5E7F486D22778086006656AD /* CoreCronetEnd2EndTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CoreCronetEnd2EndTests.mm; sourceTree = ""; }; @@ -392,6 +396,8 @@ 635697C91B14FC11007A7283 /* Tests */ = { isa = PBXGroup; children = ( + 5E3F148A227918C4007C6D90 /* EnableCronet.h */, + 5E3F1487227918AA007C6D90 /* EnableCronet.m */, 5EAFE8271F8EFB87007F2189 /* version.h */, 635697D71B14FC11007A7283 /* Supporting Files */, ); @@ -813,6 +819,7 @@ buildActionMask = 2147483647; files = ( 5E3F14852278BF5D007C6D90 /* InteropTestsBlockCallbacks.m in Sources */, + 5E3F148D22792856007C6D90 /* EnableCronet.m in Sources */, 5E7F486E22778086006656AD /* CoreCronetEnd2EndTests.mm in Sources */, 5E7F488522778A88006656AD /* InteropTests.m in Sources */, 5E7F486422775B37006656AD /* InteropTestsRemoteWithCronet.m in Sources */, @@ -825,6 +832,7 @@ buildActionMask = 2147483647; files = ( 5E3F14842278B461007C6D90 /* InteropTestsBlockCallbacks.m in Sources */, + 5E3F148E22792AF5007C6D90 /* EnableCronet.m in Sources */, 5E7F488922778B04006656AD /* InteropTestsRemote.m in Sources */, 5E7F487922778226006656AD /* InteropTestsMultipleChannels.m in Sources */, 5EA477042273617B000F72FC /* InteropTestsLocalCleartext.m in Sources */, diff --git a/src/objective-c/tests/Tests.xcodeproj/xcshareddata/xcschemes/CronetTests.xcscheme b/src/objective-c/tests/Tests.xcodeproj/xcshareddata/xcschemes/CronetTests.xcscheme index aea349a06cc..ac8cfc971c8 100644 --- a/src/objective-c/tests/Tests.xcodeproj/xcshareddata/xcschemes/CronetTests.xcscheme +++ b/src/objective-c/tests/Tests.xcodeproj/xcshareddata/xcschemes/CronetTests.xcscheme @@ -37,6 +37,11 @@ BlueprintName = "CronetTests" ReferencedContainer = "container:Tests.xcodeproj"> + + + +