@hot-updater/react-native 1.0.0-rc.1 → 1.0.0-rc.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/android/proguard-rules.pro +15 -0
  2. package/android/src/main/java/com/hotupdater/BundleFileStorageService.kt +1 -1
  3. package/android/src/main/java/com/hotupdater/BundleMetadata.kt +7 -0
  4. package/android/src/main/java/com/hotupdater/HotUpdaterRecoveryManager.kt +37 -54
  5. package/android/src/main/java/com/hotupdater/HotUpdaterRecoveryState.kt +64 -0
  6. package/android/src/test/java/com/hotupdater/HotUpdaterRecoveryStateTest.kt +97 -0
  7. package/android/src/test/java/com/hotupdater/PendingCrashRecoveryTest.kt +44 -0
  8. package/ios/HotUpdater/Internal/HotUpdaterImpl.swift +102 -20
  9. package/ios/HotUpdater/Recovery/HotUpdaterRecovery.h +16 -0
  10. package/ios/HotUpdater/Recovery/HotUpdaterRecovery.mm +83 -0
  11. package/ios/HotUpdater/Recovery/HotUpdaterRecoveryHooks.mm +49 -0
  12. package/lib/commonjs/checkForUpdate.js +25 -13
  13. package/lib/commonjs/checkForUpdate.js.map +1 -1
  14. package/lib/commonjs/checkForUpdate.releaseCatalog.spec.js +88 -24
  15. package/lib/commonjs/checkForUpdate.releaseCatalog.spec.js.map +1 -1
  16. package/lib/commonjs/httpClient.js +2 -2
  17. package/lib/commonjs/httpClient.js.map +1 -1
  18. package/lib/commonjs/index.js +30 -9
  19. package/lib/commonjs/index.js.map +1 -1
  20. package/lib/commonjs/index.spec.js +56 -11
  21. package/lib/commonjs/index.spec.js.map +1 -1
  22. package/lib/commonjs/native.js +8 -6
  23. package/lib/commonjs/native.js.map +1 -1
  24. package/lib/commonjs/native.spec.js +107 -6
  25. package/lib/commonjs/native.spec.js.map +1 -1
  26. package/lib/commonjs/{notifyAppReadyAnalytics.boundary.spec.js → notifyAppReadyInsights.boundary.spec.js} +21 -21
  27. package/lib/commonjs/notifyAppReadyInsights.boundary.spec.js.map +1 -0
  28. package/lib/commonjs/notifyAppReadyInsights.js +185 -0
  29. package/lib/commonjs/notifyAppReadyInsights.js.map +1 -0
  30. package/lib/commonjs/{notifyAppReadyAnalytics.spec.js → notifyAppReadyInsights.spec.js} +102 -29
  31. package/lib/commonjs/notifyAppReadyInsights.spec.js.map +1 -0
  32. package/lib/commonjs/{notifyAppReadyAnalytics.test-utils.js → notifyAppReadyInsights.test-utils.js} +3 -3
  33. package/lib/commonjs/notifyAppReadyInsights.test-utils.js.map +1 -0
  34. package/lib/commonjs/{notifyAppReadyAnalytics.transport.spec.js → notifyAppReadyInsights.transport.spec.js} +7 -7
  35. package/lib/commonjs/notifyAppReadyInsights.transport.spec.js.map +1 -0
  36. package/lib/commonjs/sdkVersion.js +1 -1
  37. package/lib/commonjs/sdkVersion.js.map +1 -1
  38. package/lib/commonjs/wrap.js +10 -9
  39. package/lib/commonjs/wrap.js.map +1 -1
  40. package/lib/commonjs/wrap.spec.js +90 -25
  41. package/lib/commonjs/wrap.spec.js.map +1 -1
  42. package/lib/module/checkForUpdate.js +25 -13
  43. package/lib/module/checkForUpdate.js.map +1 -1
  44. package/lib/module/checkForUpdate.releaseCatalog.spec.js +88 -24
  45. package/lib/module/checkForUpdate.releaseCatalog.spec.js.map +1 -1
  46. package/lib/module/httpClient.js +2 -2
  47. package/lib/module/httpClient.js.map +1 -1
  48. package/lib/module/index.js +31 -10
  49. package/lib/module/index.js.map +1 -1
  50. package/lib/module/index.spec.js +56 -11
  51. package/lib/module/index.spec.js.map +1 -1
  52. package/lib/module/native.js +6 -4
  53. package/lib/module/native.js.map +1 -1
  54. package/lib/module/native.spec.js +107 -6
  55. package/lib/module/native.spec.js.map +1 -1
  56. package/lib/module/{notifyAppReadyAnalytics.boundary.spec.js → notifyAppReadyInsights.boundary.spec.js} +16 -16
  57. package/lib/module/notifyAppReadyInsights.boundary.spec.js.map +1 -0
  58. package/lib/module/notifyAppReadyInsights.js +178 -0
  59. package/lib/module/notifyAppReadyInsights.js.map +1 -0
  60. package/lib/module/{notifyAppReadyAnalytics.spec.js → notifyAppReadyInsights.spec.js} +94 -21
  61. package/lib/module/notifyAppReadyInsights.spec.js.map +1 -0
  62. package/lib/module/{notifyAppReadyAnalytics.test-utils.js → notifyAppReadyInsights.test-utils.js} +3 -3
  63. package/lib/module/notifyAppReadyInsights.test-utils.js.map +1 -0
  64. package/lib/module/{notifyAppReadyAnalytics.transport.spec.js → notifyAppReadyInsights.transport.spec.js} +4 -4
  65. package/lib/module/notifyAppReadyInsights.transport.spec.js.map +1 -0
  66. package/lib/module/sdkVersion.js +1 -1
  67. package/lib/module/sdkVersion.js.map +1 -1
  68. package/lib/module/wrap.js +10 -9
  69. package/lib/module/wrap.js.map +1 -1
  70. package/lib/module/wrap.spec.js +92 -25
  71. package/lib/module/wrap.spec.js.map +1 -1
  72. package/lib/typescript/commonjs/checkForUpdate.d.ts +1 -1
  73. package/lib/typescript/commonjs/checkForUpdate.d.ts.map +1 -1
  74. package/lib/typescript/commonjs/httpClient.d.ts +6 -6
  75. package/lib/typescript/commonjs/httpClient.d.ts.map +1 -1
  76. package/lib/typescript/commonjs/index.d.ts +4 -5
  77. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  78. package/lib/typescript/commonjs/native.d.ts +4 -3
  79. package/lib/typescript/commonjs/native.d.ts.map +1 -1
  80. package/lib/typescript/commonjs/notifyAppReadyInsights.d.ts +25 -0
  81. package/lib/typescript/commonjs/notifyAppReadyInsights.d.ts.map +1 -0
  82. package/lib/typescript/commonjs/notifyAppReadyInsights.test-utils.d.ts +8 -0
  83. package/lib/typescript/commonjs/notifyAppReadyInsights.test-utils.d.ts.map +1 -0
  84. package/lib/typescript/commonjs/sdkVersion.d.ts +1 -1
  85. package/lib/typescript/commonjs/sdkVersion.d.ts.map +1 -1
  86. package/lib/typescript/commonjs/wrap.d.ts.map +1 -1
  87. package/lib/typescript/commonjs/wrap.types.d.ts +5 -6
  88. package/lib/typescript/commonjs/wrap.types.d.ts.map +1 -1
  89. package/lib/typescript/module/checkForUpdate.d.ts +1 -1
  90. package/lib/typescript/module/checkForUpdate.d.ts.map +1 -1
  91. package/lib/typescript/module/httpClient.d.ts +6 -6
  92. package/lib/typescript/module/httpClient.d.ts.map +1 -1
  93. package/lib/typescript/module/index.d.ts +4 -5
  94. package/lib/typescript/module/index.d.ts.map +1 -1
  95. package/lib/typescript/module/native.d.ts +4 -3
  96. package/lib/typescript/module/native.d.ts.map +1 -1
  97. package/lib/typescript/module/notifyAppReadyInsights.d.ts +25 -0
  98. package/lib/typescript/module/notifyAppReadyInsights.d.ts.map +1 -0
  99. package/lib/typescript/module/notifyAppReadyInsights.test-utils.d.ts +8 -0
  100. package/lib/typescript/module/notifyAppReadyInsights.test-utils.d.ts.map +1 -0
  101. package/lib/typescript/module/sdkVersion.d.ts +1 -1
  102. package/lib/typescript/module/sdkVersion.d.ts.map +1 -1
  103. package/lib/typescript/module/wrap.d.ts.map +1 -1
  104. package/lib/typescript/module/wrap.types.d.ts +5 -6
  105. package/lib/typescript/module/wrap.types.d.ts.map +1 -1
  106. package/package.json +3 -3
  107. package/src/checkForUpdate.releaseCatalog.spec.ts +127 -57
  108. package/src/checkForUpdate.ts +30 -18
  109. package/src/httpClient.ts +11 -11
  110. package/src/index.spec.ts +59 -11
  111. package/src/index.ts +41 -13
  112. package/src/native.spec.ts +110 -6
  113. package/src/native.ts +9 -8
  114. package/src/{notifyAppReadyAnalytics.boundary.spec.ts → notifyAppReadyInsights.boundary.spec.ts} +17 -17
  115. package/src/{notifyAppReadyAnalytics.spec.ts → notifyAppReadyInsights.spec.ts} +81 -22
  116. package/src/{notifyAppReadyAnalytics.test-utils.ts → notifyAppReadyInsights.test-utils.ts} +4 -4
  117. package/src/{notifyAppReadyAnalytics.transport.spec.ts → notifyAppReadyInsights.transport.spec.ts} +5 -5
  118. package/src/notifyAppReadyInsights.ts +281 -0
  119. package/src/sdkVersion.ts +1 -1
  120. package/src/wrap.spec.ts +101 -31
  121. package/src/wrap.tsx +11 -9
  122. package/src/wrap.types.ts +5 -6
  123. package/lib/commonjs/notifyAppReadyAnalytics.boundary.spec.js.map +0 -1
  124. package/lib/commonjs/notifyAppReadyAnalytics.js +0 -133
  125. package/lib/commonjs/notifyAppReadyAnalytics.js.map +0 -1
  126. package/lib/commonjs/notifyAppReadyAnalytics.spec.js.map +0 -1
  127. package/lib/commonjs/notifyAppReadyAnalytics.test-utils.js.map +0 -1
  128. package/lib/commonjs/notifyAppReadyAnalytics.transport.spec.js.map +0 -1
  129. package/lib/module/notifyAppReadyAnalytics.boundary.spec.js.map +0 -1
  130. package/lib/module/notifyAppReadyAnalytics.js +0 -128
  131. package/lib/module/notifyAppReadyAnalytics.js.map +0 -1
  132. package/lib/module/notifyAppReadyAnalytics.spec.js.map +0 -1
  133. package/lib/module/notifyAppReadyAnalytics.test-utils.js.map +0 -1
  134. package/lib/module/notifyAppReadyAnalytics.transport.spec.js.map +0 -1
  135. package/lib/typescript/commonjs/notifyAppReadyAnalytics.d.ts +0 -13
  136. package/lib/typescript/commonjs/notifyAppReadyAnalytics.d.ts.map +0 -1
  137. package/lib/typescript/commonjs/notifyAppReadyAnalytics.test-utils.d.ts +0 -8
  138. package/lib/typescript/commonjs/notifyAppReadyAnalytics.test-utils.d.ts.map +0 -1
  139. package/lib/typescript/module/notifyAppReadyAnalytics.d.ts +0 -13
  140. package/lib/typescript/module/notifyAppReadyAnalytics.d.ts.map +0 -1
  141. package/lib/typescript/module/notifyAppReadyAnalytics.test-utils.d.ts +0 -8
  142. package/lib/typescript/module/notifyAppReadyAnalytics.test-utils.d.ts.map +0 -1
  143. package/src/notifyAppReadyAnalytics.ts +0 -194
