|
| 1 | +import { HarnessPlatformRunner } from '@react-native-harness/platforms'; |
| 2 | +import { BridgeServer } from '@react-native-harness/bridge/server'; |
| 3 | +import { NativeCrashError } from './errors.js'; |
| 4 | +import { logger } from '@react-native-harness/tools'; |
| 5 | + |
| 6 | +export type CrashMonitor = { |
| 7 | + startMonitoring(testFilePath: string): Promise<never>; |
| 8 | + stopMonitoring(): void; |
| 9 | + markIntentionalRestart(): void; |
| 10 | + clearIntentionalRestart(): void; |
| 11 | + dispose(): void; |
| 12 | +}; |
| 13 | + |
| 14 | +export type CrashMonitorOptions = { |
| 15 | + interval: number; |
| 16 | + platformRunner: HarnessPlatformRunner; |
| 17 | + bridgeServer: BridgeServer; |
| 18 | +}; |
| 19 | + |
| 20 | +export const createCrashMonitor = ({ |
| 21 | + interval, |
| 22 | + platformRunner, |
| 23 | + bridgeServer, |
| 24 | +}: CrashMonitorOptions): CrashMonitor => { |
| 25 | + let pollingInterval: NodeJS.Timeout | null = null; |
| 26 | + let isIntentionalRestart = false; |
| 27 | + let currentTestFilePath: string | null = null; |
| 28 | + let rejectFn: ((error: NativeCrashError) => void) | null = null; |
| 29 | + |
| 30 | + const handleDisconnect = () => { |
| 31 | + // Verify if it's actually a crash by checking if app is still running |
| 32 | + if (!isIntentionalRestart && currentTestFilePath) { |
| 33 | + // Capture the value to avoid it being null when setTimeout callback runs |
| 34 | + const testFilePath = currentTestFilePath; |
| 35 | + logger.debug('Bridge disconnected, checking if app crashed'); |
| 36 | + // Use a slight delay to allow the OS to clean up the process |
| 37 | + setTimeout(async () => { |
| 38 | + const isRunning = await platformRunner.isAppRunning(); |
| 39 | + if (!isRunning && !isIntentionalRestart && rejectFn) { |
| 40 | + logger.debug(`Native crash detected during: ${testFilePath}`); |
| 41 | + rejectFn(new NativeCrashError(testFilePath)); |
| 42 | + } |
| 43 | + }, 100); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const startMonitoring = (testFilePath: string): Promise<never> => { |
| 48 | + currentTestFilePath = testFilePath; |
| 49 | + |
| 50 | + return new Promise<never>((_, reject) => { |
| 51 | + rejectFn = reject; |
| 52 | + |
| 53 | + // Listen for bridge disconnect as early indicator |
| 54 | + bridgeServer.on('disconnect', handleDisconnect); |
| 55 | + |
| 56 | + // Poll for app running status |
| 57 | + pollingInterval = setInterval(async () => { |
| 58 | + // Skip check during intentional restarts |
| 59 | + if (isIntentionalRestart) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + const isRunning = await platformRunner.isAppRunning(); |
| 65 | + |
| 66 | + if (!isRunning && currentTestFilePath) { |
| 67 | + logger.debug( |
| 68 | + `Native crash detected during: ${currentTestFilePath}` |
| 69 | + ); |
| 70 | + stopMonitoring(); |
| 71 | + reject(new NativeCrashError(currentTestFilePath)); |
| 72 | + } |
| 73 | + } catch (error) { |
| 74 | + logger.error('Error checking app status:', error); |
| 75 | + } |
| 76 | + }, interval); |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + const stopMonitoring = () => { |
| 81 | + if (pollingInterval) { |
| 82 | + clearInterval(pollingInterval); |
| 83 | + pollingInterval = null; |
| 84 | + } |
| 85 | + bridgeServer.off('disconnect', handleDisconnect); |
| 86 | + currentTestFilePath = null; |
| 87 | + rejectFn = null; |
| 88 | + }; |
| 89 | + |
| 90 | + const markIntentionalRestart = () => { |
| 91 | + isIntentionalRestart = true; |
| 92 | + }; |
| 93 | + |
| 94 | + const clearIntentionalRestart = () => { |
| 95 | + isIntentionalRestart = false; |
| 96 | + }; |
| 97 | + |
| 98 | + const dispose = () => { |
| 99 | + stopMonitoring(); |
| 100 | + }; |
| 101 | + |
| 102 | + return { |
| 103 | + startMonitoring, |
| 104 | + stopMonitoring, |
| 105 | + markIntentionalRestart, |
| 106 | + clearIntentionalRestart, |
| 107 | + dispose, |
| 108 | + }; |
| 109 | +}; |
0 commit comments