Skip to content

Commit 8772a6a

Browse files
javachefacebook-github-bot-0
authored andcommitted
Wait for JSExecutor to tear down in RCTBridgeTests
Reviewed By: tadeuzagallo Differential Revision: D2803092 fb-gh-sync-id: 71ccac2c13221bfbcb2f09a14d48ac2d2901d04e
1 parent ec76271 commit 8772a6a

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
#import "RCTJavaScriptExecutor.h"
2222
#import "RCTUtils.h"
2323

24+
25+
#define RUN_RUNLOOP_WHILE(CONDITION) \
26+
_Pragma("clang diagnostic push") \
27+
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
28+
NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:0.1]; \
29+
while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \
30+
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \
31+
} \
32+
_Pragma("clang diagnostic pop")
33+
2434
@interface TestExecutor : NSObject <RCTJavaScriptExecutor>
2535

2636
@property (nonatomic, readonly, copy) NSMutableDictionary<NSString *, id> *injectedStuff;
@@ -93,6 +103,8 @@ - (void)invalidate {}
93103
@interface RCTBridgeTests : XCTestCase <RCTBridgeModule>
94104
{
95105
RCTBridge *_bridge;
106+
__weak TestExecutor *_jsExecutor;
107+
96108
BOOL _testMethodCalled;
97109
}
98110
@end
@@ -112,35 +124,34 @@ - (void)setUp
112124
launchOptions:nil];
113125

114126
_bridge.executorClass = [TestExecutor class];
127+
115128
// Force to recreate the executor with the new class
116129
// - reload: doesn't work here since bridge hasn't loaded yet.
117130
[_bridge invalidate];
118131
[_bridge setUp];
132+
133+
_jsExecutor = [_bridge.batchedBridge valueForKey:@"javaScriptExecutor"];
134+
XCTAssertNotNil(_jsExecutor);
119135
}
120136

121137
- (void)tearDown
122138
{
123139
[super tearDown];
124140

125-
[_bridge invalidate];
126141
_testMethodCalled = NO;
127-
}
128142

129-
#define RUN_RUNLOOP_WHILE(CONDITION) \
130-
_Pragma("clang diagnostic push") \
131-
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
132-
NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:0.1]; \
133-
while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \
134-
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \
135-
} \
136-
_Pragma("clang diagnostic pop")
143+
[_bridge invalidate];
144+
_bridge = nil;
145+
146+
RUN_RUNLOOP_WHILE(_jsExecutor != nil);
147+
XCTAssertNotNil(_jsExecutor);
148+
}
137149

138150
- (void)testHookRegistration
139151
{
140-
TestExecutor *executor = [_bridge.batchedBridge valueForKey:@"_javaScriptExecutor"];
141-
142152
NSString *injectedStuff;
143-
RUN_RUNLOOP_WHILE(!(injectedStuff = executor.injectedStuff[@"__fbBatchedBridgeConfig"]));
153+
RUN_RUNLOOP_WHILE(!(injectedStuff = _jsExecutor.injectedStuff[@"__fbBatchedBridgeConfig"]));
154+
XCTAssertNotNil(injectedStuff);
144155

145156
__block NSNumber *testModuleID = nil;
146157
__block NSDictionary<NSString *, id> *testConstants = nil;
@@ -165,10 +176,9 @@ - (void)testHookRegistration
165176

166177
- (void)testCallNativeMethod
167178
{
168-
TestExecutor *executor = [_bridge.batchedBridge valueForKey:@"_javaScriptExecutor"];
169-
170179
NSString *injectedStuff;
171-
RUN_RUNLOOP_WHILE(!(injectedStuff = executor.injectedStuff[@"__fbBatchedBridgeConfig"]));
180+
RUN_RUNLOOP_WHILE(!(injectedStuff = _jsExecutor.injectedStuff[@"__fbBatchedBridgeConfig"]));
181+
XCTAssertNotNil(injectedStuff);
172182

173183
__block NSNumber *testModuleID = nil;
174184
__block NSNumber *testMethodID = nil;

Libraries/RCTTest/RCTTestRunner.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "RCTTestModule.h"
1717
#import "RCTUtils.h"
1818
#import "RCTJSCExecutor.h"
19+
#import "RCTBridge+Private.h"
1920

2021
static const NSTimeInterval kTestTimeoutSeconds = 60;
2122
static const NSTimeInterval kTestTeardownTimeoutSeconds = 30;
@@ -134,7 +135,7 @@ - (void)runTest:(SEL)test module:(NSString *)moduleName
134135

135136
// Take a weak reference to the JS context, so we track its deallocation later
136137
// (we can only do this now, since it's been lazily initialized)
137-
id jsExecutor = [bridge valueForKeyPath:@"batchedBridge.javaScriptExecutor"];
138+
id jsExecutor = [bridge.batchedBridge valueForKey:@"javaScriptExecutor"];
138139
if ([jsExecutor isKindOfClass:[RCTJSCExecutor class]]) {
139140
weakJSContext = [jsExecutor valueForKey:@"_context"];
140141
}

0 commit comments

Comments
 (0)