@@ -2,9 +2,19 @@
2
2
  # Invoked via reflection, when setting js bundle.
3
3
  -keepclassmembers class com.facebook.react.ReactInstanceManager {
4
4
  private final ** mBundleLoader;
5
+ private final ** mJSExceptionHandler;
5
6
  }
6
7
 
7
8
  # New Architecture
9
+ # Crash recovery discovers these accessors through reflection.
10
+ -keepclassmembers class * implements com.facebook.react.ReactApplication {
11
+ public *** getReactHost();
12
+ }
13
+
14
+ -keepclassmembers class * implements com.facebook.react.ReactHost {
15
+ public *** getCurrentReactContext();
16
+ }
17
+
8
18
  # Keep fields accessed via reflection in ReactHost
9
19
  # Support both Java (mReactHostDelegate) and Kotlin (reactHostDelegate) field names
10
20
  -keepclassmembers class com.facebook.react.runtime.ReactHostImpl {
@@ -14,6 +24,11 @@
14
24
 
15
25
  -keepclassmembers class * implements com.facebook.react.runtime.ReactHostDelegate {
16
26
  ** jsBundleLoader;
27
+ ** exceptionHandler;
28
+ }
29
+
30
+ -keepclassmembers class com.facebook.react.bridge.CatalystInstanceImpl {
31
+ ** mJSExceptionHandler;
17
32
  }
18
33
 
19
34
  # Preserve the Brotli decoder and its embedded static dictionary
@@ -149,7 +149,7 @@ interface BundleStorageService {
149
149
  fun getUsername(): String?
150
150
 
151
151
  /**
152
- * Persists the optional user envelope for analytics.
152
+ * Persists the optional user envelope for insights.
153
153
  * Passing null clears the stored user identity.
154
154
  */
155
155
  fun setUser(
@@ -394,6 +394,13 @@ data class PendingCrashRecovery(
394
394
  val shouldRollback: Boolean,
395
395
  ) {
396
396
  companion object {
397
+ internal fun loadFromFile(file: File): PendingCrashRecovery? =
398
+ try {
399
+ fromJson(JSONObject(file.readText()))
400
+ } catch (_: Exception) {
401
+ null
402
+ }
403
+
397
404
  fun fromJson(json: JSONObject): PendingCrashRecovery =
398
405
  PendingCrashRecovery(
399
406
  launchedBundleId =
@@ -9,6 +9,7 @@ import android.os.Looper
9
9
  import android.os.Process
10
10
  import android.os.SystemClock
11
11
  import android.util.Log
12
+ import androidx.core.util.AtomicFile
12
13
  import com.facebook.react.ReactApplication
13
14
  import com.facebook.react.bridge.JSExceptionHandler
14
15
  import com.facebook.react.bridge.ReactContext
@@ -16,6 +17,7 @@ import com.facebook.react.bridge.ReactMarker
16
17
  import com.facebook.react.bridge.ReactMarkerConstants
17
18
  import org.json.JSONObject
18
19
  import java.io.File
20
+ import java.io.FileOutputStream
19
21
  import java.lang.reflect.Field
20
22
  import kotlin.system.exitProcess
21
23
 
@@ -28,10 +30,7 @@ internal class HotUpdaterRecoveryManager(
28
30
  private val crashMarkerFile = File(bundleStoreDir, CRASH_MARKER_FILENAME)
29
31
  private val watchdogStateFile = File(bundleStoreDir, WATCHDOG_STATE_FILENAME)
30
32
 
31
- private var currentBundleId: String? = null
32
- private var shouldRollbackOnCrash = false
33
- private var isMonitoring = false
34
- private var recoveryRequested = false
33
+ private val launchState = HotUpdaterRecoveryState()
35
34
  private var contentAppearedCallback: ((String?) -> Unit)? = null
36
35
  private var recoveryRestartCallback: (() -> Boolean)? = null
37
36
 
@@ -39,10 +38,7 @@ internal class HotUpdaterRecoveryManager(
39
38
  Runnable {
40
39
  cancelRecoveryWatchdog()
41
40
  Log.d(TAG, "Stopping crash monitoring for current launch")
42
- isMonitoring = false
43
- recoveryRequested = false
44
- shouldRollbackOnCrash = false
45
- currentBundleId = null
41
+ launchState.stop()
46
42
  contentAppearedCallback = null
47
43
  recoveryRestartCallback = null
48
44
  activeManager = null
@@ -52,7 +48,7 @@ internal class HotUpdaterRecoveryManager(
52
48
  private val installJsExceptionHooksRunnable =
53
49
  object : Runnable {
54
50
  override fun run() {
55
- if (!isMonitoring) {
51
+ if (launchState.snapshot() == null) {
56
52
  return
57
53
  }
58
54
 
@@ -74,7 +70,7 @@ internal class HotUpdaterRecoveryManager(
74
70
  }
75
71
 
76
72
  return try {
77
- val recovery = PendingCrashRecovery.fromJson(JSONObject(crashMarkerFile.readText()))
73
+ val recovery = PendingCrashRecovery.loadFromFile(crashMarkerFile) ?: return null
78
74
  Log.d(
79
75
  TAG,
80
76
  "Consumed pending crash marker bundleId=${recovery.launchedBundleId} shouldRollback=${recovery.shouldRollback}",
@@ -96,11 +92,8 @@ internal class HotUpdaterRecoveryManager(
96
92
  ) {
97
93
  crashMarkerFile.parentFile?.mkdirs()
98
94
  contentAppearedCallback = onContentAppeared
99
- currentBundleId = bundleId
100
- shouldRollbackOnCrash = shouldRollback
101
- isMonitoring = true
102
- recoveryRequested = false
103
95
  recoveryRestartCallback = onRecoveryRestartRequested
96
+ launchState.start(bundleId, shouldRollback)
104
97
  activeManager = this
105
98
 
106
99
  ensureExceptionHandlerInstalled()
@@ -122,17 +115,13 @@ internal class HotUpdaterRecoveryManager(
122
115
  }
123
116
 
124
117
  private fun handleContentAppeared() {
125
- if (!isMonitoring) {
126
- return
127
- }
128
-
129
- Log.d(TAG, "First content appeared for bundleId=$currentBundleId")
118
+ val launch = launchState.completeLaunch() ?: return
119
+ Log.d(TAG, "First content appeared for bundleId=${launch.bundleId}")
130
120
  ReactMarker.removeListener(contentAppearedListener)
131
121
  mainHandler.removeCallbacks(installJsExceptionHooksRunnable)
132
122
  mainHandler.removeCallbacks(stopMonitoringRunnable)
133
- contentAppearedCallback?.invoke(currentBundleId)
134
- shouldRollbackOnCrash = false
135
- updateNativeLaunchState(currentBundleId, false)
123
+ contentAppearedCallback?.invoke(launch.bundleId)
124
+ updateNativeLaunchState(launch.bundleId, false)
136
125
  cancelRecoveryWatchdog()
137
126
  mainHandler.postDelayed(stopMonitoringRunnable, MONITORING_GRACE_PERIOD_MS)
138
127
  }
@@ -145,11 +134,10 @@ internal class HotUpdaterRecoveryManager(
145
134
  previousExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
146
135
  Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
147
136
  val manager = activeManager
148
- manager?.writeCrashMarker()
149
-
150
137
  if (manager?.requestAutomaticRecovery() == true) {
151
- Process.killProcess(Process.myPid())
152
- exitProcess(10)
138
+ // The separate restart activity kills this process after it starts.
139
+ // A second fatal error must not kill it while that request is in flight.
140
+ return@setDefaultUncaughtExceptionHandler
153
141
  }
154
142
 
155
143
  previousExceptionHandler?.uncaughtException(thread, throwable)
@@ -161,20 +149,21 @@ internal class HotUpdaterRecoveryManager(
161
149
  exceptionHandlerInstalled = true
162
150
  }
163
151
 
164
- private fun writeCrashMarker() {
165
- if (!isMonitoring) {
166
- return
167
- }
168
-
152
+ private fun writeCrashMarker(launch: HotUpdaterRecoveryState.Launch) {
153
+ val atomicFile = AtomicFile(crashMarkerFile)
154
+ var output: FileOutputStream? = null
169
155
  try {
170
156
  val payload =
171
157
  JSONObject().apply {
172
- put("bundleId", currentBundleId ?: JSONObject.NULL)
173
- put("shouldRollback", shouldRollbackOnCrash)
158
+ put("bundleId", launch.bundleId ?: JSONObject.NULL)
159
+ put("shouldRollback", launch.shouldRollback)
174
160
  }
175
161
  crashMarkerFile.parentFile?.mkdirs()
176
- crashMarkerFile.writeText(payload.toString())
162
+ output = atomicFile.startWrite()
163
+ output.write(payload.toString().toByteArray(Charsets.UTF_8))
164
+ atomicFile.finishWrite(output)
177
165
  } catch (e: Exception) {
166
+ output?.let(atomicFile::failWrite)
178
167
  Log.e(TAG, "Failed to write crash marker", e)
179
168
  }
180
169
  }
@@ -194,35 +183,28 @@ internal class HotUpdaterRecoveryManager(
194
183
  cancelRecoveryWatchdogAlarm(appContext)
195
184
  }
196
185
 
197
- private fun requestAutomaticRecovery(): Boolean {
198
- if (!isMonitoring || !shouldRollbackOnCrash) {
199
- return false
186
+ private fun requestAutomaticRecovery(exception: Exception? = null): Boolean {
187
+ val request = launchState.requestRecovery()
188
+ if (exception != null) {
189
+ Log.e(TAG, "Caught React startup exception for bundleId=${launchState.snapshot()?.bundleId}", exception)
200
190
  }
201
-
202
- synchronized(this) {
203
- if (recoveryRequested) {
204
- return true
205
- }
206
- recoveryRequested = true
191
+ if (request == null) {
192
+ launchState.snapshot()?.let(::writeCrashMarker)
193
+ return false
207
194
  }
208
-
195
+ if (!request.shouldRestart) return true
196
+ writeCrashMarker(request.launch)
209
197
  val started = recoveryRestartCallback?.invoke() == true
198
+ launchState.finishRecoveryRequest(request, started)
210
199
  if (!started) {
211
- synchronized(this) {
212
- recoveryRequested = false
213
- }
214
200
  Log.w(TAG, "Failed to schedule automatic recovery restart")
215
201
  } else {
216
- Log.i(TAG, "Scheduled automatic recovery restart for bundleId=$currentBundleId")
202
+ Log.i(TAG, "Scheduled automatic recovery restart for bundleId=${request.launch.bundleId}")
217
203
  }
218
204
  return started
219
205
  }
220
206
 
221
- private fun handleJavaScriptException(exception: Exception): Boolean {
222
- Log.e(TAG, "Caught React startup exception for bundleId=$currentBundleId", exception)
223
- writeCrashMarker()
224
- return requestAutomaticRecovery()
225
- }
207
+ private fun handleJavaScriptException(exception: Exception): Boolean = requestAutomaticRecovery(exception)
226
208
 
227
209
  private fun ensureNativeSignalHandlerInstalled() {
228
210
  if (signalHandlerInstalled || !loadNativeLibrary()) {
@@ -467,7 +449,8 @@ internal class HotUpdaterRecoveryManager(
467
449
  }
468
450
 
469
451
  val crashMarkerFile = File(getBundleStoreDir(appContext), CRASH_MARKER_FILENAME)
470
- if (crashMarkerFile.exists()) {
452
+ val recovery = PendingCrashRecovery.loadFromFile(crashMarkerFile)
453
+ if (recovery?.shouldRollback == true && recovery.launchedBundleId != null) {
471
454
  Log.i(TAG, "Recovery watchdog detected crash marker, relaunching app")
472
455
  watchdogStateFile.delete()
473
456
  cancelRecoveryWatchdogAlarm(appContext)
@@ -0,0 +1,64 @@
1
+ package com.hotupdater
2
+
3
+ internal class HotUpdaterRecoveryState {
4
+ data class Launch(
5
+ val bundleId: String?,
6
+ val shouldRollback: Boolean,
7
+ )
8
+
9
+ data class RecoveryRequest(
10
+ val launch: Launch,
11
+ val shouldRestart: Boolean,
12
+ )
13
+
14
+ private enum class RestartState { NONE, REQUESTED, SCHEDULED, FAILED }
15
+
16
+ private var launch: Launch? = null
17
+ private var restartState = RestartState.NONE
18
+ private var contentAppeared = false
19
+
20
+ @Synchronized
21
+ fun start(
22
+ bundleId: String?,
23
+ shouldRollback: Boolean,
24
+ ) {
25
+ launch = Launch(bundleId, shouldRollback)
26
+ restartState = RestartState.NONE
27
+ contentAppeared = false
28
+ }
29
+
30
+ @Synchronized
31
+ fun snapshot(): Launch? = launch
32
+
33
+ @Synchronized
34
+ fun stop() {
35
+ launch = null
36
+ restartState = RestartState.NONE
37
+ }
38
+
39
+ @Synchronized
40
+ fun completeLaunch(): Launch? {
41
+ val current = launch ?: return null
42
+ if (contentAppeared || restartState != RestartState.NONE) return null
43
+ contentAppeared = true
44
+ launch = current.copy(shouldRollback = false)
45
+ return current
46
+ }
47
+
48
+ @Synchronized
49
+ fun requestRecovery(): RecoveryRequest? {
50
+ val current = launch?.takeIf { it.shouldRollback } ?: return null
51
+ val shouldRestart = restartState == RestartState.NONE || restartState == RestartState.FAILED
52
+ if (shouldRestart) restartState = RestartState.REQUESTED
53
+ return RecoveryRequest(current, shouldRestart)
54
+ }
55
+
56
+ @Synchronized
57
+ fun finishRecoveryRequest(
58
+ request: RecoveryRequest,
59
+ started: Boolean,
60
+ ) {
61
+ if (launch !== request.launch) return
62
+ restartState = if (started) RestartState.SCHEDULED else RestartState.FAILED
63
+ }
64
+ }
@@ -0,0 +1,97 @@
1
+ package com.hotupdater
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertNotNull
6
+ import org.junit.Assert.assertNull
7
+ import org.junit.Assert.assertTrue
8
+ import org.junit.Test
9
+ import java.util.concurrent.CountDownLatch
10
+ import java.util.concurrent.TimeUnit
11
+ import java.util.concurrent.atomic.AtomicInteger
12
+
13
+ class HotUpdaterRecoveryStateTest {
14
+ @Test
15
+ fun `content queued after a fatal error cannot verify the failed bundle`() {
16
+ val state = HotUpdaterRecoveryState()
17
+ state.start("crashed-bundle", true)
18
+
19
+ val request = requireNotNull(state.requestRecovery())
20
+ assertNull(state.completeLaunch())
21
+ state.finishRecoveryRequest(request, true)
22
+
23
+ assertNull(state.completeLaunch())
24
+ assertEquals(HotUpdaterRecoveryState.Launch("crashed-bundle", true), state.snapshot())
25
+ }
26
+
27
+ @Test
28
+ fun `a fatal error after first content preserves the verified bundle`() {
29
+ val state = HotUpdaterRecoveryState()
30
+ state.start("verified-bundle", true)
31
+
32
+ assertNotNull(state.completeLaunch())
33
+
34
+ assertNull(state.requestRecovery())
35
+ assertNull(state.completeLaunch())
36
+ assertEquals(HotUpdaterRecoveryState.Launch("verified-bundle", false), state.snapshot())
37
+ }
38
+
39
+ @Test
40
+ fun `concurrent fatal errors schedule only one restart while the request is in flight`() {
41
+ val state = HotUpdaterRecoveryState()
42
+ state.start("crashed-bundle", true)
43
+ val begin = CountDownLatch(1)
44
+ val finished = CountDownLatch(2)
45
+ val restartRequests = AtomicInteger()
46
+ val handledErrors = AtomicInteger()
47
+ val workers =
48
+ List(2) {
49
+ Thread {
50
+ begin.await()
51
+ val request = state.requestRecovery()
52
+ if (request != null) {
53
+ handledErrors.incrementAndGet()
54
+ if (request.shouldRestart) restartRequests.incrementAndGet()
55
+ }
56
+ finished.countDown()
57
+ }.apply { start() }
58
+ }
59
+ begin.countDown()
60
+ assertTrue(finished.await(5, TimeUnit.SECONDS))
61
+ workers.forEach { it.join() }
62
+
63
+ assertEquals(2, handledErrors.get())
64
+ assertEquals(1, restartRequests.get())
65
+ assertNull(state.completeLaunch())
66
+ }
67
+
68
+ @Test
69
+ fun `a failed restart keeps the crash unverified and permits another restart request`() {
70
+ val state = HotUpdaterRecoveryState()
71
+ state.start("crashed-bundle", true)
72
+ val request = requireNotNull(state.requestRecovery())
73
+
74
+ state.finishRecoveryRequest(request, false)
75
+
76
+ assertNull(state.completeLaunch())
77
+ val retry = requireNotNull(state.requestRecovery())
78
+ assertTrue(retry.shouldRestart)
79
+ state.finishRecoveryRequest(retry, true)
80
+ assertFalse(requireNotNull(state.requestRecovery()).shouldRestart)
81
+ }
82
+
83
+ @Test
84
+ fun `a previous launch restart completion cannot change the next launch`() {
85
+ val state = HotUpdaterRecoveryState()
86
+ state.start("crashed-bundle", true)
87
+ val request = requireNotNull(state.requestRecovery())
88
+ state.start("safe-bundle", false)
89
+
90
+ state.finishRecoveryRequest(request, true)
91
+
92
+ assertEquals("safe-bundle", state.completeLaunch()?.bundleId)
93
+ state.stop()
94
+ assertNull(state.snapshot())
95
+ assertNull(state.requestRecovery())
96
+ }
97
+ }
@@ -0,0 +1,44 @@
1
+ package com.hotupdater
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertNull
6
+ import org.junit.Rule
7
+ import org.junit.Test
8
+ import org.junit.rules.TemporaryFolder
9
+ import java.io.File
10
+
11
+ class PendingCrashRecoveryTest {
12
+ @get:Rule
13
+ val temporaryFolder = TemporaryFolder()
14
+
15
+ @Test
16
+ fun `a watchdog reader waits until the native crash marker is fully written`() {
17
+ val marker = temporaryFolder.newFile("crash-marker.json")
18
+ marker.writeText("{\"bundleId\":\"crashed-bundle\",\"shouldRollback\":")
19
+
20
+ assertNull(PendingCrashRecovery.loadFromFile(marker))
21
+
22
+ marker.appendText("true}\n")
23
+ assertEquals(
24
+ PendingCrashRecovery("crashed-bundle", true),
25
+ PendingCrashRecovery.loadFromFile(marker),
26
+ )
27
+ }
28
+
29
+ @Test
30
+ fun `missing or empty markers do not request a restart`() {
31
+ assertNull(PendingCrashRecovery.loadFromFile(File(temporaryFolder.root, "missing.json")))
32
+ assertNull(PendingCrashRecovery.loadFromFile(temporaryFolder.newFile("empty.json")))
33
+ }
34
+
35
+ @Test
36
+ fun `a crash after content appeared does not request rollback`() {
37
+ val marker = temporaryFolder.newFile("verified-crash.json")
38
+ marker.writeText("{\"bundleId\":\"verified-bundle\",\"shouldRollback\":false}")
39
+
40
+ val recovery = requireNotNull(PendingCrashRecovery.loadFromFile(marker))
41
+ assertFalse(recovery.shouldRollback)
42
+ assertEquals("verified-bundle", recovery.launchedBundleId)
43
+ }
44
+ }