-
-
Notifications
You must be signed in to change notification settings - Fork 366
fix(replay): Defer buffered replay upload until after error sampling #6685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
antonis
wants to merge
3
commits into
main
Choose a base branch
from
antonis/fix-6598-buffer-replay-orphaned
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
packages/core/android/src/test/java/io/sentry/react/RNSentryReplayIdTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| package io.sentry.react; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import android.content.pm.PackageInfo; | ||
| import android.content.pm.PackageManager; | ||
| import com.facebook.react.bridge.Promise; | ||
| import com.facebook.react.bridge.ReactApplicationContext; | ||
| import io.sentry.IScope; | ||
| import io.sentry.IScopes; | ||
| import io.sentry.ReplayController; | ||
| import io.sentry.Sentry; | ||
| import io.sentry.SentryOptions; | ||
| import io.sentry.android.core.InternalSentrySdk; | ||
| import io.sentry.protocol.SentryId; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| /** | ||
| * Coverage for {@link RNSentryModuleImpl#getCurrentReplayId()} and its buffer (on-error) replay | ||
| * lookup added for https://github.com/getsentry/sentry-react-native/issues/6598. | ||
| * | ||
| * <p>A buffer replay's id is assigned by the {@link ReplayController} when recording starts, but | ||
| * the scope's replayId is only populated once a replay is sent. The JS mobile replay integration | ||
| * must be able to read the buffered id BEFORE the replay is flushed, so {@code | ||
| * getCurrentReplayId()} prefers the controller's id and only falls back to the scope. | ||
| */ | ||
| public class RNSentryReplayIdTest { | ||
|
|
||
| private RNSentryModuleImpl module; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| ReactApplicationContext reactContext = mock(ReactApplicationContext.class); | ||
| PackageManager packageManager = mock(PackageManager.class); | ||
| when(packageManager.getPackageInfo(anyString(), anyInt())).thenReturn(new PackageInfo()); | ||
| when(reactContext.getPackageManager()).thenReturn(packageManager); | ||
| when(reactContext.getPackageName()).thenReturn("com.test.app"); | ||
| module = new RNSentryModuleImpl(reactContext); | ||
| } | ||
|
|
||
| /** | ||
| * Wires {@code Sentry.getCurrentScopes().getOptions().getReplayController()} to return the id and | ||
| * hands back the mocked controller so callers can verify interactions with it. | ||
| */ | ||
| private ReplayController stubControllerReplayId( | ||
| final MockedStatic<Sentry> sentry, final SentryId id) { | ||
| final ReplayController replayController = mock(ReplayController.class); | ||
| when(replayController.getReplayId()).thenReturn(id); | ||
| final SentryOptions options = mock(SentryOptions.class); | ||
| when(options.getReplayController()).thenReturn(replayController); | ||
| final IScopes scopes = mock(IScopes.class); | ||
| when(scopes.getOptions()).thenReturn(options); | ||
| sentry.when(Sentry::getCurrentScopes).thenReturn(scopes); | ||
| return replayController; | ||
| } | ||
|
|
||
| @Test | ||
| public void prefersReplayControllerIdWhenBuffering() { | ||
| // A buffer replay is recording: the controller exposes its id even though the scope has none. | ||
| final SentryId bufferedId = new SentryId(); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| stubControllerReplayId(sentry, bufferedId); | ||
| // Scope has no replay id yet โ the fix must not depend on it. | ||
| final IScope scope = mock(IScope.class); | ||
| when(scope.getReplayId()).thenReturn(SentryId.EMPTY_ID); | ||
| internal.when(InternalSentrySdk::getCurrentScope).thenReturn(scope); | ||
|
|
||
| assertEquals(bufferedId.toString(), module.getCurrentReplayId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void fallsBackToScopeIdWhenControllerEmpty() { | ||
| // A full-session replay was sent: the controller reports empty, the id lives on the scope. | ||
| final SentryId scopeId = new SentryId(); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| stubControllerReplayId(sentry, SentryId.EMPTY_ID); | ||
| final IScope scope = mock(IScope.class); | ||
| when(scope.getReplayId()).thenReturn(scopeId); | ||
| internal.when(InternalSentrySdk::getCurrentScope).thenReturn(scope); | ||
|
|
||
| assertEquals(scopeId.toString(), module.getCurrentReplayId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullWhenControllerEmptyAndScopeEmpty() { | ||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| stubControllerReplayId(sentry, SentryId.EMPTY_ID); | ||
| final IScope scope = mock(IScope.class); | ||
| when(scope.getReplayId()).thenReturn(SentryId.EMPTY_ID); | ||
| internal.when(InternalSentrySdk::getCurrentScope).thenReturn(scope); | ||
|
|
||
| assertNull(module.getCurrentReplayId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsNullWhenControllerEmptyAndScopeNull() { | ||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| stubControllerReplayId(sentry, SentryId.EMPTY_ID); | ||
| internal.when(InternalSentrySdk::getCurrentScope).thenReturn(null); | ||
|
|
||
| assertNull(module.getCurrentReplayId()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void captureReplayResolvesNullOnSamplingMissEvenWhileBuffering() { | ||
| // On an on-error sampling miss the controller still holds the buffered id, but the scope has | ||
| // none because nothing was uploaded. captureReplay must resolve null (matching iOS), rather | ||
| // than leak the buffered id as if a replay had been sent. | ||
| final SentryId bufferedId = new SentryId(); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| final ReplayController replayController = stubControllerReplayId(sentry, bufferedId); | ||
| final IScope scope = mock(IScope.class); | ||
| when(scope.getReplayId()).thenReturn(SentryId.EMPTY_ID); | ||
| internal.when(InternalSentrySdk::getCurrentScope).thenReturn(scope); | ||
|
|
||
| final Promise promise = mock(Promise.class); | ||
| module.captureReplay(true, promise); | ||
|
|
||
| verify(replayController).captureReplay(true); | ||
| verify(promise).resolve(null); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void captureReplayResolvesScopeIdWhenReplayWasSent() { | ||
| // When a replay is actually sent the scope carries its id; captureReplay resolves that, not the | ||
| // controller's id, so JS learns the real uploaded replay id. | ||
| final SentryId bufferedId = new SentryId(); | ||
| final SentryId scopeId = new SentryId(); | ||
|
|
||
| try (MockedStatic<Sentry> sentry = mockStatic(Sentry.class); | ||
| MockedStatic<InternalSentrySdk> internal = mockStatic(InternalSentrySdk.class)) { | ||
| final ReplayController replayController = stubControllerReplayId(sentry, bufferedId); | ||
| final IScope scope = mock(IScope.class); | ||
| when(scope.getReplayId()).thenReturn(scopeId); | ||
| internal.when(InternalSentrySdk::getCurrentScope).thenReturn(scope); | ||
|
|
||
| final Promise promise = mock(Promise.class); | ||
| module.captureReplay(false, promise); | ||
|
|
||
| verify(replayController).captureReplay(false); | ||
| verify(promise).resolve(scopeId.toString()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.