From 8772a6a542ad07f06ef4048a8e06240a3a612aa0 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 5 Jan 2016 15:29:23 -0800 Subject: [PATCH] Wait for JSExecutor to tear down in RCTBridgeTests Reviewed By: tadeuzagallo Differential Revision: D2803092 fb-gh-sync-id: 71ccac2c13221bfbcb2f09a14d48ac2d2901d04e --- .../UIExplorerUnitTests/RCTBridgeTests.m | 42 ++++++++++++------- Libraries/RCTTest/RCTTestRunner.m | 3 +- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m b/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m index fe2f48c5c..d428fb441 100644 --- a/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m +++ b/Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m @@ -21,6 +21,16 @@ #import "RCTJavaScriptExecutor.h" #import "RCTUtils.h" + +#define RUN_RUNLOOP_WHILE(CONDITION) \ +_Pragma("clang diagnostic push") \ +_Pragma("clang diagnostic ignored \"-Wshadow\"") \ +NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:0.1]; \ +while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \ +[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \ +} \ +_Pragma("clang diagnostic pop") + @interface TestExecutor : NSObject @property (nonatomic, readonly, copy) NSMutableDictionary *injectedStuff; @@ -93,6 +103,8 @@ RCT_EXPORT_MODULE() @interface RCTBridgeTests : XCTestCase { RCTBridge *_bridge; + __weak TestExecutor *_jsExecutor; + BOOL _testMethodCalled; } @end @@ -112,35 +124,34 @@ RCT_EXPORT_MODULE(TestModule) launchOptions:nil]; _bridge.executorClass = [TestExecutor class]; + // Force to recreate the executor with the new class // - reload: doesn't work here since bridge hasn't loaded yet. [_bridge invalidate]; [_bridge setUp]; + + _jsExecutor = [_bridge.batchedBridge valueForKey:@"javaScriptExecutor"]; + XCTAssertNotNil(_jsExecutor); } - (void)tearDown { [super tearDown]; - [_bridge invalidate]; _testMethodCalled = NO; -} -#define RUN_RUNLOOP_WHILE(CONDITION) \ -_Pragma("clang diagnostic push") \ -_Pragma("clang diagnostic ignored \"-Wshadow\"") \ -NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:0.1]; \ -while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \ - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \ -} \ -_Pragma("clang diagnostic pop") + [_bridge invalidate]; + _bridge = nil; + + RUN_RUNLOOP_WHILE(_jsExecutor != nil); + XCTAssertNotNil(_jsExecutor); +} - (void)testHookRegistration { - TestExecutor *executor = [_bridge.batchedBridge valueForKey:@"_javaScriptExecutor"]; - NSString *injectedStuff; - RUN_RUNLOOP_WHILE(!(injectedStuff = executor.injectedStuff[@"__fbBatchedBridgeConfig"])); + RUN_RUNLOOP_WHILE(!(injectedStuff = _jsExecutor.injectedStuff[@"__fbBatchedBridgeConfig"])); + XCTAssertNotNil(injectedStuff); __block NSNumber *testModuleID = nil; __block NSDictionary *testConstants = nil; @@ -165,10 +176,9 @@ _Pragma("clang diagnostic pop") - (void)testCallNativeMethod { - TestExecutor *executor = [_bridge.batchedBridge valueForKey:@"_javaScriptExecutor"]; - NSString *injectedStuff; - RUN_RUNLOOP_WHILE(!(injectedStuff = executor.injectedStuff[@"__fbBatchedBridgeConfig"])); + RUN_RUNLOOP_WHILE(!(injectedStuff = _jsExecutor.injectedStuff[@"__fbBatchedBridgeConfig"])); + XCTAssertNotNil(injectedStuff); __block NSNumber *testModuleID = nil; __block NSNumber *testMethodID = nil; diff --git a/Libraries/RCTTest/RCTTestRunner.m b/Libraries/RCTTest/RCTTestRunner.m index 46a3a2abe..6203d0f1d 100644 --- a/Libraries/RCTTest/RCTTestRunner.m +++ b/Libraries/RCTTest/RCTTestRunner.m @@ -16,6 +16,7 @@ #import "RCTTestModule.h" #import "RCTUtils.h" #import "RCTJSCExecutor.h" +#import "RCTBridge+Private.h" static const NSTimeInterval kTestTimeoutSeconds = 60; static const NSTimeInterval kTestTeardownTimeoutSeconds = 30; @@ -134,7 +135,7 @@ expectErrorBlock:(BOOL(^)(NSString *error))expectErrorBlock // Take a weak reference to the JS context, so we track its deallocation later // (we can only do this now, since it's been lazily initialized) - id jsExecutor = [bridge valueForKeyPath:@"batchedBridge.javaScriptExecutor"]; + id jsExecutor = [bridge.batchedBridge valueForKey:@"javaScriptExecutor"]; if ([jsExecutor isKindOfClass:[RCTJSCExecutor class]]) { weakJSContext = [jsExecutor valueForKey:@"_context"]; }