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 ]; \
2626while ((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" );
0 commit comments