Skip to content
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

[QA] Move NDK scope sync to background thread #3754

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- If you're using code obfuscation, adjust your proguard-rules accordingly, so your custom view class name is not minified
- Fix ensure Application Context is used even when SDK is initialized via Activity Context ([#3669](https://github.com/getsentry/sentry-java/pull/3669))
- Fix potential ANRs due to `Calendar.getInstance` usage in Breadcrumbs constructor ([#3736](https://github.com/getsentry/sentry-java/pull/3736))
- Fix potential ANRs due to NDK scope sync ([#3754](https://github.com/getsentry/sentry-java/pull/3754))

*Breaking changes*:

Expand Down
2 changes: 1 addition & 1 deletion sentry-android-ndk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ dependencies {

testImplementation(kotlin(Config.kotlinStdLib, KotlinCompilerVersion.VERSION))
testImplementation(Config.TestLibs.kotlinTestJunit)

testImplementation(Config.TestLibs.mockitoKotlin)
testImplementation(projects.sentryTestSupport)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ public NdkScopeObserver(final @NotNull SentryOptions options) {
@Override
public void setUser(final @Nullable User user) {
try {
if (user == null) {
// remove user if its null
nativeScope.removeUser();
} else {
nativeScope.setUser(user.getId(), user.getEmail(), user.getIpAddress(), user.getUsername());
}
options
.getExecutorService()
.submit(
() -> {
if (user == null) {
// remove user if its null
nativeScope.removeUser();
} else {
nativeScope.setUser(
user.getId(), user.getEmail(), user.getIpAddress(), user.getUsername());
}
});
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync setUser has an error.");
}
Expand All @@ -45,24 +51,36 @@ public void setUser(final @Nullable User user) {
@Override
public void addBreadcrumb(final @NotNull Breadcrumb crumb) {
try {
String level = null;
if (crumb.getLevel() != null) {
level = crumb.getLevel().name().toLowerCase(Locale.ROOT);
}
final String timestamp = DateUtils.getTimestamp(crumb.getTimestamp());
options
.getExecutorService()
.submit(
() -> {
String level = null;
if (crumb.getLevel() != null) {
level = crumb.getLevel().name().toLowerCase(Locale.ROOT);
}
final String timestamp = DateUtils.getTimestamp(crumb.getTimestamp());

String data = null;
try {
final Map<String, Object> dataRef = crumb.getData();
if (!dataRef.isEmpty()) {
data = options.getSerializer().serialize(dataRef);
}
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Breadcrumb data is not serializable.");
}
String data = null;
try {
final Map<String, Object> dataRef = crumb.getData();
if (!dataRef.isEmpty()) {
data = options.getSerializer().serialize(dataRef);
}
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.ERROR, e, "Breadcrumb data is not serializable.");
}

nativeScope.addBreadcrumb(
level, crumb.getMessage(), crumb.getCategory(), crumb.getType(), timestamp, data);
nativeScope.addBreadcrumb(
level,
crumb.getMessage(),
crumb.getCategory(),
crumb.getType(),
timestamp,
data);
});
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync addBreadcrumb has an error.");
}
Expand All @@ -71,7 +89,7 @@ public void addBreadcrumb(final @NotNull Breadcrumb crumb) {
@Override
public void setTag(final @NotNull String key, final @NotNull String value) {
try {
nativeScope.setTag(key, value);
options.getExecutorService().submit(() -> nativeScope.setTag(key, value));
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync setTag(%s) has an error.", key);
}
Expand All @@ -80,7 +98,7 @@ public void setTag(final @NotNull String key, final @NotNull String value) {
@Override
public void removeTag(final @NotNull String key) {
try {
nativeScope.removeTag(key);
options.getExecutorService().submit(() -> nativeScope.removeTag(key));
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync removeTag(%s) has an error.", key);
}
Expand All @@ -89,7 +107,7 @@ public void removeTag(final @NotNull String key) {
@Override
public void setExtra(final @NotNull String key, final @NotNull String value) {
try {
nativeScope.setExtra(key, value);
options.getExecutorService().submit(() -> nativeScope.setExtra(key, value));
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync setExtra(%s) has an error.", key);
}
Expand All @@ -98,7 +116,7 @@ public void setExtra(final @NotNull String key, final @NotNull String value) {
@Override
public void removeExtra(final @NotNull String key) {
try {
nativeScope.removeExtra(key);
options.getExecutorService().submit(() -> nativeScope.removeExtra(key));
} catch (Throwable e) {
options
.getLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import io.sentry.JsonSerializer
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.protocol.User
import io.sentry.test.DeferredExecutorService
import io.sentry.test.ImmediateExecutorService
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import kotlin.test.Test

Expand All @@ -17,6 +22,7 @@ class NdkScopeObserverTest {
val nativeScope = mock<INativeScope>()
val options = SentryOptions().apply {
setSerializer(JsonSerializer(mock()))
executorService = ImmediateExecutorService()
}

fun getSut(): NdkScopeObserver {
Expand Down Expand Up @@ -111,4 +117,36 @@ class NdkScopeObserverTest {
eq(data)
)
}

@Test
fun `scope sync utilizes executor service`() {
val executorService = DeferredExecutorService()
fixture.options.executorService = executorService
val sut = fixture.getSut()

sut.setTag("a", "b")
sut.removeTag("a")
sut.setExtra("a", "b")
sut.removeExtra("a")
sut.setUser(User())
sut.addBreadcrumb(Breadcrumb())

// as long as the executor service is not run, the scope sync is not called
verify(fixture.nativeScope, never()).setTag(any(), any())
verify(fixture.nativeScope, never()).removeTag(any())
verify(fixture.nativeScope, never()).setExtra(any(), any())
verify(fixture.nativeScope, never()).removeExtra(any())
verify(fixture.nativeScope, never()).setUser(any(), any(), any(), any())
verify(fixture.nativeScope, never()).addBreadcrumb(any(), any(), any(), any(), any(), any())

// when the executor service is run, the scope sync is called
executorService.runAll()

verify(fixture.nativeScope).setTag(any(), any())
verify(fixture.nativeScope).removeTag(any())
verify(fixture.nativeScope).setExtra(any(), any())
verify(fixture.nativeScope).removeExtra(any())
verify(fixture.nativeScope).setUser(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
verify(fixture.nativeScope).addBreadcrumb(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
}
}
Loading