Skip to content

Commit

Permalink
Merge branch 'main' into fix/activity-feature-notitle
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Oct 18, 2021
2 parents 2c2b4e3 + a001bbf commit ac49e6e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Fix: Window.FEATURE_NO_TITLE does not work when using activity traces (#1769)
* Fix: unregister UncaughtExceptionHandler on close (#1770)

## 5.2.3

Expand Down
7 changes: 5 additions & 2 deletions sentry/src/main/java/io/sentry/UncaughtExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.sentry;

import org.jetbrains.annotations.Nullable;

/** An adapter to make UncaughtExceptionHandler testable */
interface UncaughtExceptionHandler {
Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler();

void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler);
void setDefaultUncaughtExceptionHandler(@Nullable Thread.UncaughtExceptionHandler handler);

final class Adapter implements UncaughtExceptionHandler {

Expand All @@ -22,7 +24,8 @@ public Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler() {
}

@Override
public void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler) {
public void setDefaultUncaughtExceptionHandler(
final @Nullable Thread.UncaughtExceptionHandler handler) {
Thread.setDefaultUncaughtExceptionHandler(handler);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ static Throwable getUnhandledThrowable(

@Override
public void close() {
if (defaultExceptionHandler != null
&& this == threadAdapter.getDefaultUncaughtExceptionHandler()) {
if (this == threadAdapter.getDefaultUncaughtExceptionHandler()) {
threadAdapter.setDefaultUncaughtExceptionHandler(defaultExceptionHandler);

if (options != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,33 @@ class UncaughtExceptionHandlerIntegrationTest {
integration.register(hub, options)
verify(handlerMock).defaultUncaughtExceptionHandler = argWhere { it is UncaughtExceptionHandlerIntegration }
}

@Test
fun `When defaultUncaughtExceptionHandler is set and integration is closed, default uncaught exception handler is reset to previous handler`() {
val handlerMock = mock<UncaughtExceptionHandler>()
val integration = UncaughtExceptionHandlerIntegration(handlerMock)

val defaultExceptionHandler = mock<Thread.UncaughtExceptionHandler>()
whenever(handlerMock.defaultUncaughtExceptionHandler)
.thenReturn(defaultExceptionHandler)
integration.register(mock(), SentryOptions())
whenever(handlerMock.defaultUncaughtExceptionHandler)
.thenReturn(integration)
integration.close()
verify(handlerMock).defaultUncaughtExceptionHandler = defaultExceptionHandler
}

@Test
fun `When defaultUncaughtExceptionHandler is not set and integration is closed, default uncaught exception handler is reset to null`() {
val handlerMock = mock<UncaughtExceptionHandler>()
val integration = UncaughtExceptionHandlerIntegration(handlerMock)

whenever(handlerMock.defaultUncaughtExceptionHandler)
.thenReturn(null)
integration.register(mock(), SentryOptions())
whenever(handlerMock.defaultUncaughtExceptionHandler)
.thenReturn(integration)
integration.close()
verify(handlerMock).defaultUncaughtExceptionHandler = null
}
}

0 comments on commit ac49e6e

Please sign in to comment.