Skip to content

Commit

Permalink
Wait for JSExecutor to tear down in RCTBridgeTests
Browse files Browse the repository at this point in the history
Reviewed By: tadeuzagallo

Differential Revision: D2803092

fb-gh-sync-id: 71ccac2c13221bfbcb2f09a14d48ac2d2901d04e
  • Loading branch information
javache authored and facebook-github-bot-0 committed Jan 5, 2016
1 parent ec76271 commit 8772a6a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
42 changes: 26 additions & 16 deletions Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 <RCTJavaScriptExecutor>

@property (nonatomic, readonly, copy) NSMutableDictionary<NSString *, id> *injectedStuff;
Expand Down Expand Up @@ -93,6 +103,8 @@ - (void)invalidate {}
@interface RCTBridgeTests : XCTestCase <RCTBridgeModule>
{
RCTBridge *_bridge;
__weak TestExecutor *_jsExecutor;

BOOL _testMethodCalled;
}
@end
Expand All @@ -112,35 +124,34 @@ - (void)setUp
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<NSString *, id> *testConstants = nil;
Expand All @@ -165,10 +176,9 @@ - (void)testHookRegistration

- (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;
Expand Down
3 changes: 2 additions & 1 deletion Libraries/RCTTest/RCTTestRunner.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -134,7 +135,7 @@ - (void)runTest:(SEL)test module:(NSString *)moduleName

// 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"];
}
Expand Down

0 comments on commit 8772a6a

Please sign in to comment.