|
1 | 1 | package io.sentry.react |
2 | 2 |
|
| 3 | +import android.os.Looper |
| 4 | +import com.facebook.react.bridge.Promise |
| 5 | +import io.sentry.ILogger |
| 6 | +import io.sentry.SentryDate |
| 7 | +import io.sentry.SentryDateProvider |
| 8 | +import io.sentry.SentryLevel |
3 | 9 | import org.junit.Assert.assertEquals |
4 | 10 | import org.junit.Assert.assertNotNull |
5 | 11 | import org.junit.Assert.assertNull |
6 | 12 | import org.junit.Before |
7 | 13 | import org.junit.Test |
8 | 14 | import org.junit.runner.RunWith |
9 | | -import org.junit.runners.JUnit4 |
| 15 | +import org.mockito.kotlin.any |
| 16 | +import org.mockito.kotlin.doThrow |
| 17 | +import org.mockito.kotlin.eq |
| 18 | +import org.mockito.kotlin.mock |
| 19 | +import org.mockito.kotlin.never |
| 20 | +import org.mockito.kotlin.times |
| 21 | +import org.mockito.kotlin.verify |
| 22 | +import org.mockito.kotlin.whenever |
| 23 | +import org.robolectric.RobolectricTestRunner |
| 24 | +import org.robolectric.Shadows.shadowOf |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean |
10 | 26 |
|
11 | | -@RunWith(JUnit4::class) |
| 27 | +@RunWith(RobolectricTestRunner::class) |
12 | 28 | class RNSentryTimeToDisplayTest { |
13 | 29 | companion object { |
14 | 30 | val TEST_ID = "test-id" |
@@ -45,4 +61,137 @@ class RNSentryTimeToDisplayTest { |
45 | 61 | assertNotNull(secondOldestEntry) |
46 | 62 | assertNotNull(newestEntry) |
47 | 63 | } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `resolveSafely settles the promise only once`() { |
| 67 | + val promise = mock<Promise>() |
| 68 | + val logger = mock<ILogger>() |
| 69 | + val settled = AtomicBoolean(false) |
| 70 | + |
| 71 | + RNSentryTimeToDisplay.resolveSafely(promise, settled, 1.0, logger) |
| 72 | + RNSentryTimeToDisplay.resolveSafely(promise, settled, 2.0, logger) |
| 73 | + |
| 74 | + verify(promise, times(1)).resolve(1.0) |
| 75 | + verify(promise, never()).resolve(2.0) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `resolveSafely does not throw when the bridge callback was already settled`() { |
| 80 | + val promise = mock<Promise>() |
| 81 | + val logger = mock<ILogger>() |
| 82 | + val settled = AtomicBoolean(false) |
| 83 | + // Simulate the runtime having already settled/invalidated the bridge callback, as happens |
| 84 | + // on Expo bridgeless under rapid navigation ("JavaCallback was already settled"). (#6709) |
| 85 | + doThrow(RuntimeException("JavaCallback was already settled. Cannot invoke it again")) |
| 86 | + .whenever(promise) |
| 87 | + .resolve(any()) |
| 88 | + |
| 89 | + // Must not propagate - a throw here reaches the host app as a fatal uncaught exception. |
| 90 | + RNSentryTimeToDisplay.resolveSafely(promise, settled, 1.0, logger) |
| 91 | + |
| 92 | + verify(logger).log(eq(SentryLevel.WARNING), any<String>(), any<Throwable>()) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `rejectSafely is a no-op once the promise has been resolved`() { |
| 97 | + val promise = mock<Promise>() |
| 98 | + val logger = mock<ILogger>() |
| 99 | + val settled = AtomicBoolean(false) |
| 100 | + |
| 101 | + RNSentryTimeToDisplay.resolveSafely(promise, settled, 1.0, logger) |
| 102 | + RNSentryTimeToDisplay.rejectSafely(promise, settled, "boom", null, logger) |
| 103 | + |
| 104 | + verify(promise, times(1)).resolve(1.0) |
| 105 | + verify(promise, never()).reject(any<String>(), any<String>()) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `rejectSafely does not throw when the bridge callback was already settled`() { |
| 110 | + val promise = mock<Promise>() |
| 111 | + val logger = mock<ILogger>() |
| 112 | + val settled = AtomicBoolean(false) |
| 113 | + doThrow(RuntimeException("JavaCallback was already settled. Cannot invoke it again")) |
| 114 | + .whenever(promise) |
| 115 | + .reject(any<String>(), any<String>()) |
| 116 | + |
| 117 | + RNSentryTimeToDisplay.rejectSafely(promise, settled, "boom", null, logger) |
| 118 | + |
| 119 | + verify(logger).log(eq(SentryLevel.WARNING), any<String>(), any<Throwable>()) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun `rejectSafely with a cause passes an explicit error code to the coded overload`() { |
| 124 | + val promise = mock<Promise>() |
| 125 | + val logger = mock<ILogger>() |
| 126 | + val settled = AtomicBoolean(false) |
| 127 | + val cause = RuntimeException("boom") |
| 128 | + |
| 129 | + RNSentryTimeToDisplay.rejectSafely(promise, settled, "message", cause, logger) |
| 130 | + |
| 131 | + // The code must be the stable "SentryReactNative" identifier, not the descriptive message - |
| 132 | + // reject(String, Throwable) would otherwise use the message as the error code. |
| 133 | + verify(promise, times(1)).reject(eq("SentryReactNative"), eq("message"), eq(cause)) |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `resolveSafely swallows failures that are not the already-settled case`() { |
| 138 | + val promise = mock<Promise>() |
| 139 | + val logger = mock<ILogger>() |
| 140 | + val settled = AtomicBoolean(false) |
| 141 | + // The guard must degrade on any settle failure, not only the literal "already settled" |
| 142 | + // message - e.g. a torn-down Catalyst instance - so it can never crash the host app. |
| 143 | + doThrow(IllegalStateException("Tried to access a JS module after the React instance was destroyed")) |
| 144 | + .whenever(promise) |
| 145 | + .resolve(any()) |
| 146 | + |
| 147 | + RNSentryTimeToDisplay.resolveSafely(promise, settled, 1.0, logger) |
| 148 | + |
| 149 | + verify(logger).log(eq(SentryLevel.WARNING), any<String>(), any<Throwable>()) |
| 150 | + } |
| 151 | + |
| 152 | + // End-to-end tests that drive the real getTimeToDisplay path (main-thread Handler post + |
| 153 | + // Choreographer frame callback), so the crashing production code - not just the helpers - is |
| 154 | + // covered. |
| 155 | + |
| 156 | + private fun mockDateProvider(nanoTimestamp: Long): SentryDateProvider { |
| 157 | + val date = mock<SentryDate>() |
| 158 | + whenever(date.nanoTimestamp()).thenReturn(nanoTimestamp) |
| 159 | + val dateProvider = mock<SentryDateProvider>() |
| 160 | + whenever(dateProvider.now()).thenReturn(date) |
| 161 | + return dateProvider |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + fun `getTimeToDisplay resolves once with the frame timestamp in seconds`() { |
| 166 | + val promise = mock<Promise>() |
| 167 | + val logger = mock<ILogger>() |
| 168 | + val dateProvider = mockDateProvider(2_000_000_000L) // 2s expressed in nanoseconds |
| 169 | + |
| 170 | + RNSentryTimeToDisplay.getTimeToDisplay(promise, dateProvider, logger) |
| 171 | + |
| 172 | + // Run the posted main-thread runnable and the Choreographer frame callback. |
| 173 | + shadowOf(Looper.getMainLooper()).idle() |
| 174 | + |
| 175 | + verify(promise, times(1)).resolve(2.0) |
| 176 | + verify(promise, never()).reject(any<String>(), any<String>()) |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + fun `getTimeToDisplay does not crash the host app when the promise was already settled`() { |
| 181 | + val promise = mock<Promise>() |
| 182 | + val logger = mock<ILogger>() |
| 183 | + val dateProvider = mockDateProvider(2_000_000_000L) |
| 184 | + // Reproduce the reported crash: the bridge callback backing the promise has already been |
| 185 | + // settled by the runtime, so resolving it from the frame callback throws. (#6709) |
| 186 | + doThrow(RuntimeException("JavaCallback was already settled. Cannot invoke it again")) |
| 187 | + .whenever(promise) |
| 188 | + .resolve(any()) |
| 189 | + |
| 190 | + RNSentryTimeToDisplay.getTimeToDisplay(promise, dateProvider, logger) |
| 191 | + |
| 192 | + // Before the fix this propagated out of the frame callback as a fatal uncaught exception. |
| 193 | + shadowOf(Looper.getMainLooper()).idle() |
| 194 | + |
| 195 | + verify(logger).log(eq(SentryLevel.WARNING), any<String>(), any<Throwable>()) |
| 196 | + } |
48 | 197 | } |
0 commit comments