Skip to content

Commit 36f76e5

Browse files
committed
Improve the performance of React Native tests
1 parent f74efc1 commit 36f76e5

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

Examples/UIExplorer/UIExplorerUnitTests/RCTAllocationTests.m

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
#import "RCTContextExecutor.h"
2020
#import "RCTRootView.h"
2121

22-
#define RUN_RUNLOOP_WHILE(CONDITION, TIMEOUT) \
22+
#define RUN_RUNLOOP_WHILE(CONDITION) \
2323
_Pragma("clang diagnostic push") \
2424
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
25-
NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:TIMEOUT]; \
25+
NSDate *timeout = [[NSDate date] dateByAddingTimeInterval:0.1]; \
2626
while ((CONDITION) && [timeout timeIntervalSinceNow] > 0) { \
2727
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout]; \
2828
} \
2929
_Pragma("clang diagnostic pop")
3030

31-
#define DEFAULT_TIMEOUT 2
32-
3331
@interface RCTBridge (RCTAllocationTests)
3432

3533
@property (nonatomic, weak) RCTBridge *batchedBridge;
@@ -83,7 +81,6 @@ - (void)testBridgeIsDeallocated
8381
(void)view;
8482
}
8583

86-
sleep(DEFAULT_TIMEOUT);
8784
XCTAssertNil(weakBridge, @"RCTBridge should have been deallocated");
8885
}
8986

@@ -104,8 +101,7 @@ - (void)testModulesAreInvalidated
104101
* Sleep on the main thread to allow js thread deallocations then run the runloop
105102
* to allow the module to be deallocated on the main thread
106103
*/
107-
sleep(1);
108-
RUN_RUNLOOP_WHILE(module.isValid, 1)
104+
RUN_RUNLOOP_WHILE(module.isValid)
109105
XCTAssertFalse(module.isValid, @"AllocationTestModule should have been invalidated by the bridge");
110106
}
111107

@@ -124,12 +120,7 @@ - (void)testModulesAreDeallocated
124120
(void)bridge;
125121
}
126122

127-
/**
128-
* Sleep on the main thread to allow js thread deallocations then run the runloop
129-
* to allow the module to be deallocated on the main thread
130-
*/
131-
sleep(1);
132-
RUN_RUNLOOP_WHILE(weakModule, 1)
123+
RUN_RUNLOOP_WHILE(weakModule)
133124
XCTAssertNil(weakModule, @"AllocationTestModule should have been deallocated");
134125
}
135126

@@ -145,8 +136,7 @@ - (void)testJavaScriptExecutorIsDeallocated
145136
(void)bridge;
146137
}
147138

148-
RUN_RUNLOOP_WHILE(weakExecutor, 1);
149-
sleep(1);
139+
RUN_RUNLOOP_WHILE(weakExecutor);
150140
XCTAssertNil(weakExecutor, @"JavaScriptExecutor should have been released");
151141
}
152142

@@ -158,13 +148,12 @@ - (void)testJavaScriptContextIsDeallocated
158148
moduleProvider:nil
159149
launchOptions:nil];
160150
id executor = [bridge.batchedBridge valueForKey:@"javaScriptExecutor"];
161-
RUN_RUNLOOP_WHILE(!(weakContext = [executor valueForKey:@"context"]), DEFAULT_TIMEOUT);
151+
RUN_RUNLOOP_WHILE(!(weakContext = [executor valueForKey:@"context"]));
162152
XCTAssertNotNil(weakContext, @"RCTJavaScriptContext should have been created");
163153
(void)bridge;
164154
}
165155

166-
RUN_RUNLOOP_WHILE(weakContext, 1);
167-
sleep(1);
156+
RUN_RUNLOOP_WHILE(weakContext);
168157
XCTAssertNil(weakContext, @"RCTJavaScriptContext should have been deallocated");
169158
}
170159

@@ -176,12 +165,11 @@ - (void)testContentViewIsInvalidated
176165
__weak id rootContentView;
177166
@autoreleasepool {
178167
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@""];
179-
RUN_RUNLOOP_WHILE(!(rootContentView = [rootView valueForKey:@"contentView"]), DEFAULT_TIMEOUT)
168+
RUN_RUNLOOP_WHILE(!(rootContentView = [rootView valueForKey:@"contentView"]))
180169
XCTAssertTrue([rootContentView isValid], @"RCTContentView should be valid");
181170
(void)rootView;
182171
}
183172

184-
sleep(DEFAULT_TIMEOUT);
185173
XCTAssertFalse([rootContentView isValid], @"RCTContentView should have been invalidated");
186174
}
187175

@@ -196,8 +184,7 @@ - (void)testUnderlyingBridgeIsDeallocated
196184
[bridge reload];
197185
}
198186

199-
// Use RUN_RUNLOOP_WHILE because `batchedBridge` deallocates on the main thread.
200-
RUN_RUNLOOP_WHILE(batchedBridge != nil, DEFAULT_TIMEOUT)
187+
RUN_RUNLOOP_WHILE(batchedBridge != nil)
201188

202189
XCTAssertNotNil(bridge, @"RCTBridge should not have been deallocated");
203190
XCTAssertNil(batchedBridge, @"RCTBatchedBridge should have been deallocated");

Examples/UIExplorer/UIExplorerUnitTests/RCTContextExecutorTests.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "RCTContextExecutor.h"
2020
#import "RCTUtils.h"
2121

22+
#define RUN_PERF_TESTS 0
2223

2324
@interface RCTContextExecutorTests : XCTestCase
2425

@@ -48,6 +49,8 @@ - (void)testNativeLoggingHookExceptionBehavior
4849
[_executor invalidate];
4950
}
5051

52+
#if RUN_PERF_TESTS
53+
5154
static uint64_t _get_time_nanoseconds(void)
5255
{
5356
static struct mach_timebase_info tb_info = {0, 0};
@@ -91,7 +94,7 @@ - (void)testDeserializationPerf
9194
JSContextGroupRelease(group);
9295
}
9396

94-
- (void)MANUALLY_testJavaScriptCallSpeed
97+
- (void)testJavaScriptCallSpeed
9598
{
9699
/**
97100
* Since we almost don't change the RCTContextExecutor logic, and this test is
@@ -200,4 +203,6 @@ function require(module) { \
200203
}
201204
}
202205

206+
#endif
207+
203208
@end

0 commit comments

Comments
 (0)