Improve the performance of React Native tests

This commit is contained in:
Frédéric Sagnes
2015-07-16 09:23:23 -07:00
parent f74efc13ed
commit 36f76e5893
2 changed files with 15 additions and 23 deletions

View File

@@ -19,17 +19,15 @@
#import "RCTContextExecutor.h" #import "RCTContextExecutor.h"
#import "RCTRootView.h" #import "RCTRootView.h"
#define RUN_RUNLOOP_WHILE(CONDITION, TIMEOUT) \ #define RUN_RUNLOOP_WHILE(CONDITION) \
_Pragma("clang diagnostic push") \ _Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \ _Pragma("clang diagnostic ignored \"-Wshadow\"") \
NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:TIMEOUT]; \ NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:0.1]; \
while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \ while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \
} \ } \
_Pragma("clang diagnostic pop") _Pragma("clang diagnostic pop")
#define DEFAULT_TIMEOUT 2
@interface RCTBridge (RCTAllocationTests) @interface RCTBridge (RCTAllocationTests)
@property (nonatomic, weak) RCTBridge *batchedBridge; @property (nonatomic, weak) RCTBridge *batchedBridge;
@@ -83,7 +81,6 @@ RCT_EXPORT_MODULE();
(void)view; (void)view;
} }
sleep(DEFAULT_TIMEOUT);
XCTAssertNil(weakBridge, @"RCTBridge should have been deallocated"); XCTAssertNil(weakBridge, @"RCTBridge should have been deallocated");
} }
@@ -104,8 +101,7 @@ RCT_EXPORT_MODULE();
* Sleep on the main thread to allow js thread deallocations then run the runloop * Sleep on the main thread to allow js thread deallocations then run the runloop
* to allow the module to be deallocated on the main thread * to allow the module to be deallocated on the main thread
*/ */
sleep(1); RUN_RUNLOOP_WHILE(module.isValid)
RUN_RUNLOOP_WHILE(module.isValid, 1)
XCTAssertFalse(module.isValid, @"AllocationTestModule should have been invalidated by the bridge"); XCTAssertFalse(module.isValid, @"AllocationTestModule should have been invalidated by the bridge");
} }
@@ -124,12 +120,7 @@ RCT_EXPORT_MODULE();
(void)bridge; (void)bridge;
} }
/** RUN_RUNLOOP_WHILE(weakModule)
* Sleep on the main thread to allow js thread deallocations then run the runloop
* to allow the module to be deallocated on the main thread
*/
sleep(1);
RUN_RUNLOOP_WHILE(weakModule, 1)
XCTAssertNil(weakModule, @"AllocationTestModule should have been deallocated"); XCTAssertNil(weakModule, @"AllocationTestModule should have been deallocated");
} }
@@ -145,8 +136,7 @@ RCT_EXPORT_MODULE();
(void)bridge; (void)bridge;
} }
RUN_RUNLOOP_WHILE(weakExecutor, 1); RUN_RUNLOOP_WHILE(weakExecutor);
sleep(1);
XCTAssertNil(weakExecutor, @"JavaScriptExecutor should have been released"); XCTAssertNil(weakExecutor, @"JavaScriptExecutor should have been released");
} }
@@ -158,13 +148,12 @@ RCT_EXPORT_MODULE();
moduleProvider:nil moduleProvider:nil
launchOptions:nil]; launchOptions:nil];
id executor = [bridge.batchedBridge valueForKey:@"javaScriptExecutor"]; id executor = [bridge.batchedBridge valueForKey:@"javaScriptExecutor"];
RUN_RUNLOOP_WHILE(!(weakContext = [executor valueForKey:@"context"]), DEFAULT_TIMEOUT); RUN_RUNLOOP_WHILE(!(weakContext = [executor valueForKey:@"context"]));
XCTAssertNotNil(weakContext, @"RCTJavaScriptContext should have been created"); XCTAssertNotNil(weakContext, @"RCTJavaScriptContext should have been created");
(void)bridge; (void)bridge;
} }
RUN_RUNLOOP_WHILE(weakContext, 1); RUN_RUNLOOP_WHILE(weakContext);
sleep(1);
XCTAssertNil(weakContext, @"RCTJavaScriptContext should have been deallocated"); XCTAssertNil(weakContext, @"RCTJavaScriptContext should have been deallocated");
} }
@@ -176,12 +165,11 @@ RCT_EXPORT_MODULE();
__weak id rootContentView; __weak id rootContentView;
@autoreleasepool { @autoreleasepool {
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@""]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@""];
RUN_RUNLOOP_WHILE(!(rootContentView = [rootView valueForKey:@"contentView"]), DEFAULT_TIMEOUT) RUN_RUNLOOP_WHILE(!(rootContentView = [rootView valueForKey:@"contentView"]))
XCTAssertTrue([rootContentView isValid], @"RCTContentView should be valid"); XCTAssertTrue([rootContentView isValid], @"RCTContentView should be valid");
(void)rootView; (void)rootView;
} }
sleep(DEFAULT_TIMEOUT);
XCTAssertFalse([rootContentView isValid], @"RCTContentView should have been invalidated"); XCTAssertFalse([rootContentView isValid], @"RCTContentView should have been invalidated");
} }
@@ -196,8 +184,7 @@ RCT_EXPORT_MODULE();
[bridge reload]; [bridge reload];
} }
// Use RUN_RUNLOOP_WHILE because `batchedBridge` deallocates on the main thread. RUN_RUNLOOP_WHILE(batchedBridge != nil)
RUN_RUNLOOP_WHILE(batchedBridge != nil, DEFAULT_TIMEOUT)
XCTAssertNotNil(bridge, @"RCTBridge should not have been deallocated"); XCTAssertNotNil(bridge, @"RCTBridge should not have been deallocated");
XCTAssertNil(batchedBridge, @"RCTBatchedBridge should have been deallocated"); XCTAssertNil(batchedBridge, @"RCTBatchedBridge should have been deallocated");

View File

@@ -19,6 +19,7 @@
#import "RCTContextExecutor.h" #import "RCTContextExecutor.h"
#import "RCTUtils.h" #import "RCTUtils.h"
#define RUN_PERF_TESTS 0
@interface RCTContextExecutorTests : XCTestCase @interface RCTContextExecutorTests : XCTestCase
@@ -48,6 +49,8 @@
[_executor invalidate]; [_executor invalidate];
} }
#if RUN_PERF_TESTS
static uint64_t _get_time_nanoseconds(void) static uint64_t _get_time_nanoseconds(void)
{ {
static struct mach_timebase_info tb_info = {0, 0}; static struct mach_timebase_info tb_info = {0, 0};
@@ -91,7 +94,7 @@ static uint64_t _get_time_nanoseconds(void)
JSContextGroupRelease(group); JSContextGroupRelease(group);
} }
- (void)MANUALLY_testJavaScriptCallSpeed - (void)testJavaScriptCallSpeed
{ {
/** /**
* Since we almost don't change the RCTContextExecutor logic, and this test is * Since we almost don't change the RCTContextExecutor logic, and this test is
@@ -200,4 +203,6 @@ static uint64_t _get_time_nanoseconds(void)
} }
} }
#endif
@end @end